diff options
author | Nick White <git@njw.name> | 2020-08-03 15:56:49 +0100 |
---|---|---|
committer | Nick White <git@njw.name> | 2020-08-03 15:56:49 +0100 |
commit | a7fca431e4eceb5523943e14620e1f2f2f563aed (patch) | |
tree | 1e19a0f3783ec2d1e8eda3363f363eed8ea9b866 | |
parent | 1fe7dcdd23691212e8393559fc9d7d5100981386 (diff) |
Add tests for Mean()
-rw-r--r-- | integralimg_test.go | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/integralimg_test.go b/integralimg_test.go index 35b353d..ccc6fc2 100644 --- a/integralimg_test.go +++ b/integralimg_test.go @@ -92,6 +92,46 @@ func TestSum(t *testing.T) { } } +func TestMean(t *testing.T) { + f, err := os.Open("testdata/in.png") + if err != nil { + t.Fatalf("Could not open file %s: %v\n", "testdata/in.png", err) + } + defer f.Close() + img, _, err := image.Decode(f) + if err != nil { + t.Fatalf("Could not decode image: %v\n", err) + } + b := img.Bounds() + + imgplus := newGray16Plus(b) + integral := NewImage(b) + + draw.Draw(imgplus, b, img, b.Min, draw.Src) + draw.Draw(integral, b, img, b.Min, draw.Src) + + cases := []struct { + name string + r image.Rectangle + }{ + {"fullimage", b}, + {"small", image.Rect(1, 1, 5, 5)}, + {"toobig", image.Rect(0, 0, 2000, b.Dy())}, + {"toosmall", image.Rect(-1, -1, 4, 5)}, + {"small2", image.Rect(0, 0, 4, 4)}, + } + + for _, c := range cases{ + t.Run(c.name, func(t *testing.T) { + meanimg := imgplus.mean(c.r) + meanint := integral.Mean(c.r) + if meanimg != meanint { + t.Errorf("Mean of integral image differs to regular image: regular: %f, integral: %f\n", meanimg, meanint) + } + }) + } +} + func imgsequal(img1, img2 image.Image) bool { b := img1.Bounds() if !b.Eq(img2.Bounds()) { @@ -138,3 +178,8 @@ func (i grayPlus) sum(r image.Rectangle) uint64 { } return sum } + +func (i grayPlus) mean(r image.Rectangle) float64 { + in := r.Intersect(i.Bounds()) + return float64(i.sum(r)) / float64(in.Dx() * in.Dy()) +} |