jf-ruby-opencv 0.0.18

Sign up to get free protection for your applications and to get access to all the features.
Files changed (241) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +28 -0
  3. data/.yardopts +3 -0
  4. data/DEVELOPERS_NOTE.md +137 -0
  5. data/Gemfile +9 -0
  6. data/History.txt +5 -0
  7. data/License.txt +30 -0
  8. data/Manifest.txt +239 -0
  9. data/README.md +94 -0
  10. data/Rakefile +99 -0
  11. data/config.yml +7 -0
  12. data/examples/alpha_blend.rb +21 -0
  13. data/examples/contours/bitmap-contours-with-labels.png +0 -0
  14. data/examples/contours/bitmap-contours.png +0 -0
  15. data/examples/contours/bounding-box-detect-canny.rb +62 -0
  16. data/examples/contours/contour_retrieval_modes.rb +139 -0
  17. data/examples/contours/rotated-boxes.jpg +0 -0
  18. data/examples/convexhull.rb +47 -0
  19. data/examples/face_detect.rb +20 -0
  20. data/examples/facerec/create_csv.rb +43 -0
  21. data/examples/facerec/facerec_eigenfaces.rb +132 -0
  22. data/examples/facerec/facerec_fisherfaces.rb +131 -0
  23. data/examples/facerec/facerec_lbph.rb +116 -0
  24. data/examples/facerec/readme.md +111 -0
  25. data/examples/find_obj.rb +169 -0
  26. data/examples/houghcircle.rb +22 -0
  27. data/examples/images/box.png +0 -0
  28. data/examples/images/box_in_scene.png +0 -0
  29. data/examples/images/inpaint.png +0 -0
  30. data/examples/images/lena-256x256.jpg +0 -0
  31. data/examples/images/lena-eyes.jpg +0 -0
  32. data/examples/images/lenna-rotated.jpg +0 -0
  33. data/examples/images/lenna.jpg +0 -0
  34. data/examples/images/stuff.jpg +0 -0
  35. data/examples/images/tiffany.jpg +0 -0
  36. data/examples/inpaint.rb +57 -0
  37. data/examples/match_kdtree.rb +88 -0
  38. data/examples/match_template.rb +26 -0
  39. data/examples/paint.rb +70 -0
  40. data/examples/snake.rb +43 -0
  41. data/ext/opencv/algorithm.cpp +291 -0
  42. data/ext/opencv/algorithm.h +38 -0
  43. data/ext/opencv/curve.cpp +127 -0
  44. data/ext/opencv/curve.h +34 -0
  45. data/ext/opencv/cvavgcomp.cpp +64 -0
  46. data/ext/opencv/cvavgcomp.h +39 -0
  47. data/ext/opencv/cvbox2d.cpp +195 -0
  48. data/ext/opencv/cvbox2d.h +61 -0
  49. data/ext/opencv/cvcapture.cpp +633 -0
  50. data/ext/opencv/cvcapture.h +82 -0
  51. data/ext/opencv/cvchain.cpp +233 -0
  52. data/ext/opencv/cvchain.h +46 -0
  53. data/ext/opencv/cvcircle32f.cpp +126 -0
  54. data/ext/opencv/cvcircle32f.h +52 -0
  55. data/ext/opencv/cvconnectedcomp.cpp +156 -0
  56. data/ext/opencv/cvconnectedcomp.h +49 -0
  57. data/ext/opencv/cvcontour.cpp +384 -0
  58. data/ext/opencv/cvcontour.h +51 -0
  59. data/ext/opencv/cvcontourtree.cpp +96 -0
  60. data/ext/opencv/cvcontourtree.h +41 -0
  61. data/ext/opencv/cvconvexitydefect.cpp +92 -0
  62. data/ext/opencv/cvconvexitydefect.h +42 -0
  63. data/ext/opencv/cverror.cpp +115 -0
  64. data/ext/opencv/cverror.h +28 -0
  65. data/ext/opencv/cvfeaturetree.cpp +123 -0
  66. data/ext/opencv/cvfeaturetree.h +55 -0
  67. data/ext/opencv/cvfont.cpp +228 -0
  68. data/ext/opencv/cvfont.h +64 -0
  69. data/ext/opencv/cvhaarclassifiercascade.cpp +148 -0
  70. data/ext/opencv/cvhaarclassifiercascade.h +39 -0
  71. data/ext/opencv/cvhistogram.cpp +717 -0
  72. data/ext/opencv/cvhistogram.h +73 -0
  73. data/ext/opencv/cvhumoments.cpp +178 -0
  74. data/ext/opencv/cvhumoments.h +51 -0
  75. data/ext/opencv/cvline.cpp +159 -0
  76. data/ext/opencv/cvline.h +54 -0
  77. data/ext/opencv/cvmat.cpp +6086 -0
  78. data/ext/opencv/cvmat.h +290 -0
  79. data/ext/opencv/cvmemstorage.cpp +73 -0
  80. data/ext/opencv/cvmemstorage.h +50 -0
  81. data/ext/opencv/cvmoments.cpp +293 -0
  82. data/ext/opencv/cvmoments.h +75 -0
  83. data/ext/opencv/cvpoint.cpp +234 -0
  84. data/ext/opencv/cvpoint.h +64 -0
  85. data/ext/opencv/cvpoint2d32f.cpp +216 -0
  86. data/ext/opencv/cvpoint2d32f.h +63 -0
  87. data/ext/opencv/cvpoint3d32f.cpp +252 -0
  88. data/ext/opencv/cvpoint3d32f.h +66 -0
  89. data/ext/opencv/cvrect.cpp +338 -0
  90. data/ext/opencv/cvrect.h +79 -0
  91. data/ext/opencv/cvscalar.cpp +241 -0
  92. data/ext/opencv/cvscalar.h +71 -0
  93. data/ext/opencv/cvseq.cpp +663 -0
  94. data/ext/opencv/cvseq.h +75 -0
  95. data/ext/opencv/cvsize.cpp +227 -0
  96. data/ext/opencv/cvsize.h +65 -0
  97. data/ext/opencv/cvsize2d32f.cpp +215 -0
  98. data/ext/opencv/cvsize2d32f.h +64 -0
  99. data/ext/opencv/cvslice.cpp +126 -0
  100. data/ext/opencv/cvslice.h +61 -0
  101. data/ext/opencv/cvsurfparams.cpp +208 -0
  102. data/ext/opencv/cvsurfparams.h +58 -0
  103. data/ext/opencv/cvsurfpoint.cpp +246 -0
  104. data/ext/opencv/cvsurfpoint.h +52 -0
  105. data/ext/opencv/cvtermcriteria.cpp +198 -0
  106. data/ext/opencv/cvtermcriteria.h +71 -0
  107. data/ext/opencv/cvtwopoints.cpp +122 -0
  108. data/ext/opencv/cvtwopoints.h +51 -0
  109. data/ext/opencv/cvutils.cpp +192 -0
  110. data/ext/opencv/cvutils.h +30 -0
  111. data/ext/opencv/cvvideowriter.cpp +142 -0
  112. data/ext/opencv/cvvideowriter.h +43 -0
  113. data/ext/opencv/eigenfaces.cpp +75 -0
  114. data/ext/opencv/eigenfaces.h +30 -0
  115. data/ext/opencv/extconf.rb +77 -0
  116. data/ext/opencv/facerecognizer.cpp +219 -0
  117. data/ext/opencv/facerecognizer.h +46 -0
  118. data/ext/opencv/fisherfaces.cpp +75 -0
  119. data/ext/opencv/fisherfaces.h +30 -0
  120. data/ext/opencv/gui.cpp +71 -0
  121. data/ext/opencv/gui.h +30 -0
  122. data/ext/opencv/iplconvkernel.cpp +198 -0
  123. data/ext/opencv/iplconvkernel.h +71 -0
  124. data/ext/opencv/iplimage.cpp +651 -0
  125. data/ext/opencv/iplimage.h +73 -0
  126. data/ext/opencv/lbph.cpp +78 -0
  127. data/ext/opencv/lbph.h +30 -0
  128. data/ext/opencv/mouseevent.cpp +186 -0
  129. data/ext/opencv/mouseevent.h +56 -0
  130. data/ext/opencv/opencv.cpp +819 -0
  131. data/ext/opencv/opencv.h +408 -0
  132. data/ext/opencv/pointset.cpp +280 -0
  133. data/ext/opencv/pointset.h +68 -0
  134. data/ext/opencv/trackbar.cpp +127 -0
  135. data/ext/opencv/trackbar.h +69 -0
  136. data/ext/opencv/window.cpp +377 -0
  137. data/ext/opencv/window.h +66 -0
  138. data/images/CvMat_sobel.png +0 -0
  139. data/images/CvMat_sub_rect.png +0 -0
  140. data/images/CvSeq_relationmap.png +0 -0
  141. data/lib/opencv.rb +12 -0
  142. data/lib/opencv/psyched_yaml.rb +22 -0
  143. data/lib/opencv/version.rb +3 -0
  144. data/ruby-opencv.gemspec +44 -0
  145. data/test/eigenfaces_save.xml +7524 -0
  146. data/test/fisherfaces_save.xml +7530 -0
  147. data/test/helper.rb +167 -0
  148. data/test/lbph_save.xml +4304 -0
  149. data/test/runner.rb +30 -0
  150. data/test/samples/airplane.jpg +0 -0
  151. data/test/samples/baboon.jpg +0 -0
  152. data/test/samples/baboon200.jpg +0 -0
  153. data/test/samples/baboon200_rotated.jpg +0 -0
  154. data/test/samples/blank0.jpg +0 -0
  155. data/test/samples/blank1.jpg +0 -0
  156. data/test/samples/blank2.jpg +0 -0
  157. data/test/samples/blank3.jpg +0 -0
  158. data/test/samples/blank4.jpg +0 -0
  159. data/test/samples/blank5.jpg +0 -0
  160. data/test/samples/blank6.jpg +0 -0
  161. data/test/samples/blank7.jpg +0 -0
  162. data/test/samples/blank8.jpg +0 -0
  163. data/test/samples/blank9.jpg +0 -0
  164. data/test/samples/cat.jpg +0 -0
  165. data/test/samples/chessboard.jpg +0 -0
  166. data/test/samples/contours.jpg +0 -0
  167. data/test/samples/fruits.jpg +0 -0
  168. data/test/samples/haarcascade_frontalface_alt.xml.gz +0 -0
  169. data/test/samples/inpaint-mask.bmp +0 -0
  170. data/test/samples/lena-256x256.jpg +0 -0
  171. data/test/samples/lena-32x32.jpg +0 -0
  172. data/test/samples/lena-eyes.jpg +0 -0
  173. data/test/samples/lena-inpaint.jpg +0 -0
  174. data/test/samples/lena.jpg +0 -0
  175. data/test/samples/lines.jpg +0 -0
  176. data/test/samples/messy0.jpg +0 -0
  177. data/test/samples/messy1.jpg +0 -0
  178. data/test/samples/movie_sample.avi +0 -0
  179. data/test/samples/one_way_train_0000.jpg +0 -0
  180. data/test/samples/one_way_train_0001.jpg +0 -0
  181. data/test/samples/partially_blank0.jpg +0 -0
  182. data/test/samples/partially_blank1.jpg +0 -0
  183. data/test/samples/smooth0.jpg +0 -0
  184. data/test/samples/smooth1.jpg +0 -0
  185. data/test/samples/smooth2.jpg +0 -0
  186. data/test/samples/smooth3.jpg +0 -0
  187. data/test/samples/smooth4.jpg +0 -0
  188. data/test/samples/smooth5.jpg +0 -0
  189. data/test/samples/smooth6.jpg +0 -0
  190. data/test/samples/str-cv-rotated.jpg +0 -0
  191. data/test/samples/str-cv.jpg +0 -0
  192. data/test/samples/str-ov.jpg +0 -0
  193. data/test/samples/stuff.jpg +0 -0
  194. data/test/test_curve.rb +43 -0
  195. data/test/test_cvavgcomp.rb +24 -0
  196. data/test/test_cvbox2d.rb +76 -0
  197. data/test/test_cvcapture.rb +191 -0
  198. data/test/test_cvchain.rb +108 -0
  199. data/test/test_cvcircle32f.rb +41 -0
  200. data/test/test_cvconnectedcomp.rb +61 -0
  201. data/test/test_cvcontour.rb +171 -0
  202. data/test/test_cvcontourtree.rb +43 -0
  203. data/test/test_cverror.rb +50 -0
  204. data/test/test_cvfeaturetree.rb +65 -0
  205. data/test/test_cvfont.rb +58 -0
  206. data/test/test_cvhaarclassifiercascade.rb +63 -0
  207. data/test/test_cvhistogram.rb +271 -0
  208. data/test/test_cvhumoments.rb +83 -0
  209. data/test/test_cvline.rb +50 -0
  210. data/test/test_cvmat.rb +3036 -0
  211. data/test/test_cvmat_drawing.rb +349 -0
  212. data/test/test_cvmat_dxt.rb +150 -0
  213. data/test/test_cvmat_imageprocessing.rb +2085 -0
  214. data/test/test_cvmoments.rb +180 -0
  215. data/test/test_cvpoint.rb +75 -0
  216. data/test/test_cvpoint2d32f.rb +75 -0
  217. data/test/test_cvpoint3d32f.rb +93 -0
  218. data/test/test_cvrect.rb +144 -0
  219. data/test/test_cvscalar.rb +113 -0
  220. data/test/test_cvseq.rb +311 -0
  221. data/test/test_cvsize.rb +75 -0
  222. data/test/test_cvsize2d32f.rb +75 -0
  223. data/test/test_cvslice.rb +31 -0
  224. data/test/test_cvsurfparams.rb +57 -0
  225. data/test/test_cvsurfpoint.rb +66 -0
  226. data/test/test_cvtermcriteria.rb +56 -0
  227. data/test/test_cvtwopoints.rb +40 -0
  228. data/test/test_cvvideowriter.rb +58 -0
  229. data/test/test_eigenfaces.rb +93 -0
  230. data/test/test_fisherfaces.rb +93 -0
  231. data/test/test_iplconvkernel.rb +54 -0
  232. data/test/test_iplimage.rb +232 -0
  233. data/test/test_lbph.rb +166 -0
  234. data/test/test_mouseevent.rb +17 -0
  235. data/test/test_opencv.rb +360 -0
  236. data/test/test_pointset.rb +128 -0
  237. data/test/test_preliminary.rb +130 -0
  238. data/test/test_trackbar.rb +47 -0
  239. data/test/test_window.rb +115 -0
  240. data/yard_extension.rb +5 -0
  241. metadata +352 -0
