summaryrefslogtreecommitdiff
path: root/example_test.go
blob: 895855758f2961a42ce1325adb95ddd42c62526f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2020 Nick White.
// Use of this source code is governed by the GPLv3
// license that can be found in the LICENSE file.

package integralimg_test

import (
	"fmt"
	"image"
	"image/draw"
	_ "image/png"
	"log"
	"os"

	"rescribe.xyz/integralimg"
)

func ExampleImage_Sum() {
	f, err := os.Open("testdata/in.png")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()
	img, _, err := image.Decode(f)
	if err != nil {
		log.Fatal(err)
	}
	b := img.Bounds()
	integral := integralimg.NewImage(b)
	draw.Draw(integral, b, img, b.Min, draw.Src)
	fmt.Printf("Sum: %d\n", integral.Sum(b))
	// Output:
	// Sum: 601340165
}

func ExampleImage_Mean() {
	f, err := os.Open("testdata/in.png")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()
	img, _, err := image.Decode(f)
	if err != nil {
		log.Fatal(err)
	}
	b := img.Bounds()
	integral := integralimg.NewImage(b)
	draw.Draw(integral, b, img, b.Min, draw.Src)
	fmt.Printf("Mean: %f\n", integral.Mean(b))
	// Output:
	// Mean: 54677.229042
}

func ExampleMeanStdDev() {
	f, err := os.Open("testdata/in.png")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()
	img, _, err := image.Decode(f)
	if err != nil {
		log.Fatal(err)
	}
	b := img.Bounds()
	integral := integralimg.NewImage(b)
	sqIntegral := integralimg.NewSqImage(b)
	draw.Draw(integral, b, img, b.Min, draw.Src)
	draw.Draw(sqIntegral, b, img, b.Min, draw.Src)
	mean, stddev := integralimg.MeanStdDev(*integral, *sqIntegral, b)
	fmt.Printf("Mean: %f, Standard Deviation: %f\n", mean, stddev)
	// Output:
	// Mean: 54677.229042, Standard Deviation: 21643.721672
}