/
common.go
66 lines (61 loc) · 1.38 KB
/
common.go
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
package imgproc
import (
"image"
"math"
"sort"
)
// Mean ...
func Mean(img *image.Gray) (float64, error) {
if img == nil {
return 0, ErrImageIsNil
}
bounds := img.Bounds()
width, height := getSize(img)
totalPixels := float64(width * height)
if totalPixels == 0 {
return 0, nil
}
var sum float64
for x := bounds.Min.X; x < bounds.Max.X; x++ {
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
pixel := img.GrayAt(x, y).Y
sum += float64(pixel)
}
}
return sum / totalPixels, nil
}
// Median ...
func Median(img *image.Gray) (float64, error) {
if img == nil {
return 0, ErrImageIsNil
}
bounds := img.Bounds()
width, height := getSize(img)
totalPixels := width * height
if totalPixels == 0 {
return 0, nil
}
pixels := make([]int, totalPixels)
count := 0
for x := bounds.Min.X; x < bounds.Max.X; x++ {
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
pixels[count] = int(img.GrayAt(x, y).Y)
count++
}
}
sort.Ints(pixels)
if totalPixels%2 == 0 {
return float64(pixels[totalPixels/2-1]+pixels[totalPixels/2+1]) / 2, nil
}
return float64(pixels[totalPixels/2]), nil
}
func getSize(img image.Image) (int, int) {
bounds := img.Bounds()
width := bounds.Max.X - bounds.Min.X
height := bounds.Max.Y - bounds.Min.Y
return width, height
}
// cvRound rounds floating-point number to the nearest integer
func cvRound(value float64) int {
return int(math.Round(value))
}