From e65a664a80cb04e68662d0867067447dce78fe06 Mon Sep 17 00:00:00 2001 From: Russell Stanley Date: Thu, 23 Dec 2021 16:40:35 +1030 Subject: [PATCH] Updated experiment --- turbidity/main.go | 15 ++++----------- turbidity/results.go | 4 ++-- turbidity/turbidity.go | 12 +++++++----- 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/turbidity/main.go b/turbidity/main.go index e1b3034a..f43f375f 100644 --- a/turbidity/main.go +++ b/turbidity/main.go @@ -14,17 +14,14 @@ import ( ) const ( - nImages = 6 + nImages = 13 nSamples = 10 ) func main() { - nImages := 6 - nSamples := 10 - // Load template and standard image. template := gocv.IMRead("template.jpg", gocv.IMReadGrayScale) - standard := gocv.IMRead("standard.jpg", gocv.IMReadGrayScale) + standard := gocv.IMRead("default.jpg", gocv.IMReadGrayScale) imgs := make([][]gocv.Mat, nImages) @@ -40,7 +37,7 @@ func main() { ts := TurbiditySensor{template: template, standard: standard, k1: 90, k2: 90, scale: 5.0, alpha: 1.0} var finalRes Results - finalRes.New(nImages) + finalRes.new(nImages) // Score each image by calculating the average score from camera burst. for i := range imgs { @@ -52,7 +49,7 @@ func main() { } // Add the average result from camera burst. - finalRes.Update(average(sample_result.saturation), average(sample_result.contrast), float64(i*10), i) + finalRes.update(average(sample_result.saturation), average(sample_result.contrast), float64(i*10), i) } // Plot the final results. @@ -91,7 +88,6 @@ func normalize(slice []float64) []float64 { for i := range slice { out[i] = (slice[i] - min) / (max - min) } - return out } @@ -103,7 +99,6 @@ func average(slice []float64) float64 { for i := range slice { out += slice[i] } - return out / float64(len(slice)) } @@ -120,11 +115,9 @@ func plotResults(x, saturation, contrast []float64) error { ) }, ) - if err != nil { return fmt.Errorf("Could not plot results: %w", err) } - return nil } diff --git a/turbidity/results.go b/turbidity/results.go index b75dc9ff..4dc24b52 100644 --- a/turbidity/results.go +++ b/turbidity/results.go @@ -7,14 +7,14 @@ type Results struct { contrast []float64 } -func (r *Results) New(n int) { +func (r *Results) new(n int) { r.turbidity = make([]float64, n) r.saturation = make([]float64, n) r.contrast = make([]float64, n) } // Update results to add new values at specified index. -func (r *Results) Update(saturation, contrast, turbidity float64, index int) { +func (r *Results) update(saturation, contrast, turbidity float64, index int) { r.saturation[index] = saturation r.contrast[index] = contrast r.turbidity[index] = turbidity diff --git a/turbidity/turbidity.go b/turbidity/turbidity.go index afef3849..e7708aad 100644 --- a/turbidity/turbidity.go +++ b/turbidity/turbidity.go @@ -20,7 +20,7 @@ type TurbiditySensor struct { func (ts TurbiditySensor) Evaluate(imgs []gocv.Mat) (Results, error) { var result Results - result.New(len(imgs)) + result.new(len(imgs)) for i := range imgs { @@ -33,13 +33,16 @@ func (ts TurbiditySensor) Evaluate(imgs []gocv.Mat) (Results, error) { // Apply sobel filter. edge := ts.Sobel(marker, ts.scale) + gocv.IMWrite("marker.jpg", marker) + gocv.IMWrite("edge.jpg", edge) + // Evaluate image. out, err := ts.EvaluateImage(marker, edge, ts.k1, ts.k2, ts.alpha) if err != nil { return result, err } - result.Update(out[0], out[1], float64(i*10), i) + result.update(out[0], out[1], float64(i*10), i) } return result, nil @@ -48,7 +51,6 @@ func (ts TurbiditySensor) Evaluate(imgs []gocv.Mat) (Results, error) { // Evaluate image sharpness and contrast using blocks of size k1 by k2. Return a slice of the respective scores. func (ts TurbiditySensor) EvaluateImage(img, edge gocv.Mat, k1, k2 int, alpha float64) ([]float64, error) { - // Slice to store results. result := make([]float64, 2) // [0.0, 0.0] if img.Rows()%k1 != 0 || img.Cols()%k2 != 0 { @@ -127,9 +129,9 @@ func (ts TurbiditySensor) Transform(img gocv.Mat) (gocv.Mat, error) { corners_template := gocv.NewMat() // Find corners in image. - if !gocv.FindChessboardCorners(img, image.Pt(3, 3), &corners_img, gocv.CalibCBNormalizeImage) { + if !gocv.FindChessboardCorners(ts.standard, image.Pt(3, 3), &corners_img, gocv.CalibCBNormalizeImage) { // Apply default if transformation fails. - // fmt.Println("Corner detection failed applying standard transformation") + fmt.Println("Corner detection failed applying standard transformation") if !gocv.FindChessboardCorners(ts.standard, image.Pt(3, 3), &corners_img, gocv.CalibCBNormalizeImage) { return out, errors.New("Could not find corners in default image") }