Skip to content

Instantly share code, notes, and snippets.

@prl900
Last active July 9, 2020 23:52
Show Gist options
  • Save prl900/9874162c6e84692fef6012e82774f205 to your computer and use it in GitHub Desktop.
Save prl900/9874162c6e84692fef6012e82774f205 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"image"
"image/png"
"math"
"os"
"time"
"github.com/terrascope/scimage"
"github.com/terrascope/scimage/scicolor"
)
func ComputeKc(im *image.NRGBA) (*scimage.GrayU8, error) {
out := scimage.NewGrayU8(im.Rect, 0, 100, 255)
for i := 0; i < im.Bounds().Dx(); i++ {
for j := 0; j < im.Bounds().Dy(); j++ {
c := im.NRGBAAt(i, j)
red, blue, nir, swir1 := float64(c.R)*2e-3, float64(c.G)*2e-3, float64(c.B)*2e-3, float64(c.A)*2e-3
evi := 2.5 * (nir - red) / (nir + 6*red - 7.5*blue + 1)
gvmi := ((nir + 0.1) - (swir1 + 0.02)) / ((nir + 0.1) + (swir1 + 0.02))
rmi := math.Max(0, gvmi-(0.775*evi-0.0757))
evir := math.Max(0, math.Min(1, evi))
kc := 0.680 * (1 - math.Exp(-14.12*math.Pow(evir, 2.482)-7.991*math.Pow(rmi, 0.890)))
out.SetGrayU8(i, j, scicolor.GrayU8{Y: uint8(kc * 100)})
}
}
return out, nil
}
func main() {
fmt.Println("Hello")
start := time.Now()
f, err := os.Open("./refl_DEA_+13_-35_201702_l0.png")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer f.Close()
im, err := png.Decode(f)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("Openning file took %s\n", time.Since(start))
start = time.Now()
kc, _ := ComputeKc(im.(*image.NRGBA))
fmt.Printf("Computing Kc took %s\n", time.Since(start))
start = time.Now()
out, err := os.Create("./kc_DEA_+13_-35_201702_l0.png")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
err = png.Encode(out, kc)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("Saving file took %s\n", time.Since(start))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment