From d994e946781ccbc310722f56d70d43c007f8484e Mon Sep 17 00:00:00 2001 From: Russell Stanley Date: Fri, 17 Dec 2021 16:33:31 +1030 Subject: [PATCH] Split files and improved implementation --- turbidity/main.go | 130 +++++++++++++++++++++++++++++++++++++++++++ turbidity/results.go | 19 +++++++ 2 files changed, 149 insertions(+) create mode 100644 turbidity/main.go create mode 100644 turbidity/results.go diff --git a/turbidity/main.go b/turbidity/main.go new file mode 100644 index 00000000..f5cc1874 --- /dev/null +++ b/turbidity/main.go @@ -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 +} diff --git a/turbidity/results.go b/turbidity/results.go new file mode 100644 index 00000000..be3f58b2 --- /dev/null +++ b/turbidity/results.go @@ -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 +} \ No newline at end of file