Updated experiment

This commit is contained in:
Russell Stanley 2021-12-23 16:40:35 +10:30
parent 86cdff0bb2
commit e65a664a80
3 changed files with 13 additions and 18 deletions

View File

@ -14,17 +14,14 @@ import (
) )
const ( const (
nImages = 6 nImages = 13
nSamples = 10 nSamples = 10
) )
func main() { func main() {
nImages := 6
nSamples := 10
// Load template and standard image. // Load template and standard image.
template := gocv.IMRead("template.jpg", gocv.IMReadGrayScale) 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) 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} ts := TurbiditySensor{template: template, standard: standard, k1: 90, k2: 90, scale: 5.0, alpha: 1.0}
var finalRes Results var finalRes Results
finalRes.New(nImages) finalRes.new(nImages)
// Score each image by calculating the average score from camera burst. // Score each image by calculating the average score from camera burst.
for i := range imgs { for i := range imgs {
@ -52,7 +49,7 @@ func main() {
} }
// Add the average result from camera burst. // 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. // Plot the final results.
@ -91,7 +88,6 @@ func normalize(slice []float64) []float64 {
for i := range slice { for i := range slice {
out[i] = (slice[i] - min) / (max - min) out[i] = (slice[i] - min) / (max - min)
} }
return out return out
} }
@ -103,7 +99,6 @@ func average(slice []float64) float64 {
for i := range slice { for i := range slice {
out += slice[i] out += slice[i]
} }
return out / float64(len(slice)) return out / float64(len(slice))
} }
@ -120,11 +115,9 @@ func plotResults(x, saturation, contrast []float64) error {
) )
}, },
) )
if err != nil { if err != nil {
return fmt.Errorf("Could not plot results: %w", err) return fmt.Errorf("Could not plot results: %w", err)
} }
return nil return nil
} }

View File

@ -7,14 +7,14 @@ type Results struct {
contrast []float64 contrast []float64
} }
func (r *Results) New(n int) { func (r *Results) new(n int) {
r.turbidity = make([]float64, n) r.turbidity = make([]float64, n)
r.saturation = make([]float64, n) r.saturation = make([]float64, n)
r.contrast = make([]float64, n) r.contrast = make([]float64, n)
} }
// Update results to add new values at specified index. // 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.saturation[index] = saturation
r.contrast[index] = contrast r.contrast[index] = contrast
r.turbidity[index] = turbidity r.turbidity[index] = turbidity

View File

@ -20,7 +20,7 @@ type TurbiditySensor struct {
func (ts TurbiditySensor) Evaluate(imgs []gocv.Mat) (Results, error) { func (ts TurbiditySensor) Evaluate(imgs []gocv.Mat) (Results, error) {
var result Results var result Results
result.New(len(imgs)) result.new(len(imgs))
for i := range imgs { for i := range imgs {
@ -33,13 +33,16 @@ func (ts TurbiditySensor) Evaluate(imgs []gocv.Mat) (Results, error) {
// Apply sobel filter. // Apply sobel filter.
edge := ts.Sobel(marker, ts.scale) edge := ts.Sobel(marker, ts.scale)
gocv.IMWrite("marker.jpg", marker)
gocv.IMWrite("edge.jpg", edge)
// Evaluate image. // Evaluate image.
out, err := ts.EvaluateImage(marker, edge, ts.k1, ts.k2, ts.alpha) out, err := ts.EvaluateImage(marker, edge, ts.k1, ts.k2, ts.alpha)
if err != nil { if err != nil {
return result, err 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 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. // 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) { 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] result := make([]float64, 2) // [0.0, 0.0]
if img.Rows()%k1 != 0 || img.Cols()%k2 != 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() corners_template := gocv.NewMat()
// Find corners in image. // 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. // 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) { if !gocv.FindChessboardCorners(ts.standard, image.Pt(3, 3), &corners_img, gocv.CalibCBNormalizeImage) {
return out, errors.New("Could not find corners in default image") return out, errors.New("Could not find corners in default image")
} }