@@ -0,0 +1,93 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- mode: ruby; coding: utf-8-unix -*-
3
+ require 'test/unit'
4
+ require 'opencv'
5
+ require 'date'
6
+ require File.expand_path(File.dirname(__FILE__)) + '/helper'
7
+
8
+ include OpenCV
9
+
10
+ # Tests for OpenCV::FisherFaces
11
+ class TestFisherFaces < OpenCVTestCase
12
+ def setup
13
+ @fisherfaces = FisherFaces.new
14
+
15
+ @fisherfaces_trained = FisherFaces.new
16
+ @images = [CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_GRAYSCALE)] * 2
17
+ @labels = [1, 2]
18
+ @fisherfaces_trained.train(@images, @labels)
19
+ end
20
+
21
+ def test_initialize
22
+ [FisherFaces.new, FisherFaces.new(1), FisherFaces.new(1, 99999)].each { |ff|
23
+ assert_equal(FisherFaces, ff.class)
24
+ }
25
+
26
+ assert_raise(TypeError) {
27
+ FisherFaces.new(DUMMY_OBJ)
28
+ }
29
+
30
+ assert_raise(TypeError) {
31
+ FisherFaces.new(1, DUMMY_OBJ)
32
+ }
33
+ end
34
+
35
+ def test_train
36
+ assert_nil(@fisherfaces.train(@images, @labels))
37
+
38
+ assert_raise(TypeError) {
39
+ @fisherfaces.train(DUMMY_OBJ, @labels)
40
+ }
41
+
42
+ assert_raise(TypeError) {
43
+ @fisherfaces.train(@images, DUMMY_OBJ)
44
+ }
45
+ end
46
+
47
+ def test_predict
48
+ predicted_label, predicted_confidence = @fisherfaces_trained.predict(@images[0])
49
+ assert_equal(@labels[0], predicted_label)
50
+ assert_in_delta(0.0, predicted_confidence, 0.01)
51
+
52
+ assert_raise(TypeError) {
53
+ @fisherfaces_trained.predict(DUMMY_OBJ)
54
+ }
55
+ end
56
+
57
+ def test_save
58
+ filename = "fisherfaces_save-#{DateTime.now.strftime('%Y%m%d%H%M%S')}.xml"
59
+ begin
60
+ @fisherfaces_trained.save(filename)
61
+ assert(File.exist? filename)
62
+ ensure
63
+ File.delete filename
64
+ end
65
+ assert_raise(TypeError) {
66
+ @fisherfaces_trained.save(DUMMY_OBJ)
67
+ }
68
+ end
69
+
70
+ def test_load
71
+ assert_nothing_raised {
72
+ @fisherfaces.load('fisherfaces_save.xml')
73
+ }
74
+ assert_raise(TypeError) {
75
+ @fisherfaces.load(DUMMY_OBJ)
76
+ }
77
+ end
78
+
79
+ def test_name
80
+ assert_equal('FaceRecognizer.Fisherfaces', @fisherfaces.name)
81
+ end
82
+
83
+ def test_get_mat
84
+ mat = @fisherfaces_trained.get_mat('eigenvalues')
85
+ assert_not_nil(mat)
86
+ assert_equal(CvMat, mat.class)
87
+
88
+ assert_raise(TypeError) {
89
+ @fisherfaces_trained.get_mat(DUMMY_OBJ)
90
+ }
91
+ end
92
+ end
93
+
@@ -0,0 +1,54 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- mode: ruby; coding: utf-8 -*-
3
+ require 'test/unit'
4
+ require 'opencv'
5
+ require File.expand_path(File.dirname(__FILE__)) + '/helper'
6
+
7
+ include OpenCV
8
+
9
+ # Tests for OpenCV::IplConvKernel
10
+ class TestIplConvKernel < OpenCVTestCase
11
+ def test_initialize
12
+ [:rect, :cross, :ellipse, CV_SHAPE_RECT, CV_SHAPE_CROSS, CV_SHAPE_ELLIPSE].each { |shape|
13
+ kernel = IplConvKernel.new(5, 5, 2, 2, shape)
14
+ assert_not_nil(kernel)
15
+ }
16
+
17
+ values = [1] * 25
18
+ [:custom, CV_SHAPE_CUSTOM].each { |shape|
19
+ kernel = IplConvKernel.new(5, 5, 2, 2, shape, values)
20
+ assert_not_nil(kernel)
21
+ }
22
+
23
+ [:custom, CV_SHAPE_CUSTOM].each { |shape|
24
+ assert_raise(ArgumentError) {
25
+ IplConvKernel.new(5, 5, 2, 2, shape)
26
+ }
27
+ }
28
+
29
+ assert_raise(TypeError) {
30
+ IplConvKernel.new(5, 5, 2, 2, :foobar)
31
+ }
32
+ end
33
+
34
+ def test_size
35
+ kernel = IplConvKernel.new(5, 4, 2, 2, :rect)
36
+ size = kernel.size
37
+ assert_equal(5, size.width)
38
+ assert_equal(4, size.height)
39
+
40
+ assert_equal(5, kernel.cols)
41
+ assert_equal(4, kernel.rows)
42
+ end
43
+
44
+ def test_anchor
45
+ kernel = IplConvKernel.new(5, 4, 3, 2, :rect)
46
+ a = kernel.anchor
47
+ assert_equal(3, a.x)
48
+ assert_equal(2, a.y)
49
+
50
+ assert_equal(3, kernel.anchor_x)
51
+ assert_equal(2, kernel.anchor_y)
52
+ end
53
+ end
54
+
@@ -0,0 +1,232 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- mode: ruby; coding: utf-8 -*-
3
+ require 'test/unit'
4
+ require 'opencv'
5
+ require File.expand_path(File.dirname(__FILE__)) + '/helper'
6
+
7
+ include OpenCV
8
+
9
+ # Tests for OpenCV::IplImage
10
+ class TestIplImage < OpenCVTestCase
11
+ def should_classify_images_as(filename, classification)
12
+ assert_equal(OpenCV::IplImage::load(filename, OpenCV::CV_LOAD_IMAGE_GRAYSCALE).smoothness[0], classification)
13
+ end
14
+
15
+ def test_initialize
16
+ img = IplImage.new(10, 20)
17
+ assert_equal(10, img.width)
18
+ assert_equal(20, img.height)
19
+ assert_equal(:cv8u, img.depth)
20
+ assert_equal(3, img.channel)
21
+
22
+ img = IplImage.new(30, 40, :cv32f, 1)
23
+ assert_equal(30, img.width)
24
+ assert_equal(40, img.height)
25
+ assert_equal(:cv32f, img.depth)
26
+ assert_equal(1, img.channel)
27
+ end
28
+
29
+ def test_load
30
+ img1 = IplImage.load(FILENAME_CAT)
31
+ assert_equal(IplImage, img1.class)
32
+ assert_equal(375, img1.width)
33
+ assert_equal(500, img1.height)
34
+ assert_equal(:cv8u, img1.depth)
35
+ assert_equal(3, img1.channel)
36
+
37
+ img2 = IplImage.load(FILENAME_CAT, CV_LOAD_IMAGE_GRAYSCALE)
38
+ assert_equal(IplImage, img2.class)
39
+ assert_equal(375, img2.width)
40
+ assert_equal(500, img2.height)
41
+ assert_equal(:cv8u, img2.depth)
42
+ assert_equal(1, img2.channel)
43
+
44
+ img3 = IplImage.load(FILENAME_CAT, CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR)
45
+ assert_equal(IplImage, img3.class)
46
+ assert_equal(375, img3.width)
47
+ assert_equal(500, img3.height)
48
+ assert_equal(:cv8u, img3.depth)
49
+ assert_equal(3, img3.channel)
50
+
51
+ assert_raise(ArgumentError) {
52
+ IplImage.load
53
+ }
54
+ assert_raise(TypeError) {
55
+ IplImage.load(123)
56
+ }
57
+ assert_raise(TypeError) {
58
+ IplImage.load(FILENAME_CAT, 'foobar')
59
+ }
60
+ assert_raise(StandardError) {
61
+ IplImage.load('file/does/not/exist')
62
+ }
63
+
64
+ # Uncomment the following lines to show the results
65
+ # snap img1, img2, img3
66
+ end
67
+
68
+ def test_decode
69
+ data = nil
70
+ open(FILENAME_CAT, 'rb') { |f|
71
+ data = f.read
72
+ }
73
+ data_ary = data.unpack("c*")
74
+ data_mat = CvMat.new(1, data_ary.size).set_data(data_ary)
75
+ expected = IplImage.load(FILENAME_CAT)
76
+
77
+ img1 = IplImage.decode(data)
78
+ img2 = IplImage.decode(data_ary)
79
+ img3 = IplImage.decode(data_mat)
80
+ img4 = IplImage.decode(data, CV_LOAD_IMAGE_COLOR)
81
+ img5 = IplImage.decode(data_ary, CV_LOAD_IMAGE_COLOR)
82
+ img6 = IplImage.decode(data_mat, CV_LOAD_IMAGE_COLOR)
83
+
84
+ [img1, img2, img3, img4, img5, img6].each { |img|
85
+ assert_equal(IplImage, img.class)
86
+ assert_equal(expected.rows, img.rows)
87
+ assert_equal(expected.cols, img.cols)
88
+ assert_equal(expected.channel, img.channel)
89
+ }
90
+
91
+ expected_c1 = IplImage.load(FILENAME_CAT, CV_LOAD_IMAGE_GRAYSCALE)
92
+ img1c1 = IplImage.decode(data, CV_LOAD_IMAGE_GRAYSCALE)
93
+ img2c1 = IplImage.decode(data_ary, CV_LOAD_IMAGE_GRAYSCALE)
94
+ img3c1 = IplImage.decode(data_mat, CV_LOAD_IMAGE_GRAYSCALE)
95
+
96
+ [img1c1, img2c1, img3c1].each { |img|
97
+ assert_equal(IplImage, img.class)
98
+ assert_equal(expected_c1.rows, img.rows)
99
+ assert_equal(expected_c1.cols, img.cols)
100
+ assert_equal(expected_c1.channel, img.channel)
101
+ }
102
+
103
+ assert_raise(TypeError) {
104
+ IplImage.decode(DUMMY_OBJ)
105
+ }
106
+ assert_raise(TypeError) {
107
+ IplImage.decode(data, DUMMY_OBJ)
108
+ }
109
+
110
+ # Uncomment the following line to show the result images
111
+ # snap img1, img2, img3
112
+ end
113
+
114
+ def test_roi
115
+ img = IplImage.new(20, 30)
116
+ rect = img.roi
117
+ assert_equal(0, rect.x)
118
+ assert_equal(0, rect.y)
119
+ assert_equal(img.width, rect.width)
120
+ assert_equal(img.height, rect.height)
121
+
122
+ img.set_roi(CvRect.new(2, 3, 10, 20))
123
+ rect = img.roi
124
+ assert_equal(2, rect.x)
125
+ assert_equal(3, rect.y)
126
+ assert_equal(10, rect.width)
127
+ assert_equal(20, rect.height)
128
+
129
+ img.reset_roi
130
+ rect = img.roi
131
+ assert_equal(0, rect.x)
132
+ assert_equal(0, rect.y)
133
+ assert_equal(img.width, rect.width)
134
+ assert_equal(img.height, rect.height)
135
+
136
+ img.set_roi(CvRect.new(1, 2, 5, 6)) {|image|
137
+ rect = image.roi
138
+ assert_equal(1, rect.x)
139
+ assert_equal(2, rect.y)
140
+ assert_equal(5, rect.width)
141
+ assert_equal(6, rect.height)
142
+ }
143
+ rect = img.roi
144
+ assert_equal(0, rect.x)
145
+ assert_equal(0, rect.y)
146
+ assert_equal(img.width, rect.width)
147
+ assert_equal(img.height, rect.height)
148
+
149
+ # Alias
150
+ img.roi = CvRect.new(4, 5, 11, 12)
151
+ rect = img.roi
152
+ assert_equal(4, rect.x)
153
+ assert_equal(5, rect.y)
154
+ assert_equal(11, rect.width)
155
+ assert_equal(12, rect.height)
156
+ end
157
+
158
+ def test_coi
159
+ img = IplImage.new(20, 30)
160
+ assert_equal(0, img.coi)
161
+
162
+ img.set_coi(1)
163
+ assert_equal(1, img.coi)
164
+
165
+ img.reset_coi
166
+ assert_equal(0, img.coi)
167
+
168
+ img.set_coi(2) {|image|
169
+ assert_equal(2, image.coi)
170
+ }
171
+ assert_equal(0, img.coi)
172
+
173
+ # Alias
174
+ img.coi = 1
175
+ assert_equal(1, img.coi)
176
+ end
177
+
178
+ def test_smoothness
179
+ asset_path = File.join(File.dirname(__FILE__), 'samples')
180
+
181
+ for image in Array.new(7) { |e| e = File.join(asset_path, "smooth%d.jpg") % e } do
182
+ should_classify_images_as image, :smooth
183
+ end
184
+
185
+ for image in Array.new(2) { |e| e = File.join(asset_path, "messy%d.jpg") % e } do
186
+ should_classify_images_as image, :messy
187
+ end
188
+
189
+ for image in Array.new(10) { |e| e = File.join(asset_path, "blank%d.jpg") % e } do
190
+ should_classify_images_as image, :blank
191
+ end
192
+
193
+ for image in Array.new(2) { |e| e = File.join(asset_path, "partially_blank%d.jpg") % e } do
194
+ should_classify_images_as image, :blank
195
+ end
196
+ end
197
+
198
+ def test_pyr_segmentation
199
+ img0 = IplImage.load(FILENAME_CAT, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH)
200
+ img0.set_roi(CvRect.new(0, 0, 256, 512))
201
+ img1, seq1 = img0.pyr_segmentation(2, 255, 50)
202
+ assert_equal('963b26f51b14f175fbbf128e9b9e979f', hash_img(img1))
203
+ assert_equal(11, seq1.total)
204
+
205
+ assert_raise(CvStsAssert) {
206
+ img0.pyr_segmentation(-1, 255, 50)
207
+ }
208
+ assert_raise(CvStsAssert) {
209
+ img0.pyr_segmentation(1000, 255, 50)
210
+ }
211
+ assert_raise(CvStsAssert) {
212
+ img0.pyr_segmentation(4, -1, 50)
213
+ }
214
+ assert_raise(CvStsAssert) {
215
+ img0.pyr_segmentation(4, 255, -1)
216
+ }
217
+ assert_raise(TypeError) {
218
+ img0.pyr_segmentation(DUMMY_OBJ, 255, 50)
219
+ }
220
+ assert_raise(TypeError) {
221
+ img0.pyr_segmentation(4, DUMMY_OBJ, 50)
222
+ }
223
+ assert_raise(TypeError) {
224
+ img0.pyr_segmentation(4, 255, DUMMY_OBJ)
225
+ }
226
+ assert_raise(CvBadDepth) {
227
+ IplImage.new(10, 10, :cv32f, 2).pyr_segmentation(4, 255, 50)
228
+ }
229
+ end
230
+ end
231
+
232
+
@@ -0,0 +1,166 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- mode: ruby; coding: utf-8-unix -*-
3
+ require 'test/unit'
4
+ require 'opencv'
5
+ require 'date'
6
+ require File.expand_path(File.dirname(__FILE__)) + '/helper'
7
+
8
+ include OpenCV
9
+
10
+ # Tests for OpenCV::LBPH
11
+ class TestLBPH < OpenCVTestCase
12
+ def setup
13
+ @lbph = LBPH.new
14
+
15
+ @lbph_trained = LBPH.new
16
+ @lbph_update = LBPH.new
17
+ @images = [CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_GRAYSCALE)] * 2
18
+ @labels = [1, 2]
19
+ @lbph_trained.train(@images, @labels)
20
+ end
21
+
22
+ def test_initialize
23
+ [LBPH.new, LBPH.new(1), LBPH.new(1, 2, 3, 4, 5.0)].each { |lbph|
24
+ assert_equal(LBPH, lbph.class)
25
+ }
26
+
27
+ assert_raise(TypeError) {
28
+ LBPH.new(DUMMY_OBJ)
29
+ }
30
+ assert_raise(TypeError) {
31
+ LBPH.new(1, DUMMY_OBJ)
32
+ }
33
+ assert_raise(TypeError) {
34
+ LBPH.new(1, 2, DUMMY_OBJ)
35
+ }
36
+ assert_raise(TypeError) {
37
+ LBPH.new(1, 2, 3, DUMMY_OBJ)
38
+ }
39
+ assert_raise(TypeError) {
40
+ LBPH.new(1, 2, 3, 4, DUMMY_OBJ)
41
+ }
42
+ end
43
+
44
+ def test_train
45
+ assert_nil(@lbph.train(@images, @labels))
46
+
47
+ assert_raise(TypeError) {
48
+ @lbph.train(DUMMY_OBJ, @labels)
49
+ }
50
+
51
+ assert_raise(TypeError) {
52
+ @lbph.train(@images, DUMMY_OBJ)
53
+ }
54
+ end
55
+
56
+ def test_update
57
+ assert_nil(@lbph_update.train([@images[0]], [@labels[0]]))
58
+ assert_nil(@lbph_update.update([@images[1]], [@labels[1]]))
59
+
60
+ assert_raise(TypeError) {
61
+ @lbph_update.update(DUMMY_OBJ, @labels)
62
+ }
63
+
64
+ assert_raise(TypeError) {
65
+ @lbph_update.update(@images, DUMMY_OBJ)
66
+ }
67
+ end
68
+
69
+ def test_predict
70
+ predicted_label, predicted_confidence = @lbph_trained.predict(@images[0])
71
+ assert_equal(@labels[0], predicted_label)
72
+ assert_in_delta(0.0, predicted_confidence, 0.01)
73
+
74
+ assert_raise(TypeError) {
75
+ @lbph_trained.predict(DUMMY_OBJ)
76
+ }
77
+ end
78
+
79
+ def test_save
80
+ filename = "lbph_save-#{DateTime.now.strftime('%Y%m%d%H%M%S')}.xml"
81
+ begin
82
+ @lbph_trained.save(filename)
83
+ assert(File.exist? filename)
84
+ ensure
85
+ File.delete filename
86
+ end
87
+ assert_raise(TypeError) {
88
+ @lbph_trained.save(DUMMY_OBJ)
89
+ }
90
+ end
91
+
92
+ def test_load
93
+ assert_nothing_raised {
94
+ @lbph.load('lbph_save.xml')
95
+ }
96
+ assert_raise(TypeError) {
97
+ @lbph.load(DUMMY_OBJ)
98
+ }
99
+ end
100
+
101
+ def test_name
102
+ assert_equal('FaceRecognizer.LBPH', @lbph.name)
103
+ end
104
+
105
+ def test_get_int
106
+ assert_equal(1, @lbph.get_int('radius'))
107
+ assert_equal(8, @lbph.get_int('neighbors'))
108
+ assert_equal(8, @lbph.get_int('grid_x'))
109
+ assert_equal(8, @lbph.get_int('grid_y'))
110
+
111
+ assert_raise(TypeError) {
112
+ @lbph.get_int(DUMMY_OBJ)
113
+ }
114
+ end
115
+
116
+ def test_get_double
117
+ assert_equal(Float::MAX, @lbph.get_double('threshold'))
118
+
119
+ assert_raise(TypeError) {
120
+ @lbph.get_double(DUMMY_OBJ)
121
+ }
122
+ end
123
+
124
+ def test_get_matvector
125
+ histgrams = @lbph_trained.get_matvector('histograms')
126
+ assert_equal(Array, histgrams.class)
127
+ assert_equal(2, histgrams.size)
128
+ assert_equal(CvMat, histgrams[0].class)
129
+
130
+ assert_raise(TypeError) {
131
+ @lbph.get_matvector(DUMMY_OBJ)
132
+ }
133
+ end
134
+
135
+ def test_set_int
136
+ @lbph.set_int('radius', 2)
137
+ @lbph.set_int('neighbors', 3)
138
+ @lbph.set_int('grid_x', 4)
139
+ @lbph.set_int('grid_y', 5)
140
+
141
+ assert_equal(2, @lbph.get_int('radius'))
142
+ assert_equal(3, @lbph.get_int('neighbors'))
143
+ assert_equal(4, @lbph.get_int('grid_x'))
144
+ assert_equal(5, @lbph.get_int('grid_y'))
145
+
146
+ assert_raise(TypeError) {
147
+ @lbph.set_int(DUMMY_OBJ, 1)
148
+ }
149
+ assert_raise(TypeError) {
150
+ @lbph.set_int('radius', DUMMY_OBJ)
151
+ }
152
+ end
153
+
154
+ def test_set_double
155
+ @lbph.set_double('threshold', 1.0)
156
+ assert_in_delta(1.0, @lbph.get_double('threshold'), 0.001)
157
+
158
+ assert_raise(TypeError) {
159
+ @lbph.set_double(DUMMY_OBJ, 1.0)
160
+ }
161
+ assert_raise(TypeError) {
162
+ @lbph.set_double('threshold', DUMMY_OBJ)
163
+ }
164
+ end
165
+ end
166
+