forked from sightmachine/SimpleCV
/
tests.py
2838 lines (2334 loc) · 69 KB
/
tests.py
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# /usr/bin/python
# To run this test you need python nose tools installed
# Run test just use:
# nosetest tests.py
#
# *Note: If you add additional test, please prefix the function name
# to the type of operation being performed. For instance modifying an
# image, test_image_erode(). If you are looking for lines, then
# test_detection_lines(). This makes it easier to verify visually
# that all the correct test per operation exist
import os, sys, pickle, operator
from SimpleCV import *
from nose.tools import with_setup, nottest
VISUAL_TEST = False # if TRUE we save the images - otherwise we DIFF against them - the default is False
SHOW_WARNING_TESTS = False # show that warnings are working - tests will pass but warnings are generated.
#colors
black = Color.BLACK
white = Color.WHITE
red = Color.RED
green = Color.GREEN
blue = Color.BLUE
###############
# TODO -
# Examples of how to do profiling
# Examples of how to do a single test -
# UPDATE THE VISUAL TESTS WITH EXAMPLES.
# Fix exif data
# Turn off test warnings using decorators.
# Write a use the tests doc.
#images
barcode = "../sampleimages/barcode.png"
testimage = "../sampleimages/9dots4lines.png"
testimage2 = "../sampleimages/aerospace.jpg"
whiteimage = "../sampleimages/white.png"
blackimage = "../sampleimages/black.png"
testimageclr = "../sampleimages/statue_liberty.jpg"
testbarcode = "../sampleimages/barcode.png"
testoutput = "../sampleimages/9d4l.jpg"
tmpimg = "../sampleimages/tmpimg.jpg"
greyscaleimage = "../sampleimages/greyscale.jpg"
logo = "../sampleimages/simplecv.png"
logo_inverted = "../sampleimages/simplecv_inverted.png"
ocrimage = "../sampleimages/ocr-test.png"
circles = "../sampleimages/circles.png"
webp = "../sampleimages/simplecv.webp"
#alpha masking images
topImg = "../sampleimages/RatTop.png"
bottomImg = "../sampleimages/RatBottom.png"
maskImg = "../sampleimages/RatMask.png"
alphaMaskImg = "../sampleimages/RatAlphaMask.png"
alphaSrcImg = "../sampleimages/GreenMaskSource.png"
#standards path
standard_path = "./standard/"
#Given a set of images, a path, and a tolerance do the image diff.
def imgDiffs(test_imgs,name_stem,tolerance,path):
count = len(test_imgs)
for idx in range(0,count):
lhs = test_imgs[idx].applyLayers() # this catches drawing methods
fname = standard_path+name_stem+str(idx)+".jpg"
rhs = Image(fname)
if( lhs.width == rhs.width and lhs.height == rhs.height ):
diff = (lhs-rhs)
val = np.average(diff.getNumpy())
if( val > tolerance ):
print val
return True
return False
#Save a list of images to a standard path.
def imgSaves(test_imgs, name_stem, path=standard_path):
count = len(test_imgs)
for idx in range(0,count):
fname = standard_path+name_stem+str(idx)+".jpg"
test_imgs[idx].save(fname)#,quality=95)
#perform the actual image save and image diffs.
def perform_diff(result,name_stem,tolerance=3.0,path=standard_path):
if(VISUAL_TEST): # save the correct images for a visual test
imgSaves(result,name_stem,path)
else: # otherwise we test our output against the visual test
if( imgDiffs(result,name_stem,tolerance,path) ):
assert False
else:
pass
def test_image_stretch():
img = Image(greyscaleimage)
stretched = img.stretch(100,200)
if(stretched == None):
assert False
result = [stretched]
name_stem = "test_stretch"
perform_diff(result,name_stem)
#These function names are required by nose test, please leave them as is
def setup_context():
img = Image(testimage)
def destroy_context():
img = ""
@with_setup(setup_context, destroy_context)
def test_image_loadsave():
img = Image(testimage)
img.save(testoutput)
if (os.path.isfile(testoutput)):
os.remove(testoutput)
pass
else:
assert False
def test_image_numpy_constructor():
img = Image(testimage)
grayimg = img.grayscale()
chan3_array = np.array(img.getMatrix())
chan1_array = np.array(img.getGrayscaleMatrix())
img2 = Image(chan3_array)
grayimg2 = Image(chan1_array)
if (img2[0,0] == img[0,0] and grayimg2[0,0] == grayimg[0,0]):
pass
else:
assert False
def test_image_bitmap():
img1 = Image("lenna")
img2 = Image("lenna")
img2 = img2.smooth()
result = [img1,img2]
name_stem = "test_image_bitmap"
perform_diff(result,name_stem)
# # Image Class Test
def test_image_scale():
img = Image(testimage)
thumb = img.scale(30,30)
if(thumb == None):
assert False
result = [thumb]
name_stem = "test_image_scale"
perform_diff(result,name_stem)
def test_image_copy():
img = Image(testimage2)
copy = img.copy()
if (img[1,1] != copy[1,1] or img.size() != copy.size()):
assert False
result = [copy]
name_stem = "test_image_copy"
perform_diff(result,name_stem)
pass
def test_image_getitem():
img = Image(testimage)
colors = img[1,1]
if (colors[0] == 255 and colors[1] == 255 and colors[2] == 255):
pass
else:
assert False
def test_image_getslice():
img = Image(testimage)
section = img[1:10,1:10]
if(section == None):
assert False
def test_image_setitem():
img = Image(testimage)
img[1,1] = (0, 0, 0)
newimg = Image(img.getBitmap())
colors = newimg[1,1]
if (colors[0] == 0 and colors[1] == 0 and colors[2] == 0):
pass
else:
assert False
result = [newimg]
name_stem = "test_image_setitem"
perform_diff(result,name_stem)
def test_image_setslice():
img = Image(testimage)
img[1:10,1:10] = (0,0,0) #make a black box
newimg = Image(img.getBitmap())
section = newimg[1:10,1:10]
for i in range(5):
colors = section[i,0]
if (colors[0] != 0 or colors[1] != 0 or colors[2] != 0):
assert False
pass
result = [newimg]
name_stem = "test_image_setslice"
perform_diff(result,name_stem)
def test_detection_findCorners():
img = Image(testimage2)
corners = img.findCorners(25)
corners.draw()
if (len(corners) == 0):
assert False
result = [img]
name_stem = "test_detection_findCorners"
perform_diff(result,name_stem)
def test_color_meancolor():
img = Image(testimage2)
roi = img[1:50,1:50]
r, g, b = roi.meanColor()
if (r >= 0 and r <= 255 and g >= 0 and g <= 255 and b >= 0 and b <= 255):
pass
def test_image_smooth():
img = Image(testimage2)
result = []
result.append(img.smooth())
result.append(img.smooth('bilateral', (3,3), 4, 1))
result.append(img.smooth('blur', (3, 3)))
result.append(img.smooth('median', (3, 3)))
result.append(img.smooth('gaussian', (5,5), 0))
result.append(img.smooth('bilateral', (3,3), 4, 1,grayscale=False))
result.append(img.smooth('blur', (3, 3),grayscale=True))
result.append(img.smooth('median', (3, 3),grayscale=True))
result.append(img.smooth('gaussian', (5,5), 0,grayscale=True))
name_stem = "test_image_smooth"
perform_diff(result,name_stem)
pass
def test_image_binarize():
img = Image(testimage2)
binary = img.binarize()
binary2 = img.binarize((60, 100, 200))
hist = binary.histogram(20)
hist2 = binary2.histogram(20)
result = [binary,binary2]
name_stem = "test_image_binarize"
perform_diff(result,name_stem)
if (hist[0] + hist[-1] == np.sum(hist) and hist2[0] + hist2[-1] == np.sum(hist2)):
pass
else:
assert False
def test_image_binarize_adaptive():
img = Image(testimage2)
binary = img.binarize(-1)
hist = binary.histogram(20)
result = [binary]
name_stem = "test_image_binarize_adaptive"
perform_diff(result,name_stem)
if (hist[0] + hist[-1] == np.sum(hist)):
pass
else:
assert False
def test_image_invert():
img = Image(testimage2)
clr = img[1,1]
img = img.invert()
result = [img]
name_stem = "test_image_invert"
perform_diff(result,name_stem)
if (clr[0] == (255 - img[1,1][0])):
pass
else:
assert False
def test_image_size():
img = Image(testimage2)
(width, height) = img.size()
if type(width) == int and type(height) == int and width > 0 and height > 0:
pass
else:
assert False
def test_image_drawing():
img = Image(testimageclr)
img.drawCircle((img.width/2, img.height/2), 10,thickness=3)
img.drawCircle((img.width/2, img.height/2), 15,thickness=5,color=Color.RED)
img.drawCircle((img.width/2, img.height/2), 20)
img.drawLine((5, 5), (5, 8))
img.drawLine((5, 5), (10, 10),thickness=3)
img.drawLine((0, 0), (img.width, img.height),thickness=3,color=Color.BLUE)
img.drawRectangle(20,20,10,5)
img.drawRectangle(22,22,10,5,alpha=128)
img.drawRectangle(24,24,10,15,width=-1,alpha=128)
img.drawRectangle(28,28,10,15,width=3,alpha=128)
result = [img]
name_stem = "test_image_drawing"
perform_diff(result,name_stem)
def test_image_splitchannels():
img = Image(testimageclr)
(r, g, b) = img.splitChannels(True)
(red, green, blue) = img.splitChannels()
result = [r,g,b,red,green,blue]
name_stem = "test_image_splitchannels"
perform_diff(result,name_stem)
pass
def test_image_histogram():
img = Image(testimage2)
h = img.histogram(25)
for i in h:
if type(i) != int:
assert False
pass
def test_detection_lines():
img = Image(testimage2)
lines = img.findLines()
lines.draw()
result = [img]
name_stem = "test_detection_lines"
perform_diff(result,name_stem)
if(lines == 0 or lines == None):
assert False
def test_detection_feature_measures():
img = Image(testimage2)
fs = FeatureSet()
fs.append(Corner(img, 5, 5))
fs.append(Line(img, ((2, 2), (3,3))))
bm = BlobMaker()
result = bm.extract(img)
fs.extend(result)
for f in fs:
a = f.area()
l = f.length()
c = f.meanColor()
d = f.colorDistance()
th = f.angle()
pts = f.coordinates()
dist = f.distanceFrom() #distance from center of image
fs2 = fs.sortAngle()
fs3 = fs.sortLength()
fs4 = fs.sortColorDistance()
fs5 = fs.sortArea()
fs1 = fs.sortDistance()
pass
def test_detection_blobs_appx():
img = Image("lenna")
blobs = img.findBlobs()
blobs[-1].draw(color=Color.RED)
blobs[-1].drawAppx(color=Color.BLUE)
result = [img]
img2 = Image("lenna")
blobs = img2.findBlobs(appx_level=11)
blobs[-1].draw(color=Color.RED)
blobs[-1].drawAppx(color=Color.BLUE)
result.append(img2)
name_stem = "test_detection_blobs_appx"
perform_diff(result,name_stem,5.00)
if blobs == None:
assert False
def test_detection_blobs():
img = Image(testbarcode)
blobs = img.findBlobs()
blobs.draw(color=Color.RED)
result = [img]
#TODO - WE NEED BETTER COVERAGE HERE
name_stem = "test_detection_blobs"
perform_diff(result,name_stem,5.00)
if blobs == None:
assert False
def test_detection_blobs_lazy():
img = Image("lenna")
b = img.findBlobs()
result = []
s = pickle.dumps(b[-1]) # use two otherwise it w
b2 = pickle.loads(s)
result.append(b[-1].mImg)
result.append(b[-1].mMask)
result.append(b[-1].mHullImg)
result.append(b[-1].mHullMask)
result.append(b2.mImg)
result.append(b2.mMask)
result.append(b2.mHullImg)
result.append(b2.mHullMask)
#TODO - WE NEED BETTER COVERAGE HERE
name_stem = "test_detection_blobs_lazy"
perform_diff(result,name_stem,6.00)
def test_detection_blobs_adaptive():
img = Image(testimage)
blobs = img.findBlobs(-1, threshblocksize=99)
blobs.draw(color=Color.RED)
result = [img]
name_stem = "test_detection_blobs_adaptive"
perform_diff(result,name_stem,5.00)
if blobs == None:
assert False
def test_detection_barcode():
try:
import zbar
except:
return None
img1 = Image(testimage)
img2 = Image(testbarcode)
if( SHOW_WARNING_TESTS ):
nocode = img1.findBarcode()
if nocode: #we should find no barcode in our test image
assert False
code = img2.findBarcode()
code.draw()
if code.points:
pass
result = [img1,img2]
name_stem = "test_detection_barcode"
perform_diff(result,name_stem)
else:
pass
def test_detection_x():
tmpX = Image(testimage).findLines().x()[0]
if (tmpX > 0 and Image(testimage).size()[0]):
pass
else:
assert False
def test_detection_y():
tmpY = Image(testimage).findLines().y()[0]
if (tmpY > 0 and Image(testimage).size()[0]):
pass
else:
assert False
def test_detection_area():
img = Image(testimage2)
bm = BlobMaker()
result = bm.extract(img)
area_val = result[0].area()
if(area_val > 0):
pass
else:
assert False
def test_detection_angle():
angle_val = Image(testimage).findLines().angle()[0]
def test_image():
img = Image(testimage)
if(isinstance(img, Image)):
pass
else:
assert False
def test_color_colordistance():
img = Image(blackimage)
(r,g,b) = img.splitChannels()
avg = img.meanColor()
c1 = Corner(img, 1, 1)
c2 = Corner(img, 1, 2)
if (c1.colorDistance(c2.meanColor()) != 0):
assert False
if (c1.colorDistance((0,0,0)) != 0):
assert False
if (c1.colorDistance((0,0,255)) != 255):
assert False
if (c1.colorDistance((255,255,255)) != sqrt(255**2 * 3)):
assert False
pass
def test_detection_length():
img = Image(testimage)
val = img.findLines().length()
if (val == None):
assert False
if (not isinstance(val, np.ndarray)):
assert False
if (len(val) < 0):
assert False
pass
def test_detection_sortangle():
img = Image(testimage)
val = img.findLines().sortAngle()
if(val[0].x < val[1].x):
pass
else:
assert False
def test_detection_sortarea():
img = Image(testimage)
bm = BlobMaker()
result = bm.extract(img)
val = result.sortArea()
#FIXME: Find blobs may appear to be broken. Returning type none
def test_detection_sortLength():
img = Image(testimage)
val = img.findLines().sortLength()
#FIXME: Length is being returned as euclidean type, believe we need a universal type, either Int or scvINT or something.
#def test_distanceFrom():
#def test_sortColorDistance():
#def test_sortDistance():
def test_image_add():
imgA = Image(blackimage)
imgB = Image(whiteimage)
imgC = imgA + imgB
def test_color_curve_HSL():
y = np.array([[0,0],[64,128],[192,128],[255,255]]) #These are the weights
curve = ColorCurve(y)
img = Image(testimage)
img2 = img.applyHLSCurve(curve,curve,curve)
img3 = img-img2
result = [img2,img3]
name_stem = "test_color_curve_HLS"
perform_diff(result,name_stem)
c = img3.meanColor()
if( c[0] > 2.0 or c[1] > 2.0 or c[2] > 2.0 ): #there may be a bit of roundoff error
assert False
def test_color_curve_RGB():
y = np.array([[0,0],[64,128],[192,128],[255,255]]) #These are the weights
curve = ColorCurve(y)
img = Image(testimage)
img2 = img.applyRGBCurve(curve,curve,curve)
img3 = img-img2
result = [img2,img3]
name_stem = "test_color_curve_RGB"
perform_diff(result,name_stem)
c = img3.meanColor()
if( c[0] > 1.0 or c[1] > 1.0 or c[2] > 1.0 ): #there may be a bit of roundoff error
assert False
def test_color_curve_GRAY():
y = np.array([[0,0],[64,128],[192,128],[255,255]]) #These are the weights
curve = ColorCurve(y)
img = Image(testimage)
gray = img.grayscale()
img2 = img.applyIntensityCurve(curve)
result = [img2]
name_stem = "test_color_curve_GRAY"
perform_diff(result,name_stem)
g=gray.meanColor()
i2=img2.meanColor()
if( g[0]-i2[0] > 1 ): #there may be a bit of roundoff error
assert False
def test_image_dilate():
img=Image(barcode)
img2 = img.dilate(20)
result = [img2]
name_stem = "test_image_dilate"
perform_diff(result,name_stem)
c=img2.meanColor()
if( c[0] < 254 or c[1] < 254 or c[2] < 254 ):
assert False;
def test_image_erode():
img=Image(barcode)
img2 = img.erode(100)
result = [img2]
name_stem = "test_image_erode"
perform_diff(result,name_stem)
c=img2.meanColor()
print(c)
if( c[0] > 0 or c[1] > 0 or c[2] > 0 ):
assert False;
def test_image_morph_open():
img = Image(barcode);
erode= img.erode()
dilate = erode.dilate()
result = img.morphOpen()
test = result-dilate
c=test.meanColor()
results = [result]
name_stem = "test_image_morph_open"
perform_diff(results,name_stem)
if( c[0] > 1 or c[1] > 1 or c[2] > 1 ):
assert False;
def test_image_morph_close():
img = Image(barcode)
dilate = img.dilate()
erode = dilate.erode()
result = img.morphClose()
test = result-erode
c=test.meanColor()
results = [result]
name_stem = "test_image_morph_close"
perform_diff(results,name_stem)
if( c[0] > 1 or c[1] > 1 or c[2] > 1 ):
assert False;
def test_image_morph_grad():
img = Image(barcode)
dilate = img.dilate()
erode = img.erode()
dif = dilate-erode
result = img.morphGradient()
test = result-dif
c=test.meanColor()
results = [result]
name_stem = "test_image_morph_grad"
perform_diff(results,name_stem)
if( c[0] > 1 or c[1] > 1 or c[2] > 1 ):
assert False
def test_image_rotate_fixed():
img = Image(testimage2)
img2=img.rotate(180, scale = 1)
img3=img.flipVertical()
img4=img3.flipHorizontal()
img5 = img.rotate(70)
img6 = img.rotate(70,scale=0.5)
results = [img2,img3,img4,img5,img6]
name_stem = "test_image_rotate_fixed"
perform_diff(results,name_stem)
test = img4-img2
c=test.meanColor()
print(c)
if( c[0] > 5 or c[1] > 5 or c[2] > 5 ):
assert False
def test_image_rotate_full():
img = Image(testimage2)
img2=img.rotate(180,"full",scale = 1)
results = [img2]
name_stem = "test_image_rotate_full"
perform_diff(results,name_stem)
c1=img.meanColor()
c2=img2.meanColor()
if( abs(c1[0]-c2[0]) > 5 or abs(c1[1]-c2[1]) > 5 or abs(c1[2]-c2[2]) > 5 ):
assert False
def test_image_shear_warp():
img = Image(testimage2)
dst = ((img.width/2,0),(img.width-1,img.height/2),(img.width/2,img.height-1))
s = img.shear(dst)
color = s[0,0]
if (color != (0,0,0)):
assert False
dst = ((img.width*0.05,img.height*0.03),(img.width*0.9,img.height*0.1),(img.width*0.8,img.height*0.7),(img.width*0.2,img.height*0.9))
w = img.warp(dst)
results = [s,w]
name_stem = "test_image_shear_warp"
perform_diff(results,name_stem)
color = s[0,0]
if (color != (0,0,0)):
assert False
pass
def test_image_affine():
img = Image(testimage2)
src = ((0,0),(img.width-1,0),(img.width-1,img.height-1))
dst = ((img.width/2,0),(img.width-1,img.height/2),(img.width/2,img.height-1))
aWarp = cv.CreateMat(2,3,cv.CV_32FC1)
cv.GetAffineTransform(src,dst,aWarp)
atrans = img.transformAffine(aWarp)
aWarp2 = np.array(aWarp)
atrans2 = img.transformAffine(aWarp2)
test = atrans-atrans2
c=test.meanColor()
results = [atrans,atrans2]
name_stem = "test_image_affine"
perform_diff(results,name_stem)
if( c[0] > 1 or c[1] > 1 or c[2] > 1 ):
assert False
def test_image_perspective():
img = Image(testimage2)
src = ((0,0),(img.width-1,0),(img.width-1,img.height-1),(0,img.height-1))
dst = ((img.width*0.05,img.height*0.03),(img.width*0.9,img.height*0.1),(img.width*0.8,img.height*0.7),(img.width*0.2,img.height*0.9))
pWarp = cv.CreateMat(3,3,cv.CV_32FC1)
cv.GetPerspectiveTransform(src,dst,pWarp)
ptrans = img.transformPerspective(pWarp)
pWarp2 = np.array(pWarp)
ptrans2 = img.transformPerspective(pWarp2)
test = ptrans-ptrans2
c=test.meanColor()
results = [ptrans,ptrans2]
name_stem = "test_image_perspective"
perform_diff(results,name_stem)
if( c[0] > 1 or c[1] > 1 or c[2] > 1 ):
assert False
def test_image_horz_scanline():
img = Image(logo)
sl = img.getHorzScanline(10)
if( sl.shape[0]!=img.width or sl.shape[1]!=3 ):
assert False
def test_image_vert_scanline():
img = Image(logo)
sl = img.getVertScanline(10)
if( sl.shape[0]!=img.height or sl.shape[1]!=3 ):
assert False
def test_image_horz_scanline_gray():
img = Image(logo)
sl = img.getHorzScanlineGray(10)
if( sl.shape[0]!=img.width or sl.shape[1]!=1 ):
assert False
def test_image_vert_scanline_gray():
img = Image(logo)
sl = img.getVertScanlineGray(10)
if( sl.shape[0]!=img.height or sl.shape[1]!=1 ):
assert False
def test_image_get_pixel():
img = Image(logo)
px = img.getPixel(0,0)
print(px)
if(px[0] != 0 or px[1] != 0 or px[2] != 0 ):
assert False
def test_image_get_gray_pixel():
img = Image(logo)
px = img.getGrayPixel(0,0)
if(px != 0):
assert False
def test_camera_calibration():
fakeCamera = FrameSource()
path = "../sampleimages/CalibImage"
ext = ".png"
imgs = []
for i in range(0,10):
fname = path+str(i)+ext
img = Image(fname)
imgs.append(img)
fakeCamera.calibrate(imgs)
#we're just going to check that the function doesn't puke
mat = fakeCamera.getCameraMatrix()
if( type(mat) != cv.cvmat ):
assert False
#we're also going to test load in save in the same pass
matname = "TestCalibration"
if( False == fakeCamera.saveCalibration(matname)):
assert False
if( False == fakeCamera.loadCalibration(matname)):
assert False
def test_camera_undistort():
fakeCamera = FrameSource()
fakeCamera.loadCalibration("Default")
img = Image("../sampleimages/CalibImage0.png")
img2 = fakeCamera.undistort(img)
results = [img2]
name_stem = "test_camera_undistort"
perform_diff(results,name_stem)
if( not img2 ): #right now just wait for this to return
assert False
def test_image_crop():
img = Image(logo)
x = 5
y = 6
w = 10
h = 20
crop = img.crop(x,y,w,h)
crop2 = img[x:(x+w),y:(y+h)]
crop6 = img.crop(0,0,10,10)
if( SHOW_WARNING_TESTS ):
crop7 = img.crop(0,0,-10,10)
crop8 = img.crop(-50,-50,10,10)
crop3 = img.crop(-3,-3,10,20)
crop4 = img.crop(-10,10,20,20,centered=True)
crop5 = img.crop(-10,-10,20,20)
results = [crop,crop2,crop6]
name_stem = "test_image_crop"
perform_diff(results,name_stem)
diff = crop-crop2;
c=diff.meanColor()
if( c[0] > 0 or c[1] > 0 or c[2] > 0 ):
assert False
def test_image_region_select():
img = Image(logo)
x1 = 0
y1 = 0
x2 = img.width
y2 = img.height
crop = img.regionSelect(x1,y1,x2,y2)
results = [crop]
name_stem = "test_image_region_select"
perform_diff(results,name_stem)
diff = crop-img;
c=diff.meanColor()
if( c[0] > 0 or c[1] > 0 or c[2] > 0 ):
assert False
def test_image_subtract():
imgA = Image(logo)
imgB = Image(logo_inverted)
imgC = imgA - imgB
results = [imgC]
name_stem = "test_image_subtract"
perform_diff(results,name_stem)
def test_image_negative():
imgA = Image(logo)
imgB = -imgA
results = [imgB]
name_stem = "test_image_negative"
perform_diff(results,name_stem)
def test_image_divide():
imgA = Image(logo)
imgB = Image(logo_inverted)
imgC = imgA / imgB
results = [imgC]
name_stem = "test_image_divide"
perform_diff(results,name_stem)
def test_image_and():
imgA = Image(barcode)
imgB = imgA.invert()
imgC = imgA & imgB # should be all black
results = [imgC]
name_stem = "test_image_and"
perform_diff(results,name_stem)
def test_image_or():
imgA = Image(barcode)
imgB = imgA.invert()
imgC = imgA | imgB #should be all white
results = [imgC]
name_stem = "test_image_or"
perform_diff(results,name_stem)
def test_image_edgemap():
imgA = Image(logo)
imgB = imgA._getEdgeMap()
#results = [imgB]
#name_stem = "test_image_edgemap"
#perform_diff(results,name_stem)
def test_color_colormap_build():
cm = ColorModel()
#cm.add(Image(logo))
cm.add((127,127,127))
if(cm.contains((127,127,127))):
cm.remove((127,127,127))
else:
assert False
cm.remove((0,0,0))
cm.remove((255,255,255))
cm.add((0,0,0))
cm.add([(0,0,0),(255,255,255)])
cm.add([(255,0,0),(0,255,0)])
img = cm.threshold(Image(testimage))
c=img.meanColor()
#if( c[0] > 1 or c[1] > 1 or c[2] > 1 ):
# assert False
cm.save("temp.txt")
cm2 = ColorModel()
cm2.load("temp.txt")
img = Image("logo")
img2 = cm2.threshold(img)
cm2.add((0,0,255))
img3 = cm2.threshold(img)
cm2.add((255,255,0))
cm2.add((0,255,255))
cm2.add((255,0,255))
img4 = cm2.threshold(img)
cm2.add(img)
img5 = cm2.threshold(img)
results = [img,img2,img3,img4,img5]
name_stem = "test_color_colormap_build"
perform_diff(results,name_stem)
#c=img.meanColor()
#if( c[0] > 1 or c[1] > 1 or c[2] > 1 ):