av/turbidity/turbidity_test.go

89 lines
2.3 KiB
Go

/*
DESCRIPTION
Testing functions for the turbidity sensor using images from
previous experiment.
AUTHORS
Russell Stanley <russell@ausocean.org>
LICENSE
Copyright (C) 2020 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
in gpl.txt. If not, see http://www.gnu.org/licenses.
*/
package main
import (
"fmt"
"testing"
"gocv.io/x/gocv"
)
const (
nImages = 13 // Number of images to test. (Max 13)
nSamples = 10 // Number of samples for each image. (Max 10)
increment = 2.5
)
func TestImages(t *testing.T) {
// Load template and standard image.
template := gocv.IMRead("images/template.jpg", gocv.IMReadGrayScale)
standard := gocv.IMRead("images/default.jpg", gocv.IMReadGrayScale)
imgs := make([][]gocv.Mat, nImages)
// Load test images.
for i := range imgs {
imgs[i] = make([]gocv.Mat, nSamples)
for j := range imgs[i] {
imgs[i][j] = gocv.IMRead(fmt.Sprintf("images/t-%v/000%v.jpg", i, j), gocv.IMReadGrayScale)
}
}
// Create turbidity sensor.
ts, err := NewTurbiditySensor(template, standard, 8, 8, 3, 1.0, 1.0)
if err != nil {
t.Fatal(err)
}
// Create results
results, err := NewResults(nImages)
if err != nil {
t.Fatal(err)
}
// Score each image by calculating the average score from camera burst.
for i := range imgs {
// Evaluate camera burst.
sample_result, err := ts.Evaluate(imgs[i])
if err != nil {
t.Fatalf("Evaluation Failed: %v", err)
}
// Add the average result from camera burst.
results.Update(average(sample_result.Saturation), average(sample_result.Contrast), float64(i)*increment, i)
}
// Plot the final results.
err = plotResults(results.Turbidity, normalize(results.Saturation), normalize(results.Contrast))
if err != nil {
t.Fatalf("Plotting Failed: %v", err)
}
t.Logf("Saturation: %v", results.Saturation)
t.Logf("Contrast: %v", results.Contrast)
}