Split files and improved implementation

This commit is contained in:
Russell Stanley 2021-12-17 16:33:31 +10:30
parent 613335918b
commit d994e94678
2 changed files with 149 additions and 0 deletions

130
turbidity/main.go Normal file
View File

@ -0,0 +1,130 @@
package main
import (
"fmt"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/plotutil"
"gonum.org/v1/plot/vg"
"gocv.io/x/gocv"
)
func main() {
n := 10
// Load template
template := gocv.IMRead("template.jpg", gocv.IMReadGrayScale)
// Read images
images := make([]gocv.Mat, n)
for i := 0; i < n; i++ {
images[i] = gocv.IMRead(fmt.Sprintf("images/generation-5/000%v.jpg", i), gocv.IMReadGrayScale)
}
// Calulate turbidity
ts := TurbiditySensor{template: template, k1: 90, k2: 90, scale: 5.0, alpha: 1.0}
err := ts.Evaluate(n, images)
if err != nil {
fmt.Println(err)
}
}
// RESULTS
type Results struct {
turbidity []float64
saturation []float64
contrast []float64
}
func (r *Results) New(n int) {
r.turbidity = make([]float64, n)
r.saturation = make([]float64, n)
r.contrast = make([]float64, n)
}
func (r *Results) Append(saturation, contrast, turbidity float64, i int) {
r.saturation[i] = saturation
r.contrast[i] = contrast
r.turbidity[i] = turbidity
}
func Normalize(slice []float64, n int) []float64 {
max := -1e10
min := 1e10
out := make([]float64, n)
if n <= 1 {
return slice
}
for i := 0; i < n; i++ {
if slice[i] > max {
max = slice[i]
}
if slice[i] < min {
min = slice[i]
}
}
for i := 0; i < n; i++ {
out[i] = (slice[i] - min) / (max - min)
}
return out
}
// PLOTTING
func PlotResults(x, saturation, contrast []float64) error {
err := plotToFile(
"Results",
"Filter Size",
"Score",
func(p *plot.Plot) error {
return plotutil.AddLinePoints(p,
"AMEE", plotterXY(x, contrast),
"EME", plotterXY(x, saturation),
)
},
)
if err != nil {
return fmt.Errorf("Could not plot results: %v", err)
}
return nil
}
// plotToFile creates a plot with a specified name and x&y titles using the
// provided draw function, and then saves to a PNG file with filename of name.
func plotToFile(name, xTitle, yTitle string, draw func(*plot.Plot) error) error {
p := plot.New()
p.Title.Text = name
p.X.Label.Text = xTitle
p.Y.Label.Text = yTitle
err := draw(p)
if err != nil {
return fmt.Errorf("could not draw plot contents: %w", err)
}
if err := p.Save(15*vg.Centimeter, 15*vg.Centimeter, "plots/"+name+".png"); err != nil {
return fmt.Errorf("could not save plot: %w", err)
}
return nil
}
// plotterXY provides a plotter.XYs type value based on the given x and y data.
func plotterXY(x, y []float64) plotter.XYs {
xy := make(plotter.XYs, len(x))
for i := range x {
xy[i].X = x[i]
xy[i].Y = y[i]
}
return xy
}

19
turbidity/results.go Normal file
View File

@ -0,0 +1,19 @@
package main
type Results struct {
turbidity []float64
saturation []float64
contrast []float64
}
func (r *Results) New(n int) {
r.turbidity = make([]float64, n)
r.saturation = make([]float64, n)
r.contrast = make([]float64, n)
}
func (r *Results) Append(saturation, contrast, turbidity float64, i int) {
r.saturation[i] = saturation
r.contrast[i] = contrast
r.turbidity[i] = turbidity
}