-
Notifications
You must be signed in to change notification settings - Fork 2
/
image.go
232 lines (199 loc) · 5.69 KB
/
image.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package haar
import (
"image"
"math"
)
// An IntegralImage is a grayscale image optimized for
// Haar-like feature computatiod.
type IntegralImage interface {
// Width returns the width of the image in pixels.
Width() int
// Height returns the height of the image in pixels.
Height() int
// IntegralAt returns the integral of all the pixels
// above and to the left of the given coordinate.
//
// Coordinates start at 0 and the point (0,0) refers
// to the top-left pixel of the image.
//
// The integral around the top and left parts of the
// image needn't be zero, since an image may be the
// cropped version of another image.
IntegralAt(x, y int) float64
}
// ImageIntegralImage creates an IntegralImage from an
// image.Image, turning the image into grayscale.
func ImageIntegralImage(img image.Image) IntegralImage {
bitmap := make([]float64, img.Bounds().Dx()*img.Bounds().Dy())
var idx int
for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ {
for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ {
r, g, b, _ := img.At(x, y).RGBA()
brightness := float64(r+g+b) / (3 * 0xffff)
bitmap[idx] = brightness
idx++
}
}
return BitmapIntegralImage(bitmap, img.Bounds().Dx(), img.Bounds().Dy())
}
// BitmapIntegralImage creates an IntegralImage from a
// grayscale bitmap image.
// The pixels in the bitmap should be packed going left
// to right, then top to bottom.
func BitmapIntegralImage(pixels []float64, width, height int) IntegralImage {
if len(pixels) != width*height {
panic("invalid bitmap size")
}
res := &sliceIntegralImage{
integrals: make([]float64, width*height),
width: width,
height: height,
}
var idx int
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
pixel := pixels[idx]
aboveLeft := res.IntegralAt(x, y)
left := res.IntegralAt(x, y+1)
above := res.IntegralAt(x+1, y)
res.integrals[idx] = pixel + above + left - aboveLeft
idx++
}
}
return res
}
// ScaleIntegralImage creates an IntegralImage that
// approximates a scaled version of img.
func ScaleIntegralImage(img IntegralImage, width, height int) IntegralImage {
xScale := float64(width) / float64(img.Width())
yScale := float64(height) / float64(img.Height())
return &scaledImage{
img: img,
newWidth: width,
newHeight: height,
xUnscale: 1 / xScale,
yUnscale: 1 / yScale,
areaScale: xScale * yScale,
}
}
// A DualImage stores an image in such a way that it
// can be cropped and normalized efficiently.
type DualImage struct {
// image is the underlying image, which has the
// extra property that integrals around the top
// and left edges are 0.
image IntegralImage
// squared is an IntegralImage for the image
// computed by squaring the brightness values
// in Image.
squared IntegralImage
}
// NewDualImage creates a DualImage based on the data
// in an IntegralImage.
func NewDualImage(img IntegralImage) *DualImage {
bitmap := make([]float64, img.Width()*img.Height())
squaredBmp := make([]float64, img.Width()*img.Height())
var idx int
for y := 0; y < img.Height(); y++ {
for x := 0; x < img.Width(); x++ {
brightness := img.IntegralAt(x+1, y+1) + img.IntegralAt(x, y) -
(img.IntegralAt(x, y+1) + img.IntegralAt(x+1, y))
bitmap[idx] = brightness
squaredBmp[idx] = brightness * brightness
idx++
}
}
return &DualImage{
image: BitmapIntegralImage(bitmap, img.Width(), img.Height()),
squared: BitmapIntegralImage(squaredBmp, img.Width(), img.Height()),
}
}
// Width returns the width of the underlying image.
func (d *DualImage) Width() int {
return d.image.Width()
}
// Height returns the height of the underlying image.
func (d *DualImage) Height() int {
return d.image.Height()
}
// Window returns a normalized, cropped version of the
// underlying image.
func (d *DualImage) Window(x, y, width, height int) IntegralImage {
if x < 0 || y < 0 {
panic("crop coordinates cannot be negative")
}
if x+width > d.image.Width() || y+height > d.image.Height() {
panic("crop rectangle goes out of bounds")
}
area := float64(width * height)
totalSum := d.image.IntegralAt(x+width, y+height) + d.image.IntegralAt(x, y) -
(d.image.IntegralAt(x+width, y) + d.image.IntegralAt(x, y+height))
squareSum := d.squared.IntegralAt(x+width, y+height) + d.squared.IntegralAt(x, y) -
(d.squared.IntegralAt(x+width, y) + d.squared.IntegralAt(x, y+height))
mean := totalSum / area
return &croppedImage{
img: d.image,
x: x,
y: y,
w: width,
h: height,
mean: mean,
stddev: math.Sqrt(squareSum/area - math.Pow(mean, 2)),
}
}
type sliceIntegralImage struct {
integrals []float64
width int
height int
}
func (s *sliceIntegralImage) Width() int {
return s.width
}
func (s *sliceIntegralImage) Height() int {
return s.height
}
func (s *sliceIntegralImage) IntegralAt(x, y int) float64 {
if x <= 0 || y <= 0 {
return 0
}
return s.integrals[(x-1)+s.width*(y-1)]
}
type croppedImage struct {
img IntegralImage
x int
y int
w int
h int
mean float64
stddev float64
}
func (c *croppedImage) Width() int {
return c.w
}
func (c *croppedImage) Height() int {
return c.h
}
func (c *croppedImage) IntegralAt(x, y int) float64 {
area := float64((x + c.x) * (y + c.y))
rawVal := c.img.IntegralAt(x+c.x, y+c.y)
return (rawVal - area*c.mean) / c.stddev
}
type scaledImage struct {
img IntegralImage
newWidth int
newHeight int
xUnscale float64
yUnscale float64
areaScale float64
}
func (s *scaledImage) Width() int {
return s.newWidth
}
func (s *scaledImage) Height() int {
return s.newHeight
}
func (s *scaledImage) IntegralAt(x, y int) float64 {
newX := int(float64(x)*s.xUnscale + 0.5)
newY := int(float64(y)*s.yUnscale + 0.5)
return s.img.IntegralAt(newX, newY) * s.areaScale
}