add standard deviation function

This commit is contained in:
Russell Stanley 2022-01-10 10:08:54 +10:30 committed by Saxon Nelson-Milton
parent 88f0716f7b
commit 07c8d4774a
3 changed files with 14 additions and 3 deletions

View File

@ -34,6 +34,17 @@ import (
"gonum.org/v1/plot/vg"
)
// standarDeviation will return the standard deviation of a float slice
func standarDeviation(slice []float64) float64 {
mean := average(slice)
variance := 0.0
for _, i := range slice {
variance += math.Pow(i-mean, 2.0)
}
return math.Sqrt(variance / float64(len(slice)))
}
// Normalize values in a slice between 0 and 1.
func normalize(slice []float64) []float64 {
max := -math.MaxFloat64

View File

@ -142,7 +142,7 @@ func (ts TurbiditySensor) minMax(img gocv.Mat, xStart, yStart, xEnd, yEnd int) (
for j := yStart; j < yEnd; j++ {
value := float64(img.GetUCharAt(i, j))
// Check max/min conditions, zero values are ignored.
// Check max/min conditions, zero values are ignoredt to avoid divison by 0.
if value > max && value != 0.0 {
max = value
}

View File

@ -76,7 +76,7 @@ func TestImages(t *testing.T) {
// Evaluate camera burst.
sample_result, err := ts.Evaluate(imgs[i])
if err != nil {
t.Fatalf("evaluation Failed: %w", err)
t.Fatalf("evaluation Failed: %v", err)
}
// Add the average result from camera burst.
@ -86,7 +86,7 @@ func TestImages(t *testing.T) {
// Plot the final results.
err = plotResults(results.Turbidity, normalize(results.Sharpness), normalize(results.Contrast))
if err != nil {
t.Fatalf("plotting Failed: %w", err)
t.Fatalf("plotting Failed: %v", err)
}
t.Logf("Sharpness: %v", results.Sharpness)