-
-
Notifications
You must be signed in to change notification settings - Fork 384
/
carver_test.go
433 lines (356 loc) · 11.5 KB
/
carver_test.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package caire
import (
"image"
"image/color"
"image/draw"
"os"
"path/filepath"
"testing"
"github.com/esimov/caire/utils"
pigo "github.com/esimov/pigo/core"
"github.com/stretchr/testify/assert"
)
const (
imgWidth = 10
imgHeight = 10
)
var p *Processor
func init() {
p = &Processor{
NewWidth: imgWidth,
NewHeight: imgHeight,
BlurRadius: 1,
SobelThreshold: 4,
Percentage: false,
Square: false,
Debug: false,
}
}
func TestCarver_EnergySeamShouldNotBeDetected(t *testing.T) {
assert := assert.New(t)
var seams [][]Seam
var totalEnergySeams int
img := image.NewNRGBA(image.Rect(0, 0, imgWidth, imgHeight))
dx, dy := img.Bounds().Dx(), img.Bounds().Dy()
var c = NewCarver(dx, dy)
for x := 0; x < imgWidth; x++ {
width, height := img.Bounds().Max.X, img.Bounds().Max.Y
c = NewCarver(width, height)
c.ComputeSeams(p, img)
les := c.FindLowestEnergySeams(p)
seams = append(seams, les)
}
for i := 0; i < len(seams); i++ {
for s := 0; s < len(seams[i]); s++ {
totalEnergySeams += seams[i][s].X
}
}
assert.Equal(0, totalEnergySeams)
}
func TestCarver_DetectHorizontalEnergySeam(t *testing.T) {
assert := assert.New(t)
var seams [][]Seam
var totalEnergySeams int
img := image.NewNRGBA(image.Rect(0, 0, imgWidth, imgHeight))
draw.Draw(img, img.Bounds(), &image.Uniform{image.White}, image.Point{}, draw.Src)
// Replace the pixel colors in a single row from 0xff to 0xdd. 5 is an arbitrary value.
// The seam detector should recognize that line as being of low energy density
// and should perform the seam computation process.
// This way we'll make sure, that the seam detector correctly detects one and only one line.
dx, dy := img.Bounds().Dx(), img.Bounds().Dy()
for x := 0; x < dx; x++ {
img.Pix[(5*dx+x)*4+0] = 0xdd
img.Pix[(5*dx+x)*4+1] = 0xdd
img.Pix[(5*dx+x)*4+2] = 0xdd
img.Pix[(5*dx+x)*4+3] = 0xdd
}
var c = NewCarver(dx, dy)
for x := 0; x < imgWidth; x++ {
width, height := img.Bounds().Max.X, img.Bounds().Max.Y
c = NewCarver(width, height)
c.ComputeSeams(p, img)
les := c.FindLowestEnergySeams(p)
seams = append(seams, les)
}
for i := 0; i < len(seams); i++ {
for s := 0; s < len(seams[i]); s++ {
totalEnergySeams += seams[i][s].X
}
}
assert.Greater(totalEnergySeams, 0)
}
func TestCarver_DetectVerticalEnergySeam(t *testing.T) {
assert := assert.New(t)
var seams [][]Seam
var totalEnergySeams int
img := image.NewNRGBA(image.Rect(0, 0, imgWidth, imgHeight))
draw.Draw(img, img.Bounds(), &image.Uniform{image.White}, image.Point{}, draw.Src)
// Replace the pixel colors in a single column from 0xff to 0xdd. 5 is an arbitrary value.
// The seam detector should recognize that line as being of low energy density
// and should perform the seam computation process.
// This way we'll make sure, that the seam detector correctly detects one and only one line.
dx, dy := img.Bounds().Dx(), img.Bounds().Dy()
for y := 0; y < dy; y++ {
img.Pix[5*4+(dx*y)*4+0] = 0xdd
img.Pix[5*4+(dx*y)*4+1] = 0xdd
img.Pix[5*4+(dx*y)*4+2] = 0xdd
img.Pix[5*4+(dx*y)*4+3] = 0xff
}
var c = NewCarver(dx, dy)
img = c.RotateImage90(img)
for x := 0; x < imgHeight; x++ {
width, height := img.Bounds().Max.X, img.Bounds().Max.Y
c = NewCarver(width, height)
c.ComputeSeams(p, img)
les := c.FindLowestEnergySeams(p)
seams = append(seams, les)
}
for i := 0; i < len(seams); i++ {
for s := 0; s < len(seams[i]); s++ {
totalEnergySeams += seams[i][s].X
}
}
assert.Greater(totalEnergySeams, 0)
}
func TestCarver_RemoveSeam(t *testing.T) {
assert := assert.New(t)
img := image.NewNRGBA(image.Rect(0, 0, imgWidth, imgHeight))
bounds := img.Bounds()
// We choose to fill up the background with an uniform white color
// and afterwards we replace the colors in a single row with lower intensity ones.
draw.Draw(img, bounds, &image.Uniform{image.White}, image.Point{}, draw.Src)
origImg := img
dx, dy := img.Bounds().Dx(), img.Bounds().Dy()
// Replace the pixels in row 5 with lower intensity colors.
for x := 0; x < dx; x++ {
img.Set(x, 5, color.RGBA{R: 0xdd, G: 0xdd, B: 0xdd, A: 0xff})
}
c := NewCarver(dx, dy)
c.ComputeSeams(p, img)
seams := c.FindLowestEnergySeams(p)
img = c.RemoveSeam(img, seams, false)
isEq := true
// The test should pass if the detector correctly finds the row which pixel values are of lower intensity.
for x := 0; x < dx; x++ {
for y := 0; y < dy; y++ {
// In case the seam detector correctly recognize the modified line as of low importance
// it should remove it, which means the new image width should be 1px less then the original image.
r0, g0, b0, _ := origImg.At(x, y).RGBA()
r1, g1, b1, _ := img.At(x, y).RGBA()
if r0>>8 != r1>>8 && g0>>8 != g1>>8 && b0>>8 != b1>>8 {
isEq = false
}
}
}
assert.False(isEq)
}
func TestCarver_AddSeam(t *testing.T) {
assert := assert.New(t)
img := image.NewNRGBA(image.Rect(0, 0, imgWidth, imgHeight))
bounds := img.Bounds()
// We choose to fill up the background with an uniform white color
// Afterwards we'll replace the colors in a single row with lower intensity ones.
draw.Draw(img, bounds, &image.Uniform{image.White}, image.Point{}, draw.Src)
origImg := img
dx, dy := img.Bounds().Dx(), img.Bounds().Dy()
// Replace the pixels in row 5 with lower intensity colors.
for x := 0; x < dx; x++ {
img.Set(x, 5, color.RGBA{R: 0xdd, G: 0xdd, B: 0xdd, A: 0xff})
}
c := NewCarver(dx, dy)
c.ComputeSeams(p, img)
seams := c.FindLowestEnergySeams(p)
img = c.AddSeam(img, seams, false)
dx, dy = img.Bounds().Dx(), img.Bounds().Dy()
isEq := true
// The test should pass if the detector correctly finds the row which has lower intensity colors.
for x := 0; x < dx; x++ {
for y := 0; y < dy; y++ {
r0, g0, b0, _ := origImg.At(x, y).RGBA()
r1, g1, b1, _ := img.At(x, y).RGBA()
if r0>>8 != r1>>8 && g0>>8 != g1>>8 && b0>>8 != b1>>8 {
isEq = false
}
}
}
assert.False(isEq)
}
func TestCarver_ComputeSeams(t *testing.T) {
assert := assert.New(t)
img := image.NewNRGBA(image.Rect(0, 0, imgWidth, imgHeight))
// We choose to fill up the background with an uniform white color
// Afterwards we'll replace the colors in a single row with lower intensity ones.
dx, dy := img.Bounds().Dx(), img.Bounds().Dy()
// Replace the pixels in row 5 with lower intensity colors.
for x := 0; x < dx; x++ {
img.Pix[(5*dx+x)*4+0] = 0xdd
img.Pix[(5*dx+x)*4+1] = 0xdd
img.Pix[(5*dx+x)*4+2] = 0xdd
img.Pix[(5*dx+x)*4+3] = 0xdd
}
c := NewCarver(dx, dy)
c.ComputeSeams(p, img)
otherThenZero := findNonZeroValue(c.Points)
assert.True(otherThenZero)
}
func TestCarver_ShouldDetectFace(t *testing.T) {
assert := assert.New(t)
p.FaceDetect = true
sampleImg := filepath.Join("./testdata", "sample.jpg")
f, err := os.Open(sampleImg)
if err != nil {
t.Fatalf("could not load sample image: %v", err)
}
defer f.Close()
p.FaceDetector, err = p.FaceDetector.Unpack(cascadeFile)
if err != nil {
t.Fatalf("error unpacking the cascade file: %v", err)
}
src, _, err := image.Decode(f)
if err != nil {
t.Fatalf("error decoding image: %v", err)
}
img := p.imgToNRGBA(src)
dx, dy := img.Bounds().Max.X, img.Bounds().Max.Y
c := NewCarver(dx, dy)
// Transform the image to a pixel array.
pixels := c.rgbToGrayscale(img)
cParams := pigo.CascadeParams{
MinSize: 100,
MaxSize: utils.Max(dx, dy),
ShiftFactor: 0.1,
ScaleFactor: 1.1,
ImageParams: pigo.ImageParams{
Pixels: pixels,
Rows: dy,
Cols: dx,
Dim: dx,
},
}
// Run the classifier over the obtained leaf nodes and return the detection results.
// The result contains quadruplets representing the row, column, scale and detection score.
faces := p.FaceDetector.RunCascade(cParams, p.FaceAngle)
// Calculate the intersection over union (IoU) of two clusters.
faces = p.FaceDetector.ClusterDetections(faces, 0.2)
assert.Equal(1, len(faces))
}
func TestCarver_ShouldNotRemoveFaceZone(t *testing.T) {
p.FaceDetect = true
p.BlurRadius = 10
sampleImg := filepath.Join("./testdata", "sample.jpg")
f, err := os.Open(sampleImg)
if err != nil {
t.Fatalf("could not load sample image: %v", err)
}
defer f.Close()
p.FaceDetector, err = p.FaceDetector.Unpack(cascadeFile)
if err != nil {
t.Fatalf("error unpacking the cascade file: %v", err)
}
src, _, err := image.Decode(f)
if err != nil {
t.Fatalf("error decoding image: %v", err)
}
img := p.imgToNRGBA(src)
dx, dy := img.Bounds().Max.X, img.Bounds().Max.Y
c := NewCarver(dx, dy)
// Transform the image to a pixel array.
pixels := c.rgbToGrayscale(img)
sobel := c.SobelDetector(img, float64(p.SobelThreshold))
img = c.StackBlur(sobel, uint32(p.BlurRadius))
cParams := pigo.CascadeParams{
MinSize: 100,
MaxSize: utils.Max(dx, dy),
ShiftFactor: 0.1,
ScaleFactor: 1.1,
ImageParams: pigo.ImageParams{
Pixels: pixels,
Rows: dy,
Cols: dx,
Dim: dx,
},
}
// Run the classifier over the obtained leaf nodes and return the detection results.
// The result contains quadruplets representing the row, column, scale and detection score.
faces := p.FaceDetector.RunCascade(cParams, p.FaceAngle)
// Calculate the intersection over union (IoU) of two clusters.
faces = p.FaceDetector.ClusterDetections(faces, 0.2)
// Range over all the detected faces and draw a white rectangle mask over each of them.
// We need to trick the sobel detector to consider them as important image parts.
var rect image.Rectangle
for _, face := range faces {
if face.Q > 5.0 {
rect = image.Rect(
face.Col-face.Scale/2,
face.Row-face.Scale/2,
face.Col+face.Scale/2,
face.Row+face.Scale/2,
)
draw.Draw(sobel, rect, &image.Uniform{image.White}, image.Point{}, draw.Src)
}
}
c.ComputeSeams(p, img)
seams := c.FindLowestEnergySeams(p)
for _, seam := range seams {
if seam.X >= rect.Min.X && seam.X <= rect.Max.X {
t.Errorf("Carver shouldn't remove seams from face zone")
break
}
}
}
func TestCarver_ShouldNotResizeWithFaceDistorsion(t *testing.T) {
p.FaceDetect = true
p.BlurRadius = 10
p.NewHeight = 200
sampleImg := filepath.Join("./testdata", "sample.jpg")
f, err := os.Open(sampleImg)
if err != nil {
t.Fatalf("could not load sample image: %v", err)
}
defer f.Close()
p.FaceDetector, err = p.FaceDetector.Unpack(cascadeFile)
if err != nil {
t.Fatalf("error unpacking the cascade file: %v", err)
}
src, _, err := image.Decode(f)
if err != nil {
t.Fatalf("error decoding image: %v", err)
}
img := p.imgToNRGBA(src)
dx, dy := img.Bounds().Max.X, img.Bounds().Max.Y
c := NewCarver(dx, dy)
// Transform the image to a pixel array.
pixels := c.rgbToGrayscale(img)
cParams := pigo.CascadeParams{
MinSize: 100,
MaxSize: utils.Max(dx, dy),
ShiftFactor: 0.1,
ScaleFactor: 1.1,
ImageParams: pigo.ImageParams{
Pixels: pixels,
Rows: dy,
Cols: dx,
Dim: dx,
},
}
// Run the classifier over the obtained leaf nodes and return the detection results.
// The result contains quadruplets representing the row, column, scale and detection score.
faces := p.FaceDetector.RunCascade(cParams, p.FaceAngle)
// Calculate the intersection over union (IoU) of two clusters.
faces = p.FaceDetector.ClusterDetections(faces, 0.2)
for _, face := range faces {
if p.NewHeight < face.Scale {
t.Errorf("Should not resize image without face deformation.")
}
}
}
// findNonZeroValue utility function to check if the slice contains values other then zeros.
func findNonZeroValue(points []float64) bool {
var found = false
for i := 0; i < len(points); i++ {
if points[i] != 0 {
found = true
}
}
return found
}