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,2085 @@
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 image processing functions of OpenCV::CvMat
10
+ class TestCvMat_imageprocessing < OpenCVTestCase
11
+ FILENAME_LENA256x256 = File.expand_path(File.dirname(__FILE__)) + '/samples/lena-256x256.jpg'
12
+ FILENAME_LENA_INPAINT = File.expand_path(File.dirname(__FILE__)) + '/samples/lena-inpaint.jpg'
13
+ FILENAME_INPAINT_MASK = File.expand_path(File.dirname(__FILE__)) + '/samples/inpaint-mask.bmp'
14
+ FILENAME_LENA32x32 = File.expand_path(File.dirname(__FILE__)) + '/samples/lena-32x32.jpg'
15
+ FILENAME_LINES = File.expand_path(File.dirname(__FILE__)) + '/samples/lines.jpg'
16
+ FILENAME_LENA_EYES = File.expand_path(File.dirname(__FILE__)) + '/samples/lena-eyes.jpg'
17
+ FILENAME_STR_CV = File.expand_path(File.dirname(__FILE__)) + '/samples/str-cv.jpg'
18
+ FILENAME_STR_OV = File.expand_path(File.dirname(__FILE__)) + '/samples/str-ov.jpg'
19
+ FILENAME_STR_CV_ROTATED = File.expand_path(File.dirname(__FILE__)) + '/samples/str-cv-rotated.jpg'
20
+
21
+ def test_sobel
22
+ mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_GRAYSCALE)
23
+
24
+ mat1 = mat0.sobel(1, 0).convert_scale_abs(:scale => 1, :shift => 0)
25
+ mat2 = mat0.sobel(0, 1).convert_scale_abs(:scale => 1, :shift => 0)
26
+ mat3 = mat0.sobel(1, 1).convert_scale_abs(:scale => 1, :shift => 0)
27
+ mat4 = mat0.sobel(1, 1, 3).convert_scale_abs(:scale => 1, :shift => 0)
28
+ mat5 = mat0.sobel(1, 1, 5).convert_scale_abs(:scale => 1, :shift => 0)
29
+
30
+ assert_equal('30a26b7287fac75bb697bc7eef6bb53a', hash_img(mat1))
31
+ assert_equal('b740afb13b556d55280fa785190ac902', hash_img(mat2))
32
+ assert_equal('36c29ca64a599e0f5633f4f3948ed858', hash_img(mat3))
33
+ assert_equal('36c29ca64a599e0f5633f4f3948ed858', hash_img(mat4))
34
+ assert_equal('30b9e8fd64e7f86c50fb67d8703628e3', hash_img(mat5))
35
+
36
+ assert_equal(:cv16s, CvMat.new(16, 16, :cv8u, 1).sobel(1, 1).depth)
37
+ assert_equal(:cv32f, CvMat.new(16, 16, :cv32f, 1).sobel(1, 1).depth)
38
+
39
+ (DEPTH.keys - [:cv8u, :cv32f]).each { |depth|
40
+ assert_raise(ArgumentError) {
41
+ CvMat.new(3, 3, depth).sobel(1, 1)
42
+ }
43
+ }
44
+
45
+ # Uncomment the following lines to view the images
46
+ # snap(['original', mat0], ['sobel(1,0)', mat1], ['sobel(0,1)', mat2],
47
+ # ['sobel(1,1)', mat3], ['sobel(1,1,3)', mat4], ['sobel(1,1,5)', mat5])
48
+
49
+ assert_raise(TypeError) {
50
+ mat0.sobel(DUMMY_OBJ, 0)
51
+ }
52
+ assert_raise(TypeError) {
53
+ mat0.sobel(1, DUMMY_OBJ)
54
+ }
55
+ assert_raise(TypeError) {
56
+ mat0.sobel(1, 0, DUMMY_OBJ)
57
+ }
58
+ end
59
+
60
+ def test_laplace
61
+ mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_GRAYSCALE)
62
+
63
+ mat1 = mat0.laplace.convert_scale_abs(:scale => 1, :shift => 0)
64
+ mat2 = mat0.laplace(3).convert_scale_abs(:scale => 1, :shift => 0)
65
+ mat3 = mat0.laplace(5).convert_scale_abs(:scale => 1, :shift => 0)
66
+
67
+ assert_equal('824f8de75bfead5d83c4226f3948ce69', hash_img(mat1))
68
+ assert_equal('824f8de75bfead5d83c4226f3948ce69', hash_img(mat2))
69
+ assert_equal('23850bb8cfe9fd1b82cd73b7b4659369', hash_img(mat3))
70
+
71
+ assert_equal(:cv16s, CvMat.new(16, 16, :cv8u, 1).laplace.depth)
72
+ assert_equal(:cv32f, CvMat.new(16, 16, :cv32f, 1).laplace.depth)
73
+
74
+ (DEPTH.keys - [:cv8u, :cv32f]).each { |depth|
75
+ assert_raise(ArgumentError) {
76
+ CvMat.new(3, 3, depth).laplace
77
+ }
78
+ }
79
+
80
+ # Uncomment the following line to view the images
81
+ # snap(['original', mat0], ['laplace', mat1], ['laplace(3)', mat2], ['laplace(5)', mat3])
82
+
83
+ assert_raise(TypeError) {
84
+ mat0.laplace(DUMMY_OBJ)
85
+ }
86
+ end
87
+
88
+ def test_canny
89
+ mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_GRAYSCALE)
90
+ mat1 = mat0.canny(50, 200)
91
+ mat2 = mat0.canny(50, 200, 3)
92
+ mat3 = mat0.canny(50, 200, 5)
93
+
94
+ assert_equal('ec3e88035bb98b5c5f1a08c8e07ab0a8', hash_img(mat1))
95
+ assert_equal('ec3e88035bb98b5c5f1a08c8e07ab0a8', hash_img(mat2))
96
+ assert_equal('1983a6d325d11eea3261462103b0dae1', hash_img(mat3))
97
+
98
+ # Uncomment the following line to view the images
99
+ # snap(['canny(50,200)', mat1], ['canny(50,200,3)', mat2], ['canny(50,200,5)', mat3])
100
+
101
+ assert_raise(TypeError) {
102
+ mat0.canny(DUMMY_OBJ, 200)
103
+ }
104
+ assert_raise(TypeError) {
105
+ mat0.canny(50, DUMMY_OBJ)
106
+ }
107
+ assert_raise(TypeError) {
108
+ mat0.canny(50, 200, DUMMY_OBJ)
109
+ }
110
+ end
111
+
112
+ def test_pre_corner_detect
113
+ mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_GRAYSCALE)
114
+ mat1 = mat0.pre_corner_detect
115
+ mat2 = mat0.pre_corner_detect(3)
116
+ mat3 = mat0.pre_corner_detect(5)
117
+
118
+ assert_in_delta(0, count_threshold(mat1, 0.1), 30)
119
+ assert_in_delta(0, count_threshold(mat2, 0.1), 30)
120
+ assert_in_delta(380, count_threshold(mat3, 0.1), 30)
121
+
122
+ # Uncomment the following lines to show the images
123
+ # snap(['original', mat0], ['pre_coner_detect', mat1],
124
+ # ['pre_coner_detect(3)', mat2], ['pre_coner_detect(5)', mat3])
125
+
126
+ assert_raise(TypeError) {
127
+ mat0.pre_corner_detect(DUMMY_OBJ)
128
+ }
129
+ end
130
+
131
+ def test_corner_eigenvv
132
+ mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_GRAYSCALE)
133
+ mat1 = mat0.corner_eigenvv(3)
134
+ mat2 = mat0.corner_eigenvv(3, 3)
135
+
136
+ assert_raise(TypeError) {
137
+ mat0.corner_eigenvv(DUMMY_OBJ)
138
+ }
139
+ assert_raise(TypeError) {
140
+ mat0.corner_eigenvv(3, DUMMY_OBJ)
141
+ }
142
+
143
+ flunk('FIXME: CvMat#corner_eigenvv is not tested yet.')
144
+ end
145
+
146
+ def test_corner_min_eigen_val
147
+ mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_GRAYSCALE)
148
+ mat1 = mat0.corner_min_eigen_val(3)
149
+ mat2 = mat0.corner_min_eigen_val(3, 3)
150
+
151
+ assert_raise(TypeError) {
152
+ mat0.corner_min_eigen_val(DUMMY_OBJ)
153
+ }
154
+ assert_raise(TypeError) {
155
+ mat0.corner_min_eigen_val(3, DUMMY_OBJ)
156
+ }
157
+
158
+ flunk('FIXME: CvMat#corner_min_eigen_val is not tested yet.')
159
+ end
160
+
161
+ def test_corner_harris
162
+ mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_GRAYSCALE)
163
+ mat1 = mat0.corner_harris(3)
164
+ mat2 = mat0.corner_harris(3, 3)
165
+ mat3 = mat0.corner_harris(3, 3, 0.04)
166
+ mat4 = mat0.corner_harris(3, 7, 0.01)
167
+
168
+ [mat1, mat2, mat3].each { |mat|
169
+ assert_equal(mat0.rows, mat.rows)
170
+ assert_equal(mat0.cols, mat.cols)
171
+ assert_in_delta(0, count_threshold(mat, 10), 10)
172
+ }
173
+ assert_equal(mat0.rows, mat4.rows)
174
+ assert_equal(mat0.cols, mat4.cols)
175
+ assert_in_delta(90, count_threshold(mat4, 10), 10)
176
+
177
+ # Uncomment the following lines to show the images
178
+ # snap(['original', mat0], ['corner_harris(3)', mat1], ['corner_harris(3,3)', mat2],
179
+ # ['corner_harris(3,3,0.04)', mat3], ['corner_harris(3,7,0.01)', mat4])
180
+
181
+ assert_raise(TypeError) {
182
+ mat0.corner_harris(DUMMY_OBJ)
183
+ }
184
+ assert_raise(TypeError) {
185
+ mat0.corner_harris(3, DUMMY_OBJ)
186
+ }
187
+ assert_raise(TypeError) {
188
+ mat0.corner_harris(3, 3, DUMMY_OBJ)
189
+ }
190
+ end
191
+
192
+ def test_find_chessboard_corners
193
+ mat = CvMat.load(FILENAME_CHESSBOARD, CV_LOAD_IMAGE_GRAYSCALE)
194
+ pattern_size = CvSize.new(4, 4)
195
+ corners1, found1 = mat.find_chessboard_corners(pattern_size)
196
+ corners2, found2 = mat.find_chessboard_corners(pattern_size, CV_CALIB_CB_ADAPTIVE_THRESH)
197
+ corners3, found3 = mat.find_chessboard_corners(pattern_size, CV_CALIB_CB_NORMALIZE_IMAGE)
198
+ corners4, found4 = mat.find_chessboard_corners(pattern_size, CV_CALIB_CB_FILTER_QUADS)
199
+ corners5, found5 = mat.find_chessboard_corners(pattern_size, CV_CALIB_CB_FAST_CHECK)
200
+
201
+ expected = [[39, 39], [79, 39], [119, 39], [159, 39], [39, 79], [79, 79],
202
+ [119, 79], [159, 78], [38, 119], [79, 119], [119, 119], [158, 118],
203
+ [39, 159], [79, 159], [119, 159], [159, 159]]
204
+ [corners1, corners2, corners3, corners4, corners5].each { |corners|
205
+ assert_equal(expected.size, corners.size)
206
+ expected.zip(corners).each { |e, a|
207
+ assert_in_delta(e[0], a.x, 3.0)
208
+ assert_in_delta(e[1], a.y, 3.0)
209
+ }
210
+ }
211
+ [found1, found2, found3, found4, found5].each { |found|
212
+ assert(found)
213
+ }
214
+
215
+ assert_raise(TypeError) {
216
+ mat.find_chessboard_corners(DUMMY_OBJ)
217
+ }
218
+ assert_raise(TypeError) {
219
+ mat.find_chessboard_corners(pattern_size, DUMMY_OBJ)
220
+ }
221
+ end
222
+
223
+ def test_find_corner_sub_pix
224
+ mat = CvMat.load(FILENAME_CHESSBOARD, CV_LOAD_IMAGE_GRAYSCALE)
225
+ pattern_size = CvSize.new(4, 4)
226
+ corners, found = mat.find_chessboard_corners(pattern_size)
227
+ expected = [[39, 39], [79, 39], [119, 39], [159, 39], [39, 79], [79, 79],
228
+ [119, 79], [159, 78], [38, 119], [79, 119], [119, 119], [158, 118],
229
+ [39, 159], [79, 159], [119, 159], [159, 159]]
230
+
231
+ refined_corners = mat.find_corner_sub_pix(corners, CvSize.new(3, 3), CvSize.new(-1, -1),
232
+ CvTermCriteria.new(20, 0.03));
233
+ assert_equal(expected.size, refined_corners.size)
234
+ assert(found)
235
+ expected.zip(refined_corners).each { |e, a|
236
+ assert_in_delta(e[0], a.x, 3.0)
237
+ assert_in_delta(e[1], a.y, 3.0)
238
+ }
239
+
240
+ assert_raise(TypeError) {
241
+ mat.find_corner_sub_pix(DUMMY_OBJ, CvSize.new(3, 3), CvSize.new(-1, -1),
242
+ CvTermCriteria.new(20, 0.03));
243
+ }
244
+ assert_raise(TypeError) {
245
+ mat.find_corner_sub_pix(corners, DUMMY_OBJ, CvSize.new(-1, -1),
246
+ CvTermCriteria.new(20, 0.03));
247
+ }
248
+ assert_raise(TypeError) {
249
+ mat.find_corner_sub_pix(corners, CvSize.new(3, 3), DUMMY_OBJ,
250
+ CvTermCriteria.new(20, 0.03));
251
+ }
252
+ assert_raise(TypeError) {
253
+ mat.find_corner_sub_pix(corners, CvSize.new(3, 3), CvSize.new(-1, -1), DUMMY_OBJ);
254
+ }
255
+ end
256
+
257
+ def test_good_features_to_track
258
+ mat0 = CvMat.load(FILENAME_LENA32x32, CV_LOAD_IMAGE_GRAYSCALE)
259
+ mask = create_cvmat(mat0.rows, mat0.cols, :cv8u, 1) { |j, i, c|
260
+ if (i > 8 and i < 18) and (j > 8 and j < 18)
261
+ CvScalar.new(1)
262
+ else
263
+ CvScalar.new(0)
264
+ end
265
+ }
266
+
267
+ corners1 = mat0.good_features_to_track(0.2, 5)
268
+ corners2 = mat0.good_features_to_track(0.2, 5, :mask => mask)
269
+ corners3 = mat0.good_features_to_track(0.2, 5, :block_size => 7)
270
+ corners4 = mat0.good_features_to_track(0.2, 5, :use_harris => true)
271
+ corners5 = mat0.good_features_to_track(0.2, 5, :k => 0.01)
272
+ corners6 = mat0.good_features_to_track(0.2, 5, :max => 1)
273
+
274
+ expected1 = [[24, 7], [20, 23], [17, 11], [26, 29], [30, 24],
275
+ [19, 16], [28, 2], [13, 18], [14, 4]]
276
+ assert_equal(expected1.size, corners1.size)
277
+ expected1.each_with_index { |e, i|
278
+ assert_equal(e[0], corners1[i].x.to_i)
279
+ assert_equal(e[1], corners1[i].y.to_i)
280
+ }
281
+ expected2 = [[17, 11], [17, 16]]
282
+ assert_equal(expected2.size, corners2.size)
283
+ expected2.each_with_index { |e, i|
284
+ assert_equal(e[0], corners2[i].x.to_i)
285
+ assert_equal(e[1], corners2[i].y.to_i)
286
+ }
287
+
288
+ expected3 = [[21, 7], [22, 23], [18, 12], [28, 4], [28, 26],
289
+ [17, 27], [13, 20], [10, 11], [14, 5]]
290
+ assert_equal(expected3.size, corners3.size)
291
+ expected3.each_with_index { |e, i|
292
+ assert_equal(e[0], corners3[i].x.to_i)
293
+ assert_equal(e[1], corners3[i].y.to_i)
294
+ }
295
+
296
+ expected4 = [[24, 8], [20, 23], [16, 11],
297
+ [20, 16],[27, 28], [28, 2]]
298
+ assert_equal(expected4.size, corners4.size)
299
+ expected4.each_with_index { |e, i|
300
+ assert_equal(e[0], corners4[i].x.to_i)
301
+ assert_equal(e[1], corners4[i].y.to_i)
302
+ }
303
+
304
+ expected5 = [[24, 7], [20, 23], [17, 11], [26, 29], [30, 24],
305
+ [19, 16], [28, 2], [13, 18], [14, 4]]
306
+ assert_equal(expected5.size, corners5.size)
307
+ expected5.each_with_index { |e, i|
308
+ assert_equal(e[0], corners5[i].x.to_i)
309
+ assert_equal(e[1], corners5[i].y.to_i)
310
+ }
311
+
312
+ assert_equal(1, corners6.size)
313
+ assert_equal(24, corners6[0].x.to_i)
314
+ assert_equal(7, corners6[0].y.to_i)
315
+
316
+ assert_raise(ArgumentError) {
317
+ mat0.good_features_to_track(0.2, 5, :max => 0)
318
+ }
319
+
320
+ assert_raise(TypeError) {
321
+ mat0.good_features_to_track(DUMMY_OBJ, 5)
322
+ }
323
+ assert_raise(TypeError) {
324
+ mat0.good_features_to_track(0.2, DUMMY_OBJ)
325
+ }
326
+ assert_raise(TypeError) {
327
+ mat0.good_features_to_track(0.2, 5, :mask => DUMMY_OBJ)
328
+ }
329
+ assert_raise(TypeError) {
330
+ mat0.good_features_to_track(0.2, 5, :block_size => DUMMY_OBJ)
331
+ }
332
+ assert_raise(TypeError) {
333
+ mat0.good_features_to_track(0.2, 5, :k => DUMMY_OBJ)
334
+ }
335
+ assert_raise(TypeError) {
336
+ mat0.good_features_to_track(0.2, 5, :max => DUMMY_OBJ)
337
+ }
338
+ mat0.good_features_to_track(0.2, 5, :use_harris => DUMMY_OBJ)
339
+ end
340
+
341
+ def test_rect_sub_pix
342
+ mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH)
343
+ center = CvPoint2D32f.new(mat0.width / 2, mat0.height / 2)
344
+ mat1 = mat0.rect_sub_pix(center)
345
+ mat2 = mat0.rect_sub_pix(center, mat0.size)
346
+ mat3 = mat0.rect_sub_pix(center, CvSize.new(512, 512))
347
+
348
+ assert_equal('b3dc0e31260dd42b5341471e23e825d3', hash_img(mat1))
349
+ assert_equal('b3dc0e31260dd42b5341471e23e825d3', hash_img(mat2))
350
+ assert_equal('cc27ce8f4068efedcd31c4c782c3825c', hash_img(mat3))
351
+
352
+ assert_raise(TypeError) {
353
+ mat0.rect_sub_pix(DUMMY_OBJ)
354
+ }
355
+ assert_raise(TypeError) {
356
+ mat0.rect_sub_pix(center, DUMMY_OBJ)
357
+ }
358
+ end
359
+
360
+ def test_quadrangle_sub_pix
361
+ mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH)
362
+ angle = 60 * Math::PI / 180
363
+ map_matrix = CvMat.new(2, 3, :cv32f, 1)
364
+ map_matrix[0] = CvScalar.new(Math.cos(angle))
365
+ map_matrix[1] = CvScalar.new(-Math.sin(angle))
366
+ map_matrix[2] = CvScalar.new(mat0.width * 0.5)
367
+ map_matrix[3] = CvScalar.new(-map_matrix[1][0])
368
+ map_matrix[4] = map_matrix[0]
369
+ map_matrix[5] = CvScalar.new(mat0.height * 0.5)
370
+
371
+ mat1 = mat0.quadrangle_sub_pix(map_matrix)
372
+ mat2 = mat0.quadrangle_sub_pix(map_matrix, mat0.size)
373
+ mat3 = mat0.quadrangle_sub_pix(map_matrix, CvSize.new(512, 512))
374
+
375
+ assert_equal('f170c05fa50c3ac2a762d7b3f5c4ae2f', hash_img(mat1))
376
+ assert_equal('f170c05fa50c3ac2a762d7b3f5c4ae2f', hash_img(mat2))
377
+ assert_equal('4d949d5083405381ad9ea09dcd95e5a2', hash_img(mat3))
378
+
379
+ assert_raise(TypeError) {
380
+ mat0.quadrangle_sub_pix(DUMMY_OBJ)
381
+ }
382
+ assert_raise(TypeError) {
383
+ mat0.quadrangle_sub_pix(map_matrix, DUMMY_OBJ)
384
+ }
385
+ # assert_raise(CvError) {
386
+ # mat0.quadrangle_sub_pix(CvMat.new(3, 3))
387
+ # }
388
+ end
389
+
390
+ def test_resize
391
+ mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH)
392
+ size = CvSize.new(384, 384)
393
+ mat1 = mat0.resize(size)
394
+ mat2 = mat0.resize(size, CV_INTER_LINEAR)
395
+ mat3 = mat0.resize(size, CV_INTER_NN)
396
+ mat4 = mat0.resize(size, CV_INTER_AREA)
397
+ mat5 = mat0.resize(size, CV_INTER_CUBIC)
398
+ mat6 = mat0.resize(size, CV_INTER_LANCZOS4)
399
+
400
+ [mat1, mat2, mat3, mat4, mat5, mat6].each { |m|
401
+ assert_equal(size.width, m.cols)
402
+ assert_equal(size.height, m.rows)
403
+ assert_equal(mat0.depth, m.depth)
404
+ assert_equal(mat0.channel, m.channel)
405
+ }
406
+
407
+ assert_raise(TypeError) {
408
+ mat0.resize(DUMMY_OBJ)
409
+ }
410
+ assert_raise(TypeError) {
411
+ mat0.resize(size, DUMMY_OBJ)
412
+ }
413
+
414
+ # Uncomment the following lines to show the results
415
+ # snap(['original', mat0], ['default(linear)', mat1], ['linear', mat2],
416
+ # ['nn', mat3], ['area', mat4], ['cubic', mat5] , ['lanczos4', mat6])
417
+ end
418
+
419
+ def test_warp_affine
420
+ mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH)
421
+ map_matrix = CvMat.new(2, 3, :cv32f, 1)
422
+ # center: (128, 128), angle: 25 deg., scale: 1.0
423
+ map_matrix[0] = CvScalar.new(0.90631)
424
+ map_matrix[1] = CvScalar.new(0.42262)
425
+ map_matrix[2] = CvScalar.new(-42.10254)
426
+ map_matrix[3] = CvScalar.new(-0.42262)
427
+ map_matrix[4] = CvScalar.new(0.90631)
428
+ map_matrix[5] = CvScalar.new(66.08774)
429
+
430
+ mat1 = mat0.warp_affine(map_matrix)
431
+ mat2 = mat0.warp_affine(map_matrix, CV_INTER_NN | CV_WARP_FILL_OUTLIERS)
432
+ mat3 = mat0.warp_affine(map_matrix, CV_INTER_LINEAR | CV_WARP_FILL_OUTLIERS, CvColor::Yellow)
433
+ mat4 = mat0.warp_affine(map_matrix, CV_INTER_LINEAR | CV_WARP_FILL_OUTLIERS | CV_WARP_INVERSE_MAP)
434
+
435
+ [mat1, mat2, mat3, mat4].each { |m|
436
+ assert_equal(mat0.cols, m.cols)
437
+ assert_equal(mat0.rows, m.rows)
438
+ assert_equal(mat0.depth, m.depth)
439
+ assert_equal(mat0.channel, m.channel)
440
+ }
441
+
442
+ assert_raise(TypeError) {
443
+ mat0.warp_affine(DUMMY_OBJ)
444
+ }
445
+ assert_raise(TypeError) {
446
+ mat0.warp_affine(map_matrix, DUMMY_OBJ)
447
+ }
448
+
449
+ # Uncomment the following lines to show the results
450
+ # snap mat0, mat1, mat2, mat3, mat4
451
+ end
452
+
453
+ def test_get_perspective_transform
454
+ from = [
455
+ OpenCV::CvPoint2D32f.new(540, 382),
456
+ OpenCV::CvPoint2D32f.new(802, 400),
457
+ OpenCV::CvPoint2D32f.new(850, 731),
458
+ OpenCV::CvPoint2D32f.new(540, 731),
459
+ ]
460
+ to = [
461
+ OpenCV::CvPoint2D32f.new(0, 0),
462
+ OpenCV::CvPoint2D32f.new(233, 0),
463
+ OpenCV::CvPoint2D32f.new(233, 310),
464
+ OpenCV::CvPoint2D32f.new(0, 310),
465
+ ]
466
+ transform = OpenCV::CvMat.get_perspective_transform(from, to)
467
+ assert_equal 3, transform.rows
468
+ assert_equal 3, transform.columns
469
+ expected = [
470
+ 0.923332154750824,
471
+ 0.0,
472
+ 0.0,
473
+ 1.4432899320127035e-15,
474
+ 0.0,
475
+ 0.0,
476
+ -498.599365234375,
477
+ 0.0,
478
+ 0.0,
479
+ ]
480
+ 3.times do |i|
481
+ 3.times do |j|
482
+ assert_in_delta(expected.shift, transform[i][j], 0.001)
483
+ end
484
+ end
485
+ end
486
+
487
+ def test_rotation_matrix2D
488
+ mat1 = CvMat.rotation_matrix2D(CvPoint2D32f.new(10, 20), 60, 2.0)
489
+ expected = [1.0, 1.73205, -34.64102,
490
+ -1.73205, 1.0, 17.32051]
491
+ assert_equal(2, mat1.rows)
492
+ assert_equal(3, mat1.cols)
493
+ assert_equal(:cv32f, mat1.depth)
494
+ assert_equal(1, mat1.channel)
495
+ expected.each_with_index { |x, i|
496
+ assert_in_delta(x, mat1[i][0], 0.001)
497
+ }
498
+
499
+ assert_raise(TypeError) {
500
+ CvMat.rotation_matrix2D(DUMMY_OBJ, 60, 2.0)
501
+ }
502
+ assert_raise(TypeError) {
503
+ CvMat.rotation_matrix2D(CvPoint2D32f.new(10, 20), DUMMY_OBJ, 2.0)
504
+ }
505
+ assert_raise(TypeError) {
506
+ CvMat.rotation_matrix2D(CvPoint2D32f.new(10, 20), 60, DUMMY_OBJ)
507
+ }
508
+ end
509
+
510
+ def test_warp_perspective
511
+ mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH)
512
+ # Homography
513
+ # <src> => <dst>
514
+ # (0, 0) => (50, 0)
515
+ # (255, 0) => (205, 0)
516
+ # (255, 255) => (255, 220)
517
+ # (0, 255) => (0, 275)
518
+ map_matrix = CvMat.new(3, 3, :cv32f, 1)
519
+ map_matrix[0] = CvScalar.new(0.72430)
520
+ map_matrix[1] = CvScalar.new(-0.19608)
521
+ map_matrix[2] = CvScalar.new(50.00000)
522
+ map_matrix[3] = CvScalar.new(0.0)
523
+ map_matrix[4] = CvScalar.new(0.62489)
524
+ map_matrix[5] = CvScalar.new(0.0)
525
+ map_matrix[6] = CvScalar.new(0.00057)
526
+ map_matrix[7] = CvScalar.new(-0.00165)
527
+ map_matrix[8] = CvScalar.new(1.00000)
528
+
529
+ mat1 = mat0.warp_perspective(map_matrix)
530
+ mat2 = mat0.warp_perspective(map_matrix, CV_INTER_NN)
531
+ mat3 = mat0.warp_perspective(map_matrix, CV_INTER_LINEAR | CV_WARP_INVERSE_MAP)
532
+ mat4 = mat0.warp_perspective(map_matrix, CV_INTER_LINEAR | CV_WARP_FILL_OUTLIERS, CvColor::Yellow)
533
+
534
+ [mat1, mat2, mat3, mat4].each { |m|
535
+ assert_equal(mat0.cols, m.cols)
536
+ assert_equal(mat0.rows, m.rows)
537
+ assert_equal(mat0.depth, m.depth)
538
+ assert_equal(mat0.channel, m.channel)
539
+ }
540
+
541
+ assert_raise(TypeError) {
542
+ mat0.warp_perspective(DUMMY_OBJ)
543
+ }
544
+ assert_raise(TypeError) {
545
+ mat0.warp_perspective(map_matrix, DUMMY_OBJ)
546
+ }
547
+
548
+ # Uncomment the following line to show the results
549
+ # snap mat0, mat1, mat2, mat3, mat4
550
+ end
551
+
552
+ def test_remap
553
+ mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH)
554
+ matx = CvMat.new(mat0.height, mat0.width, :cv32f, 1).clear
555
+ maty = CvMat.new(mat0.height, mat0.width, :cv32f, 1).clear
556
+
557
+ cos30, sin30 = Math.cos(30 * Math::PI / 180), Math.sin(30 * Math::PI / 180)
558
+ half_width, half_height = mat0.width / 2, mat0.height / 2
559
+ mat0.height.times { |j|
560
+ mat0.width.times { |i|
561
+ x0 = i - half_width
562
+ y0 = j - half_height
563
+ x = x0 * cos30 - y0 * sin30 + half_width
564
+ y = x0 * sin30 + y0 * cos30 + half_height
565
+ matx[j, i] = CvScalar.new(x)
566
+ maty[j, i] = CvScalar.new(y)
567
+ }
568
+ }
569
+
570
+ mat1 = mat0.remap(matx, maty)
571
+ mat2 = mat0.remap(matx, maty, CV_INTER_NN)
572
+ mat3 = mat0.remap(matx, maty, CV_INTER_LINEAR | CV_WARP_FILL_OUTLIERS, CvColor::Yellow)
573
+
574
+ [mat1, mat2, mat3].each { |m|
575
+ assert_equal(mat0.cols, m.cols)
576
+ assert_equal(mat0.rows, m.rows)
577
+ assert_equal(mat0.depth, m.depth)
578
+ assert_equal(mat0.channel, m.channel)
579
+ }
580
+
581
+ assert_raise(TypeError) {
582
+ mat0.remap(DUMMY_OBJ, maty)
583
+ }
584
+ assert_raise(TypeError) {
585
+ mat0.remap(matx, DUMMY_OBJ)
586
+ }
587
+ assert_raise(TypeError) {
588
+ mat0.remap(matx, maty, DUMMY_OBJ)
589
+ }
590
+
591
+ # Uncomment the following line to show the results
592
+ # snap mat0, mat1, mat2, mat3
593
+ end
594
+
595
+ def test_log_polar
596
+ mat0 = CvMat.load(FILENAME_FRUITS, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH)
597
+
598
+ mat1 = mat0.log_polar(CvSize.new(255, 255), CvPoint2D32f.new(mat0.width / 2, mat0.height / 2), 40)
599
+ mat2 = mat0.log_polar(CvSize.new(255, 255), CvPoint2D32f.new(mat0.width / 2, mat0.height / 2), 40,
600
+ CV_INTER_LINEAR | CV_WARP_FILL_OUTLIERS)
601
+ mat3 = mat1.log_polar(mat0.size, CvPoint2D32f.new(mat0.width / 2, mat0.height / 2), 40,
602
+ CV_INTER_LINEAR | CV_WARP_FILL_OUTLIERS | CV_WARP_INVERSE_MAP)
603
+
604
+ [mat1, mat2].each { |mat|
605
+ assert_equal(mat0.depth, mat.depth)
606
+ assert_equal(mat0.channel, mat.channel)
607
+ b, g, r = color_hists(mat)
608
+ assert_in_delta(4000000, b, 100000)
609
+ assert_in_delta(5860000, g, 100000)
610
+ assert_in_delta(7700000, r, 100000)
611
+ }
612
+
613
+ b, g, r = color_hists(mat3)
614
+ assert_equal(mat0.depth, mat3.depth)
615
+ assert_equal(mat0.channel, mat3.channel)
616
+ assert_in_delta(11200000, b, 1000000)
617
+ assert_in_delta(20800000, g, 1000000)
618
+ assert_in_delta(26900000, r, 1000000)
619
+
620
+ # Uncomment the following line to show the results
621
+ # snap mat0, mat1, mat2
622
+ end
623
+
624
+ def test_erode
625
+ mat0 = create_cvmat(9, 9, :cv8u, 1) { |j, i, c|
626
+ if i >= 3 and i < 6 and j >= 3 and j < 6
627
+ CvScalar.new(255)
628
+ else
629
+ CvScalar.new(0)
630
+ end
631
+ }
632
+
633
+ mat1 = create_cvmat(9, 9, :cv8u, 1) { |j, i, c|
634
+ if i >= 1 and i < 8 and j >= 1 and j < 8
635
+ CvScalar.new(255)
636
+ else
637
+ CvScalar.new(0)
638
+ end
639
+ }
640
+
641
+ mat2 = create_cvmat(5, 5, :cv8u, 1) { |j, i, c|
642
+ if i == 2 or j == 2
643
+ CvScalar.new(255)
644
+ else
645
+ CvScalar.new(0)
646
+ end
647
+ }
648
+
649
+ mat3 = mat0.erode
650
+ mat4 = mat0.erode(nil, 1)
651
+ mat5 = mat1.erode(nil, 2)
652
+ mat6 = mat1.erode(IplConvKernel.new(5, 5, 2, 2, :cross))
653
+ mat7 = mat0.clone
654
+ mat7.erode!
655
+
656
+ assert_equal('075eb0e281328f768eb862735d16979d', hash_img(mat3))
657
+ assert_equal('075eb0e281328f768eb862735d16979d', hash_img(mat4))
658
+ assert_equal('9f02fc4438b1d69fea75a10dfd2b66b0', hash_img(mat5))
659
+ assert_equal('9f02fc4438b1d69fea75a10dfd2b66b0', hash_img(mat6))
660
+ assert_equal('075eb0e281328f768eb862735d16979d', hash_img(mat7))
661
+
662
+ assert_raise(TypeError) {
663
+ mat0.erode(DUMMY_OBJ)
664
+ }
665
+ assert_raise(TypeError) {
666
+ mat0.erode(nil, DUMMY_OBJ)
667
+ }
668
+ end
669
+
670
+ def test_dilate
671
+ mat0 = create_cvmat(9, 9, :cv8u, 1) { |j, i, c|
672
+ if i == 4 and j == 4
673
+ CvScalar.new(255)
674
+ else
675
+ CvScalar.new(0)
676
+ end
677
+ }
678
+
679
+ mat1 = create_cvmat(5, 5, :cv8u, 1) { |j, i, c|
680
+ if i == 2 or j == 2
681
+ CvScalar.new(255)
682
+ else
683
+ CvScalar.new(0)
684
+ end
685
+ }
686
+
687
+ mat2 = mat0.dilate
688
+ mat3 = mat0.dilate(nil, 1)
689
+ mat4 = mat0.dilate(nil, 2)
690
+ mat5 = mat1.dilate(IplConvKernel.new(5, 5, 2, 2, :cross))
691
+ mat6 = mat0.clone
692
+ mat6.dilate!
693
+
694
+ assert_equal('9f02fc4438b1d69fea75a10dfd2b66b0', hash_img(mat2))
695
+ assert_equal('9f02fc4438b1d69fea75a10dfd2b66b0', hash_img(mat3))
696
+ assert_equal('ebf07f2a0edd2fd0fe26ff5921c6871b', hash_img(mat4))
697
+ assert_equal('2841937c35c311e947bee49864b9d295', hash_img(mat5))
698
+ assert_equal('9f02fc4438b1d69fea75a10dfd2b66b0', hash_img(mat6))
699
+
700
+ assert_raise(TypeError) {
701
+ mat0.dilate(DUMMY_OBJ)
702
+ }
703
+ assert_raise(TypeError) {
704
+ mat0.dilate(nil, DUMMY_OBJ)
705
+ }
706
+ end
707
+
708
+ def test_morphology
709
+ mat0 = create_cvmat(64, 64, :cv8u, 1) { |j, i, c|
710
+ if i >= 8 and i < 56 and j >= 8 and j < 56 and (i + j) % 15 != 0
711
+ CvScalar.new(255)
712
+ else
713
+ CvScalar.new(0)
714
+ end
715
+ }
716
+
717
+ # Open
718
+ kernel = IplConvKernel.new(5, 5, 2, 2, :cross)
719
+ mat1 = mat0.morphology(CV_MOP_OPEN, kernel)
720
+ mat2 = mat0.morphology(:open, kernel)
721
+ assert_equal('63ccb07cb93efb1563657f51e3d89252', hash_img(mat1))
722
+ assert_equal('63ccb07cb93efb1563657f51e3d89252', hash_img(mat2))
723
+
724
+ # Close
725
+ mat1 = mat0.morphology(CV_MOP_CLOSE, kernel)
726
+ mat2 = mat0.morphology(:close, kernel)
727
+ assert_equal('831c513d6ed86bce3f15c697de4a72f8', hash_img(mat1))
728
+ assert_equal('831c513d6ed86bce3f15c697de4a72f8', hash_img(mat2))
729
+
730
+ # Gradient
731
+ mat1 = mat0.morphology(CV_MOP_GRADIENT, kernel)
732
+ mat2 = mat0.morphology(:gradient, kernel)
733
+ assert_equal('1e8007c211d6f464cf8584e8e83b3c35', hash_img(mat1))
734
+ assert_equal('1e8007c211d6f464cf8584e8e83b3c35', hash_img(mat2))
735
+
736
+ # Top hat
737
+ mat1 = mat0.morphology(CV_MOP_TOPHAT, kernel)
738
+ mat2 = mat0.morphology(:tophat, kernel)
739
+ assert_equal('1760c5b63a52df37069164fe3e901aa4', hash_img(mat1))
740
+ assert_equal('1760c5b63a52df37069164fe3e901aa4', hash_img(mat2))
741
+
742
+ # Black hat
743
+ mat1 = mat0.morphology(CV_MOP_BLACKHAT, kernel)
744
+ mat2 = mat0.morphology(:blackhat, kernel)
745
+ assert_equal('18b1d51637b912a38133341ee006c6ff', hash_img(mat1))
746
+ assert_equal('18b1d51637b912a38133341ee006c6ff', hash_img(mat2))
747
+
748
+ [:open, :close, :gradient, :tophat, :blackhat].each { |type|
749
+ assert_raise(TypeError) {
750
+ mat0.morphology(type, DUMMY_OBJ)
751
+ }
752
+ }
753
+ end
754
+
755
+ def test_smooth
756
+ mat0 = CvMat.load(FILENAME_LENA32x32, CV_LOAD_IMAGE_GRAYSCALE)
757
+
758
+ assert_raise(TypeError) {
759
+ mat0.smooth(DUMMY_OBJ)
760
+ }
761
+
762
+ # Blur no scale
763
+ mat1 = mat0.smooth(CV_BLUR_NO_SCALE)
764
+ mat2 = mat0.smooth(:blur_no_scale, 3, 3)
765
+ mat3 = mat0.smooth(CV_BLUR_NO_SCALE, 7, 7)
766
+ mat4 = CvMat.new(32, 32, :cv32f, 1).smooth(:blur_no_scale)
767
+
768
+ [mat1, mat2, mat3].each { |m|
769
+ assert_equal(1, m.channel)
770
+ assert_equal(:cv16u, m.depth)
771
+ }
772
+ assert_equal(1, mat4.channel)
773
+ assert_equal(:cv32f, mat4.depth)
774
+
775
+ assert_equal('3c9074c87b65117798f48e41a17b2f30', hash_img(mat1))
776
+ assert_equal('3c9074c87b65117798f48e41a17b2f30', hash_img(mat2))
777
+ assert_equal('9c549aa406a425a65b036c2f9a2689e0', hash_img(mat3))
778
+
779
+ assert_raise(TypeError) {
780
+ mat0.smooth(CV_BLUR_NO_SCALE, DUMMY_OBJ, 0, 0, 0)
781
+ }
782
+ assert_raise(TypeError) {
783
+ mat0.smooth(CV_BLUR_NO_SCALE, 3, DUMMY_OBJ, 0, 0)
784
+ }
785
+
786
+ # Blur
787
+ mat1 = mat0.smooth(CV_BLUR)
788
+ mat2 = mat0.smooth(:blur, 3, 3)
789
+ mat3 = mat0.smooth(CV_BLUR, 7, 7)
790
+ mat4 = CvMat.new(32, 32, :cv16u, 1).smooth(:blur)
791
+ mat5 = CvMat.new(32, 32, :cv32f, 1).smooth(CV_BLUR)
792
+ mat6 = CvMat.new(32, 32, :cv8u, 3).smooth(:blur)
793
+
794
+ [mat1, mat2, mat3].each { |m|
795
+ assert_equal(1, m.channel)
796
+ assert_equal(:cv8u, m.depth)
797
+ }
798
+ assert_equal(1, mat4.channel)
799
+ assert_equal(:cv16u, mat4.depth)
800
+ assert_equal(1, mat5.channel)
801
+ assert_equal(:cv32f, mat5.depth)
802
+ assert_equal(3, mat6.channel)
803
+ assert_equal(:cv8u, mat6.depth)
804
+
805
+ assert_equal('f2473b5b964ae8950f6a7fa5cde4c67a', hash_img(mat1))
806
+ assert_equal('f2473b5b964ae8950f6a7fa5cde4c67a', hash_img(mat2))
807
+ assert_equal('d7bb344fc0f6ec0da4b9754d319e4e4a', hash_img(mat3))
808
+
809
+ assert_raise(TypeError) {
810
+ mat0.smooth(CV_BLUR, DUMMY_OBJ, 0, 0, 0)
811
+ }
812
+ assert_raise(TypeError) {
813
+ mat0.smooth(CV_BLUR, 3, DUMMY_OBJ, 0, 0)
814
+ }
815
+
816
+ # Gaussian
817
+ mat1 = mat0.smooth(CV_GAUSSIAN)
818
+ mat2 = mat0.smooth(:gaussian, 3, 3)
819
+ mat3 = mat0.smooth(CV_GAUSSIAN, 3, 3, 3)
820
+ mat4 = mat0.smooth(:gaussian, 3, 3, 3, 3)
821
+ mat5 = mat0.smooth(CV_GAUSSIAN, 7, 7, 5, 3)
822
+
823
+ mat6 = CvMat.new(32, 32, :cv16u, 1).smooth(CV_GAUSSIAN)
824
+ mat7 = CvMat.new(32, 32, :cv32f, 1).smooth(CV_GAUSSIAN)
825
+ mat8 = CvMat.new(32, 32, :cv8u, 3).smooth(CV_GAUSSIAN)
826
+
827
+ [mat1, mat2, mat3, mat4, mat5].each { |m|
828
+ assert_equal(1, m.channel)
829
+ assert_equal(:cv8u, m.depth)
830
+ }
831
+ assert_equal(1, mat6.channel)
832
+ assert_equal(:cv16u, mat6.depth)
833
+ assert_equal(1, mat7.channel)
834
+ assert_equal(:cv32f, mat7.depth)
835
+ assert_equal(3, mat8.channel)
836
+ assert_equal(:cv8u, mat8.depth)
837
+
838
+ assert_equal('580c88f3e0e317a5770be3f28f31eda2', hash_img(mat1))
839
+ assert_equal('580c88f3e0e317a5770be3f28f31eda2', hash_img(mat2))
840
+ assert_equal('a1ffaa14522719e37d75eec18ff8b309', hash_img(mat3))
841
+ assert_equal('a1ffaa14522719e37d75eec18ff8b309', hash_img(mat4))
842
+ assert_equal('f7f8b4eff3240ffc8f259ce975936d92', hash_img(mat5))
843
+
844
+ assert_raise(TypeError) {
845
+ mat0.smooth(CV_GAUSSIAN, DUMMY_OBJ, 0, 0, 0)
846
+ }
847
+ assert_raise(TypeError) {
848
+ mat0.smooth(CV_GAUSSIAN, 3, DUMMY_OBJ, 0, 0)
849
+ }
850
+ assert_raise(TypeError) {
851
+ mat0.smooth(CV_GAUSSIAN, 3, 0, DUMMY_OBJ, 0)
852
+ }
853
+ assert_raise(TypeError) {
854
+ mat0.smooth(CV_GAUSSIAN, 3, 0, 0, DUMMY_OBJ)
855
+ }
856
+
857
+ # Median
858
+ mat0 = create_cvmat(64, 64, :cv8u, 1) { |j, i, c|
859
+ if (i + j) % 15 != 0
860
+ CvScalar.new(255)
861
+ else
862
+ CvScalar.new(0)
863
+ end
864
+ }
865
+ (-1..1).each { |dy|
866
+ (-1..1).each { |dx|
867
+ mat0[32 + dy, 32 + dx] = CvScalar.new(0)
868
+ }
869
+ }
870
+
871
+ mat1 = mat0.smooth(CV_MEDIAN)
872
+ mat2 = mat0.smooth(:median, 3)
873
+ mat3 = mat0.smooth(CV_MEDIAN, 7)
874
+ mat4 = CvMat.new(64, 64, :cv8u, 3).smooth(CV_MEDIAN)
875
+
876
+ assert_equal('7343a41c542e034db356636c06134961', hash_img(mat1))
877
+ assert_equal('7343a41c542e034db356636c06134961', hash_img(mat2))
878
+ assert_equal('6ae59e64850377ee5470c854761551ea', hash_img(mat3))
879
+
880
+ assert_raise(TypeError) {
881
+ mat0.smooth(CV_MEDIAN, DUMMY_OBJ, 0, 0, 0)
882
+ }
883
+
884
+ # Bilateral
885
+ mat0 = create_cvmat(64, 64, :cv8u, 1) { |j, i, c|
886
+ if i > 32
887
+ (i + j) % 15 != 0 ? CvScalar.new(32) : CvScalar.new(224)
888
+ else
889
+ (i + j) % 15 != 0 ? CvScalar.new(224) : CvScalar.new(32)
890
+ end
891
+ }
892
+
893
+ mat1 = mat0.smooth(CV_BILATERAL)
894
+ mat2 = mat0.smooth(:bilateral, 3, 3)
895
+ mat3 = mat0.smooth(CV_BILATERAL, 7, 7)
896
+ mat4 = CvMat.new(64, 64, :cv8u, 3).smooth(CV_BILATERAL)
897
+
898
+ assert_raise(TypeError) {
899
+ mat0.smooth(CV_BILATERAL, DUMMY_OBJ, 0, 0, 0)
900
+ }
901
+ assert_raise(TypeError) {
902
+ mat0.smooth(CV_BILATERAL, 3, DUMMY_OBJ, 0, 0)
903
+ }
904
+
905
+ flunk('FIXME: Cases of CvMat#smooth(CV_BILATERAL) are not tested yet.')
906
+ end
907
+
908
+ def test_filter2d
909
+ mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_GRAYSCALE)
910
+ kernel = CvMat.new(3, 3, :cv32f, 1)
911
+
912
+ # Laplacian filter kernel
913
+ laplace4 = [0, 1, 0,
914
+ 1, -4, 1,
915
+ 0, 1, 0]
916
+ laplace4.each_with_index { |x, i| kernel[i] = CvScalar.new(x) }
917
+
918
+ mat1 = mat0.filter2d(kernel)
919
+ mat2 = mat0.filter2d(kernel, CvPoint.new(-1, -1))
920
+ mat3 = mat0.filter2d(kernel, CvPoint.new(0, 0))
921
+
922
+ assert_equal('14a01cc47078e8f8fe4f0fd510d5521b', hash_img(mat1))
923
+ assert_equal('14a01cc47078e8f8fe4f0fd510d5521b', hash_img(mat2))
924
+ assert_equal('30e04de43f9240df6aadbaea6467b8fe', hash_img(mat3))
925
+
926
+ assert_raise(TypeError) {
927
+ mat0.filter2d(DUMMY_OBJ)
928
+ }
929
+ assert_raise(TypeError) {
930
+ mat0.filter2d(kernel, DUMMY_OBJ)
931
+ }
932
+ end
933
+
934
+ def test_copy_make_border
935
+ mat0 = create_cvmat(32, 32, :cv8u, 1) { CvScalar.new(128) }
936
+
937
+ [IPL_BORDER_CONSTANT, :constant].each { |type|
938
+ mat1 = mat0.copy_make_border(type, CvSize.new(64, 48), CvPoint.new(16, 8), 255)
939
+ assert_equal('5e231f8ca051b8f93e4aaa42d193d095', hash_img(mat1))
940
+ }
941
+
942
+ [IPL_BORDER_REPLICATE, :replicate].each { |type|
943
+ mat2 = mat0.copy_make_border(type, CvSize.new(300, 300), CvPoint.new(30, 30))
944
+ assert_equal('96940dc9e3abb6e2556ea51af1468031', hash_img(mat2))
945
+ }
946
+
947
+ assert_raise(TypeError) {
948
+ mat0.copy_make_border(DUMMY_OBJ, CvSize.new(64, 48), CvPoint.new(16, 8))
949
+ }
950
+ assert_raise(TypeError) {
951
+ mat0.copy_make_border(IPL_BORDER_CONSTANT, CvSize.new(64, 48), DUMMY_OBJ)
952
+ }
953
+ assert_raise(TypeError) {
954
+ mat0.copy_make_border(IPL_BORDER_CONSTANT, CvSize.new(64, 48), CvPoint.new(16, 8), DUMMY_OBJ)
955
+ }
956
+ assert_raise(ArgumentError) {
957
+ mat0.copy_make_border(:dummy, CvSize.new(64, 48), CvPoint.new(16, 8), DUMMY_OBJ)
958
+ }
959
+ end
960
+
961
+ def test_integral
962
+ mat0 = create_cvmat(3, 3, :cv8u, 1) { |j, i, n| CvScalar.new(n) }
963
+
964
+ result_sum = []
965
+ result_sqsum = []
966
+ result_tiled_sum = []
967
+
968
+ result1 = mat0.integral
969
+ assert_equal(CvMat, result1.class)
970
+ result_sum << result1
971
+
972
+ result2 = mat0.integral(true)
973
+ assert_equal(Array, result2.class)
974
+ assert_equal(2, result2.size)
975
+ assert(result2.all? {|a| a.class == CvMat})
976
+ result_sum << result2[0]
977
+ result_sqsum << result2[1]
978
+
979
+ result3 = mat0.integral(true, true)
980
+ assert_equal(Array, result3.class)
981
+ assert_equal(3, result3.size)
982
+ assert(result3.all? {|a| a.class == CvMat})
983
+ result_sum << result3[0]
984
+ result_sqsum << result3[1]
985
+ result_tiled_sum << result3[2]
986
+
987
+ result4 = mat0.integral(true, false)
988
+ assert_equal(Array, result4.class)
989
+ assert_equal(2, result4.size)
990
+ assert(result4.all? {|a| a.class == CvMat})
991
+ result_sum << result4[0]
992
+ result_sqsum << result4[1]
993
+
994
+ result5 = mat0.integral(false, true)
995
+ assert_equal(Array, result5.class)
996
+ assert_equal(2, result5.size)
997
+ assert(result5.all? {|a| a.class == CvMat})
998
+ result_sum << result5[0]
999
+ result_tiled_sum << result5[1]
1000
+
1001
+ (result_sum + result_sqsum + result_tiled_sum).each { |s|
1002
+ assert_equal(mat0.height + 1, s.height)
1003
+ assert_equal(mat0.width + 1, s.width)
1004
+ assert_equal(:cv64f, s.depth)
1005
+ assert_equal(1, s.channel)
1006
+ }
1007
+
1008
+ expected_sum = [0, 0, 0, 0,
1009
+ 0, 0, 1, 3,
1010
+ 0, 3, 8, 15,
1011
+ 0, 9, 21, 36]
1012
+ result_sum.each { |sum|
1013
+ expected_sum.each_with_index { |x, i|
1014
+ assert_in_delta(x, sum[i][0], 0.001)
1015
+ }
1016
+ }
1017
+
1018
+ expected_sqsum = [0, 0, 0, 0,
1019
+ 0, 0, 1, 5,
1020
+ 0, 9, 26, 55,
1021
+ 0, 45, 111, 204]
1022
+ result_sqsum.each { |sqsum|
1023
+ expected_sqsum.each_with_index { |x, i|
1024
+ assert_in_delta(x, sqsum[i][0], 0.001)
1025
+ }
1026
+ }
1027
+
1028
+ expected_tilted_sum = [0, 0, 0, 0,
1029
+ 0, 0, 1, 2,
1030
+ 0, 4, 7, 8,
1031
+ 4, 16, 22, 20]
1032
+ result_tiled_sum.each { |tiled_sum|
1033
+ expected_tilted_sum.each_with_index { |x, i|
1034
+ assert_in_delta(x, tiled_sum[i][0], 0.001)
1035
+ }
1036
+ }
1037
+
1038
+ mat0.integral(DUMMY_OBJ, DUMMY_OBJ)
1039
+ end
1040
+
1041
+ def test_threshold
1042
+ mat0 = create_cvmat(3, 3, :cv8u, 1) { |j, i, n| CvScalar.new(n) }
1043
+ test_proc = lambda { |type, type_sym, expected_mat, expected_threshold|
1044
+ mat1 = mat0.threshold(expected_threshold, 7, type)
1045
+ mat2 = mat0.threshold(expected_threshold, 7, type_sym)
1046
+ [mat1, mat2].each { |m|
1047
+ expected_mat.each_with_index { |x, i|
1048
+ assert_equal(x, m[i][0])
1049
+ }
1050
+ }
1051
+ }
1052
+
1053
+ test_proc_with_otsu = lambda { |type, type_sym, expected_mat, expected_threshold|
1054
+ mat3, th3 = mat0.threshold(5, 7, type | CV_THRESH_OTSU)
1055
+ mat4, th4 = mat0.threshold(3, 7, type_sym, true)
1056
+ mat5, th5 = mat0.threshold(5, 7, type | CV_THRESH_OTSU, true)
1057
+ [mat3, mat4, mat5].each { |m|
1058
+ expected_mat.each_with_index { |x, i|
1059
+ assert_equal(x, m[i][0])
1060
+ }
1061
+ }
1062
+ [th3, th4, th5].each { |th|
1063
+ assert_in_delta(expected_threshold, th, 0.001)
1064
+ }
1065
+ }
1066
+
1067
+ # Binary
1068
+ expected = [0, 0, 0,
1069
+ 0, 0, 7,
1070
+ 7, 7, 7]
1071
+ test_proc.call(CV_THRESH_BINARY, :binary, expected, 4)
1072
+
1073
+ expected = [0, 0, 0,
1074
+ 0, 7, 7,
1075
+ 7, 7, 7]
1076
+ test_proc_with_otsu.call(CV_THRESH_BINARY, :binary, expected, 3)
1077
+
1078
+ # Binary inverse
1079
+ expected = [7, 7, 7,
1080
+ 7, 7, 0,
1081
+ 0, 0, 0]
1082
+ test_proc.call(CV_THRESH_BINARY_INV, :binary_inv, expected, 4)
1083
+
1084
+ expected = [7, 7, 7,
1085
+ 7, 0, 0,
1086
+ 0, 0, 0]
1087
+ test_proc_with_otsu.call(CV_THRESH_BINARY_INV, :binary_inv, expected, 3)
1088
+
1089
+ # Trunc
1090
+ expected = [0, 1, 2,
1091
+ 3, 4, 4,
1092
+ 4, 4, 4]
1093
+ test_proc.call(CV_THRESH_TRUNC, :trunc, expected, 4)
1094
+
1095
+ expected = [0, 1, 2,
1096
+ 3, 3, 3,
1097
+ 3, 3, 3]
1098
+ test_proc_with_otsu.call(CV_THRESH_TRUNC, :trunc, expected, 3)
1099
+
1100
+ # To zero
1101
+ expected = [0, 0, 0,
1102
+ 0, 0, 5,
1103
+ 6, 7, 8]
1104
+ test_proc.call(CV_THRESH_TOZERO, :tozero, expected, 4)
1105
+
1106
+ expected = [0, 0, 0,
1107
+ 0, 4, 5,
1108
+ 6, 7, 8]
1109
+ test_proc_with_otsu.call(CV_THRESH_TOZERO, :tozero, expected, 3)
1110
+
1111
+ # To zero inverse
1112
+ expected = [0, 1, 2,
1113
+ 3, 4, 0,
1114
+ 0, 0, 0]
1115
+ test_proc.call(CV_THRESH_TOZERO_INV, :tozero_inv, expected, 4)
1116
+
1117
+ expected = [0, 1, 2,
1118
+ 3, 0, 0,
1119
+ 0, 0, 0]
1120
+ test_proc_with_otsu.call(CV_THRESH_TOZERO_INV, :tozero_inv, expected, 3)
1121
+
1122
+ assert_raise(TypeError) {
1123
+ mat0.threshold(DUMMY_OBJ, 2, :binary)
1124
+ }
1125
+ assert_raise(TypeError) {
1126
+ mat0.threshold(1, DUMMY_OBJ, :binary)
1127
+ }
1128
+ assert_raise(TypeError) {
1129
+ mat0.threshold(1, 2, DUMMY_OBJ)
1130
+ }
1131
+ assert_raise(ArgumentError) {
1132
+ mat0.threshold(1, 2, :dummy)
1133
+ }
1134
+ mat0.threshold(1, 2, :binary, DUMMY_OBJ)
1135
+ end
1136
+
1137
+ def test_adaptive_threshold
1138
+ mat0 = create_cvmat(5, 5, :cv8u, 1) { |j, i, c| (c + 1) * 10 }
1139
+
1140
+ mat1 = mat0.adaptive_threshold(128)
1141
+ expected1 = [0, 0, 0, 0, 0, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128]
1142
+ expected1.each_with_index { |expected, i|
1143
+ assert_equal(expected, mat1[i][0])
1144
+ }
1145
+
1146
+ mat2a = mat0.adaptive_threshold(255, :adaptive_method => :mean_c,
1147
+ :threshold_type => :binary, :block_size => 5,
1148
+ :param1 => 10)
1149
+ mat2b = mat0.adaptive_threshold(255, :adaptive_method => CV_THRESH_BINARY,
1150
+ :threshold_type => CV_ADAPTIVE_THRESH_MEAN_C, :block_size => 5,
1151
+ :param1 => 10)
1152
+ expected2 = [0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255]
1153
+ [mat2a, mat2b].each { |mat2|
1154
+ assert_equal(CvMat, mat2.class)
1155
+ assert_equal(mat0.rows, mat2.rows)
1156
+ assert_equal(mat0.cols, mat2.cols)
1157
+ assert_equal(mat0.depth, mat2.depth)
1158
+ assert_equal(mat0.channel, mat2.channel)
1159
+ expected2.each_with_index { |expected, i|
1160
+ assert_equal(expected, mat2[i][0])
1161
+ }
1162
+ }
1163
+
1164
+
1165
+ mat3a = mat0.adaptive_threshold(255, :adaptive_method => :gaussian_c,
1166
+ :threshold_type => :binary_inv, :block_size => 5,
1167
+ :param1 => 10)
1168
+ mat3b = mat0.adaptive_threshold(255, :adaptive_method => CV_ADAPTIVE_THRESH_GAUSSIAN_C,
1169
+ :threshold_type => CV_THRESH_BINARY_INV, :block_size => 5,
1170
+ :param1 => 10)
1171
+ expected3 = [255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
1172
+ [mat3a, mat3b].each { |mat3|
1173
+ assert_equal(CvMat, mat3.class)
1174
+ assert_equal(mat0.rows, mat3.rows)
1175
+ assert_equal(mat0.cols, mat3.cols)
1176
+ assert_equal(mat0.depth, mat3.depth)
1177
+ assert_equal(mat0.channel, mat3.channel)
1178
+ expected3.each_with_index { |expected, i|
1179
+ assert_equal(expected, mat3[i][0])
1180
+ }
1181
+ }
1182
+
1183
+ assert_raise(TypeError) {
1184
+ mat0.adaptive_threshold(DUMMY_OBJ)
1185
+ }
1186
+ assert_raise(TypeError) {
1187
+ mat0.adaptive_threshold(0, DUMMY_OBJ)
1188
+ }
1189
+ assert_raise(TypeError) {
1190
+ mat0.adaptive_threshold(0, :adaptive_method => DUMMY_OBJ)
1191
+ }
1192
+ assert_raise(TypeError) {
1193
+ mat0.adaptive_threshold(0, :threshold_type => DUMMY_OBJ)
1194
+ }
1195
+ assert_raise(TypeError) {
1196
+ mat0.adaptive_threshold(0, :block_size => DUMMY_OBJ)
1197
+ }
1198
+ assert_raise(TypeError) {
1199
+ mat0.adaptive_threshold(0, :param1 => DUMMY_OBJ)
1200
+ }
1201
+ end
1202
+
1203
+ def test_pyr_down
1204
+ mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH)
1205
+ mat1 = mat0.pyr_down
1206
+ mat2 = mat0.pyr_down(:gaussian_5x5)
1207
+
1208
+ assert_equal('de9ff2ffcf8e43f28564a201cf90b7f4', hash_img(mat1))
1209
+ assert_equal('de9ff2ffcf8e43f28564a201cf90b7f4', hash_img(mat2))
1210
+
1211
+ assert_raise(TypeError) {
1212
+ mat0.pyr_down(DUMMY_OBJ)
1213
+ }
1214
+ end
1215
+
1216
+ def test_pyr_up
1217
+ mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH)
1218
+ mat1 = mat0.pyr_up
1219
+ mat2 = mat0.pyr_up(:gaussian_5x5)
1220
+
1221
+ [mat1, mat2].each { |mat|
1222
+ assert_equal(mat0.cols * 2, mat.cols)
1223
+ assert_equal(mat0.rows * 2, mat.rows)
1224
+ assert_equal(mat0.depth, mat.depth)
1225
+ assert_equal(mat0.channel, mat.channel)
1226
+ b, g, r = color_hists(mat)
1227
+ assert_in_delta(27500000, b, 1000000)
1228
+ assert_in_delta(26000000, g, 1000000)
1229
+ assert_in_delta(47000000, r, 1000000)
1230
+ }
1231
+ # Uncomment the following lines to show the result
1232
+ # snap mat0, mat1, mat2
1233
+
1234
+ assert_raise(TypeError) {
1235
+ mat0.pyr_up(DUMMY_OBJ)
1236
+ }
1237
+ end
1238
+
1239
+ def test_flood_fill
1240
+ mat0 = create_cvmat(128, 256, :cv8u, 1) { |j, i, c|
1241
+ if (i >= 32 and i < 224) and (j >= 32 and j < 96)
1242
+ CvScalar.new(255)
1243
+ elsif (i >= 16 and i < 240) and (j >= 16 and j < 112)
1244
+ CvScalar.new(192)
1245
+ else
1246
+ CvScalar.new(128)
1247
+ end
1248
+ }
1249
+
1250
+ point = CvPoint.new(20, 20)
1251
+ mat1, comp1, mask1 = mat0.flood_fill(point, 0)
1252
+ mat2, comp2, mask2 = mat0.flood_fill(point, 0, CvScalar.new(64))
1253
+ mat3, comp3, mask3 = mat0.flood_fill(point, 0, CvScalar.new(0), CvScalar.new(64))
1254
+ mat4, comp4, mask4 = mat0.flood_fill(point, 0, CvScalar.new(0), CvScalar.new(64),
1255
+ {:connectivity => 8, :fixed_range => true, :mask_only => true})
1256
+ mat05 = mat0.clone
1257
+ mat5, comp5, mask5 = mat05.flood_fill!(point, 0, CvScalar.new(0), CvScalar.new(64),
1258
+ {:connectivity => 8, :fixed_range => true, :mask_only => true})
1259
+
1260
+ assert_equal(9216.0, comp1.area)
1261
+ assert_equal(16, comp1.rect.x)
1262
+ assert_equal(16, comp1.rect.y)
1263
+ assert_equal(224, comp1.rect.width)
1264
+ assert_equal(96, comp1.rect.height)
1265
+ assert_cvscalar_equal(CvScalar.new(0, 0, 0, 0), comp1.value)
1266
+
1267
+ assert_equal(20480.0, comp2.area)
1268
+ assert_equal(0, comp2.rect.x)
1269
+ assert_equal(0, comp2.rect.y)
1270
+ assert_equal(256, comp2.rect.width)
1271
+ assert_equal(128, comp2.rect.height)
1272
+ assert_cvscalar_equal(CvScalar.new(0, 0, 0, 0), comp2.value)
1273
+
1274
+ assert_equal(21504.0, comp3.area)
1275
+ assert_equal(16, comp3.rect.x)
1276
+ assert_equal(16, comp3.rect.y)
1277
+ assert_equal(224, comp3.rect.width)
1278
+ assert_equal(96, comp3.rect.height)
1279
+ assert_cvscalar_equal(CvScalar.new(0, 0, 0, 0), comp3.value)
1280
+
1281
+ assert_equal(21504.0, comp4.area)
1282
+ assert_equal(16, comp4.rect.x)
1283
+ assert_equal(16, comp4.rect.y)
1284
+ assert_equal(224, comp4.rect.width)
1285
+ assert_equal(96, comp4.rect.height)
1286
+ assert_cvscalar_equal(CvScalar.new(228, 0, 0, 0), comp4.value)
1287
+
1288
+ assert_equal(21504.0, comp5.area)
1289
+ assert_equal(16, comp5.rect.x)
1290
+ assert_equal(16, comp5.rect.y)
1291
+ assert_equal(224, comp5.rect.width)
1292
+ assert_equal(96, comp5.rect.height)
1293
+ assert_cvscalar_equal(CvScalar.new(228, 0, 0, 0), comp5.value)
1294
+
1295
+ assert_raise(TypeError) {
1296
+ mat0.flood_fill(DUMMY_OBJ, 0)
1297
+ }
1298
+ assert_raise(TypeError) {
1299
+ mat0.flood_fill(point, DUMMY_OBJ)
1300
+ }
1301
+ assert_raise(TypeError) {
1302
+ mat0.flood_fill(point, 0, DUMMY_OBJ)
1303
+ }
1304
+ assert_raise(TypeError) {
1305
+ mat0.flood_fill(point, 0, CvScalar.new(0), DUMMY_OBJ)
1306
+ }
1307
+ assert_raise(TypeError) {
1308
+ mat0.flood_fill(point, 0, CvScalar.new(0), CvScalar.new(64), DUMMY_OBJ)
1309
+ }
1310
+ end
1311
+
1312
+ def test_find_contours
1313
+ mat0 = CvMat.load(FILENAME_CONTOURS, CV_LOAD_IMAGE_GRAYSCALE)
1314
+
1315
+ # Make binary image
1316
+ mat0.height.times { |j|
1317
+ mat0.width.times { |i|
1318
+ mat0[j, i] = (mat0[j, i][0] < 128) ? CvColor::Black : CvColor::White
1319
+ }
1320
+ }
1321
+
1322
+ [mat0.find_contours, mat0.find_contours(:mode => CV_RETR_LIST),
1323
+ mat0.find_contours(:method => CV_CHAIN_APPROX_SIMPLE),
1324
+ mat0.find_contours(:mode => CV_RETR_LIST, :method => CV_CHAIN_APPROX_SIMPLE)].each { |contours|
1325
+ assert_not_nil(contours)
1326
+ assert_equal(8, contours.total)
1327
+ assert_not_nil(contours.h_next)
1328
+ assert_equal(4, contours.h_next.total)
1329
+ assert_not_nil(contours.h_next.h_next)
1330
+ assert_equal(8, contours.h_next.h_next.total)
1331
+ assert_not_nil(contours.h_next.h_next.h_next)
1332
+ assert_equal(4, contours.h_next.h_next.h_next.total)
1333
+ assert_nil(contours.v_next)
1334
+ assert_nil(contours.h_next.v_next)
1335
+ assert_nil(contours.h_next.h_next.v_next)
1336
+ assert_nil(contours.h_next.h_next.h_next.v_next)
1337
+ }
1338
+
1339
+ contours = mat0.find_contours(:mode => CV_RETR_TREE)
1340
+ assert_not_nil(contours)
1341
+ assert_equal(4, contours.total)
1342
+ assert_not_nil(contours.v_next)
1343
+ assert_equal(8, contours.v_next.total)
1344
+ assert_nil(contours.v_next.v_next)
1345
+ assert_not_nil(contours.h_next)
1346
+ assert_equal(4, contours.h_next.total)
1347
+ assert_not_nil(contours.h_next.v_next)
1348
+ assert_equal(8, contours.h_next.v_next.total)
1349
+ assert_nil(contours.h_next.v_next.v_next)
1350
+
1351
+ contours = mat0.find_contours(:mode => CV_RETR_CCOMP)
1352
+ assert_not_nil(contours)
1353
+ assert_equal(4, contours.total)
1354
+ assert_not_nil(contours.v_next)
1355
+ assert_equal(8, contours.v_next.total)
1356
+ assert_nil(contours.v_next.v_next)
1357
+ assert_not_nil(contours.h_next)
1358
+ assert_equal(4, contours.h_next.total)
1359
+ assert_not_nil(contours.h_next.v_next)
1360
+ assert_equal(8, contours.h_next.v_next.total)
1361
+ assert_nil(contours.h_next.v_next.v_next)
1362
+
1363
+ contours = mat0.find_contours(:mode => CV_RETR_EXTERNAL)
1364
+ assert_not_nil(contours)
1365
+ assert_equal(4, contours.total)
1366
+ assert_nil(contours.v_next)
1367
+ assert_not_nil(contours.h_next)
1368
+ assert_equal(4, contours.h_next.total)
1369
+ assert_nil(contours.h_next.v_next)
1370
+
1371
+ contours = mat0.find_contours(:mode => CV_RETR_TREE, :method => CV_CHAIN_APPROX_NONE)
1372
+ assert_not_nil(contours)
1373
+ assert_equal(474, contours.total)
1374
+ assert_not_nil(contours.v_next)
1375
+ assert_equal(318, contours.v_next.total)
1376
+ assert_nil(contours.v_next.v_next)
1377
+ assert_not_nil(contours.h_next)
1378
+ assert_equal(396, contours.h_next.total)
1379
+ assert_not_nil(contours.h_next.v_next)
1380
+ assert_equal(240, contours.h_next.v_next.total)
1381
+ assert_nil(contours.h_next.v_next.v_next)
1382
+
1383
+ contours = mat0.find_contours(:mode => CV_RETR_EXTERNAL, :method => CV_CHAIN_CODE)
1384
+ assert_equal(474, contours.total)
1385
+ assert_equal(396, contours.h_next.total)
1386
+
1387
+ contours = mat0.find_contours(:mode => CV_RETR_EXTERNAL, :method => CV_CHAIN_APPROX_TC89_L1)
1388
+ assert_equal(4, contours.total)
1389
+ assert_equal(4, contours.h_next.total)
1390
+
1391
+ contours = mat0.find_contours(:mode => CV_RETR_EXTERNAL, :method => CV_CHAIN_APPROX_TC89_KCOS)
1392
+ assert_equal(4, contours.total)
1393
+ assert_equal(4, contours.h_next.total)
1394
+
1395
+ assert_raise(TypeError) {
1396
+ mat0.find_contours(DUMMY_OBJ)
1397
+ }
1398
+ assert_raise(CvStsUnsupportedFormat) {
1399
+ CvMat.new(10, 10, :cv32f, 3).find_contours
1400
+ }
1401
+ end
1402
+
1403
+ def test_pyr_mean_shift_filtering
1404
+ mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH)
1405
+ mat1 = mat0.pyr_mean_shift_filtering(30, 30)
1406
+ mat2 = mat0.pyr_mean_shift_filtering(30, 30, 2)
1407
+ mat3 = mat0.pyr_mean_shift_filtering(30, 30, nil, CvTermCriteria.new(3, 0.01))
1408
+
1409
+ [mat1, mat2, mat3].each { |mat|
1410
+ b, g, r = color_hists(mat)
1411
+ assert_in_delta(6900000, b, 100000)
1412
+ assert_in_delta(6500000, g, 100000)
1413
+ assert_in_delta(11800000, r, 100000)
1414
+ }
1415
+
1416
+ assert_raise(TypeError) {
1417
+ mat0.pyr_mean_shift_filtering(DUMMY_OBJ, 30)
1418
+ }
1419
+ assert_raise(TypeError) {
1420
+ mat0.pyr_mean_shift_filtering(30, DUMMY_OBJ)
1421
+ }
1422
+ assert_raise(TypeError) {
1423
+ mat0.pyr_mean_shift_filtering(30, 30, 2, DUMMY_OBJ)
1424
+ }
1425
+ end
1426
+
1427
+ def test_watershed
1428
+ mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH)
1429
+ marker = CvMat.new(mat0.cols, mat0.rows, :cv32s, 1).set_zero
1430
+ marker[150, 150] = CvScalar.new(1, 1, 1, 1)
1431
+ marker[210, 210] = CvScalar.new(2, 2, 2, 2)
1432
+ marker[40, 90] = CvScalar.new(3, 3, 3, 3)
1433
+
1434
+ mat1 = mat0.watershed(marker)
1435
+ assert_equal('ee6bec03296039c8df1899d3edc4684e', hash_img(mat1))
1436
+
1437
+ assert_raise(TypeError) {
1438
+ mat0.watershed(DUMMY_OBJ)
1439
+ }
1440
+ end
1441
+
1442
+ def test_moments
1443
+ mat = create_cvmat(128, 128, :cv8u, 1) { |j, i|
1444
+ if j >= 32 and j < 96 and i >= 16 and i < 112
1445
+ CvScalar.new(0)
1446
+ elsif j >= 16 and j < 112 and i >= 16 and i < 112
1447
+ CvScalar.new(128)
1448
+ else
1449
+ CvScalar.new(255)
1450
+ end
1451
+ }
1452
+
1453
+ moments1 = mat.moments
1454
+ moments2 = mat.moments(false)
1455
+ moments3 = mat.moments(true)
1456
+
1457
+ [moments1, moments2].each { |m|
1458
+ assert_in_delta(2221056, m.spatial(0, 0), 0.1)
1459
+ assert_in_delta(2221056, m.central(0, 0), 0.1)
1460
+ assert_in_delta(1, m.normalized_central(0, 0), 0.1)
1461
+
1462
+ hu_moments = m.hu
1463
+ assert_equal(CvHuMoments, hu_moments.class)
1464
+ assert_in_delta(0.001771, hu_moments.hu1, 0.000001)
1465
+ hu_moments.to_a[1..7].each { |hu|
1466
+ assert_in_delta(0.0, hu, 0.000001)
1467
+ }
1468
+
1469
+ center = m.gravity_center
1470
+ assert_equal(CvPoint2D32f, center.class)
1471
+ assert_in_delta(63.5, center.x, 0.001)
1472
+ assert_in_delta(63.5, center.y, 0.001)
1473
+
1474
+ assert_in_delta(0, m.angle, 0.001)
1475
+
1476
+ assert_in_delta(2221056, m.m00, 0.001)
1477
+ assert_in_delta(141037056, m.m10, 0.001)
1478
+ assert_in_delta(141037056, m.m01, 0.001)
1479
+ assert_in_delta(13157049856, m.m20, 0.001)
1480
+ assert_in_delta(8955853056, m.m11, 0.001)
1481
+ assert_in_delta(13492594176, m.m02, 0.001)
1482
+ assert_in_delta(1369024659456, m.m30, 0.001)
1483
+ assert_in_delta(835472665856, m.m21, 0.001)
1484
+ assert_in_delta(856779730176, m.m12, 0.001)
1485
+ assert_in_delta(1432945852416, m.m03, 0.001)
1486
+ assert_in_delta(4201196800, m.mu20, 0.001)
1487
+ assert_in_delta(0, m.mu11, 0.001)
1488
+ assert_in_delta(4536741120, m.mu02, 0.001)
1489
+ assert_in_delta(0, m.mu30, 0.001)
1490
+ assert_in_delta(0, m.mu21, 0.001)
1491
+ assert_in_delta(0, m.mu12, 0.001)
1492
+ assert_in_delta(0, m.mu03, 0.001)
1493
+ assert_in_delta(0.000671, m.inv_sqrt_m00, 0.000001)
1494
+ }
1495
+
1496
+ m = moments3
1497
+ assert_in_delta(10240, m.spatial(0, 0), 0.1)
1498
+ assert_in_delta(10240, m.central(0, 0), 0.1)
1499
+ assert_in_delta(1, m.normalized_central(0, 0), 0.1)
1500
+
1501
+ hu_moments = m.hu
1502
+ assert_equal(CvHuMoments, hu_moments.class)
1503
+ assert_in_delta(0.361650, hu_moments.hu1, 0.000001)
1504
+ assert_in_delta(0.000625, hu_moments.hu2, 0.000001)
1505
+ hu_moments.to_a[2..7].each { |hu|
1506
+ assert_in_delta(0.0, hu, 0.000001)
1507
+ }
1508
+
1509
+ center = m.gravity_center
1510
+ assert_equal(CvPoint2D32f, center.class)
1511
+ assert_in_delta(63.5, center.x, 0.001)
1512
+ assert_in_delta(63.5, center.y, 0.001)
1513
+
1514
+ assert_in_delta(0, m.angle, 0.001)
1515
+
1516
+ assert_in_delta(10240, m.m00, 0.001)
1517
+ assert_in_delta(650240, m.m10, 0.001)
1518
+ assert_in_delta(650240, m.m01, 0.001)
1519
+ assert_in_delta(58940416, m.m20, 0.001)
1520
+ assert_in_delta(41290240, m.m11, 0.001)
1521
+ assert_in_delta(61561856, m.m02, 0.001)
1522
+ assert_in_delta(5984288768, m.m30, 0.001)
1523
+ assert_in_delta(3742716416, m.m21, 0.001)
1524
+ assert_in_delta(3909177856, m.m12, 0.001)
1525
+ assert_in_delta(6483673088, m.m03, 0.001)
1526
+ assert_in_delta(17650176, m.mu20, 0.001)
1527
+ assert_in_delta(0, m.mu11, 0.001)
1528
+ assert_in_delta(20271616, m.mu02, 0.001)
1529
+ assert_in_delta(0, m.mu30, 0.001)
1530
+ assert_in_delta(0, m.mu21, 0.001)
1531
+ assert_in_delta(0, m.mu12, 0.001)
1532
+ assert_in_delta(0, m.mu03, 0.001)
1533
+ assert_in_delta(0.009882, m.inv_sqrt_m00, 0.000001)
1534
+ end
1535
+
1536
+ def test_hough_lines
1537
+ mat0 = CvMat.load(FILENAME_LINES, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH)
1538
+ # make a binary image
1539
+ mat = CvMat.new(mat0.rows, mat0.cols, :cv8u, 1)
1540
+ (mat0.rows * mat0.cols).times { |i|
1541
+ mat[i] = (mat0[i][0] <= 100) ? CvScalar.new(0) : CvScalar.new(255);
1542
+ }
1543
+
1544
+ [CV_HOUGH_STANDARD, :standard].each { |method|
1545
+ seq = mat.hough_lines(method, 1, Math::PI / 180, 65)
1546
+ assert_equal(4, seq.size)
1547
+ }
1548
+
1549
+ [CV_HOUGH_PROBABILISTIC, :probabilistic].each { |method|
1550
+ seq = mat.hough_lines(method, 1, Math::PI / 180, 40, 30, 10)
1551
+ assert_equal(4, seq.size)
1552
+ }
1553
+
1554
+ # [CV_HOUGH_MULTI_SCALE, :multi_scale].each { |method|
1555
+ # seq = mat.hough_lines(method, 1, Math::PI / 180, 40, 2, 3)
1556
+ # assert_equal(9, seq.size)
1557
+ # }
1558
+
1559
+ assert_raise(TypeError) {
1560
+ mat.hough_lines(DUMMY_OBJ, 1, Math::PI / 180, 40, 2, 3)
1561
+ }
1562
+ assert_raise(TypeError) {
1563
+ mat.hough_lines(CV_HOUGH_STANDARD, DUMMY_OBJ, Math::PI / 180, 40, 2, 3)
1564
+ }
1565
+ assert_raise(TypeError) {
1566
+ mat.hough_lines(CV_HOUGH_STANDARD, 1, DUMMY_OBJ, 40, 2, 3)
1567
+ }
1568
+ assert_raise(TypeError) {
1569
+ mat.hough_lines(CV_HOUGH_STANDARD, 1, Math::PI / 180, DUMMY_OBJ, 2, 3)
1570
+ }
1571
+ assert_raise(TypeError) {
1572
+ mat.hough_lines(CV_HOUGH_STANDARD, 1, Math::PI / 180, 40, DUMMY_OBJ, 3)
1573
+ }
1574
+ assert_raise(TypeError) {
1575
+ mat.hough_lines(CV_HOUGH_STANDARD, 1, Math::PI / 180, 40, 2, DUMMY_OBJ)
1576
+ }
1577
+ assert_raise(ArgumentError) {
1578
+ mat.hough_lines(:dummy, 1, Math::PI / 180, 40, 2, DUMMY_OBJ)
1579
+ }
1580
+ assert_raise(CvStsBadArg) {
1581
+ CvMat.new(10, 10, :cv32f, 3).hough_lines(:standard, 1, Math::PI / 180, 65)
1582
+ }
1583
+ end
1584
+
1585
+ def test_hough_circles
1586
+ mat0 = CvMat.load(FILENAME_LINES, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH)
1587
+ # make a binary image
1588
+ mat = CvMat.new(mat0.rows, mat0.cols, :cv8u, 1)
1589
+ (mat0.rows * mat0.cols).times { |i|
1590
+ mat[i] = (mat0[i][0] <= 100) ? CvScalar.new(0) : CvScalar.new(255);
1591
+ }
1592
+
1593
+ [mat.hough_circles(CV_HOUGH_GRADIENT, 1.5, 40, 100, 40, 10, 50),
1594
+ mat.hough_circles(:gradient, 1.5, 40, 100, 40, 10, 50),
1595
+ mat.hough_circles(CV_HOUGH_GRADIENT, 1.5, 40, 100, 40),
1596
+ mat.hough_circles(:gradient, 1.5, 40, 100, 40)].each { |seq|
1597
+ assert_equal(2, seq.size)
1598
+ }
1599
+
1600
+ # Uncomment the following lines to show the result
1601
+ # seq = mat.hough_circles(:gradient, 1.5, 40, 100, 40, 10, 50)
1602
+ # seq.each { |circle|
1603
+ # mat0.circle!(circle.center, circle.radius, :color => CvColor::Red, :thickness => 2)
1604
+ # }
1605
+ # snap mat0
1606
+
1607
+ assert_raise(TypeError) {
1608
+ mat.hough_circles(DUMMY_OBJ, 1.5, 40, 100, 50, 10, 50)
1609
+ }
1610
+ assert_raise(TypeError) {
1611
+ mat.hough_circles(CV_HOUGH_GRADIENT, DUMMY_OBJ, 40, 100, 50, 10, 50)
1612
+ }
1613
+ assert_raise(TypeError) {
1614
+ mat.hough_circles(CV_HOUGH_GRADIENT, 1.5, DUMMY_OBJ, 100, 50, 10, 50)
1615
+ }
1616
+ assert_raise(TypeError) {
1617
+ mat.hough_circles(CV_HOUGH_GRADIENT, 1.5, 40, DUMMY_OBJ, 50, 10, 50)
1618
+ }
1619
+ assert_raise(TypeError) {
1620
+ mat.hough_circles(CV_HOUGH_GRADIENT, 1.5, 40, 100, DUMMY_OBJ, 10, 50)
1621
+ }
1622
+ assert_raise(TypeError) {
1623
+ mat.hough_circles(CV_HOUGH_GRADIENT, 1.5, 40, 100, 50, DUMMY_OBJ, 50)
1624
+ }
1625
+ assert_raise(TypeError) {
1626
+ mat.hough_circles(CV_HOUGH_GRADIENT, 1.5, 40, 100, 50, 10, DUMMY_OBJ)
1627
+ }
1628
+ assert_raise(ArgumentError) {
1629
+ mat.hough_circles(:dummy, 1.5, 40, 100, 50, 10, DUMMY_OBJ)
1630
+ }
1631
+ assert_raise(CvStsBadArg) {
1632
+ CvMat.new(10, 10, :cv32f, 3).hough_circles(:gradient, 1.5, 40, 100, 50, 10, 50)
1633
+ }
1634
+ end
1635
+
1636
+ def test_inpaint
1637
+ mat = CvMat.load(FILENAME_LENA_INPAINT, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH)
1638
+ mask = CvMat.load(FILENAME_INPAINT_MASK, CV_LOAD_IMAGE_GRAYSCALE)
1639
+
1640
+ [CV_INPAINT_NS, :ns].each { |method|
1641
+ result_ns = mat.inpaint(method, mask, 10)
1642
+ assert_in_delta(14000, count_threshold(result_ns, 128), 1000)
1643
+ }
1644
+ [CV_INPAINT_TELEA, :telea].each { |method|
1645
+ result_telea = mat.inpaint(method, mask, 10)
1646
+ assert_in_delta(13500, count_threshold(result_telea, 128), 1000)
1647
+ }
1648
+
1649
+ # Uncomment the following lines to show the results
1650
+ # result_ns = mat.inpaint(:ns, mask, 10)
1651
+ # result_telea = mat.inpaint(:telea, mask, 10)
1652
+ # snap mat, result_ns, result_telea
1653
+
1654
+ assert_raise(TypeError) {
1655
+ mat.inpaint(DUMMY_OBJ, mask, 10)
1656
+ }
1657
+ assert_raise(TypeError) {
1658
+ mat.inpaint(:ns, DUMMY_OBJ, 10)
1659
+ }
1660
+ assert_raise(TypeError) {
1661
+ mat.inpaint(:ns, mask, DUMMY_OBJ)
1662
+ }
1663
+ assert_raise(ArgumentError) {
1664
+ mat.inpaint(:dummy, mask, 10)
1665
+ }
1666
+ assert_raise(CvStsUnsupportedFormat) {
1667
+ CvMat.new(10, 10, :cv32f, 3).inpaint(:ns, CvMat.new(10, 10, :cv8u, 1), 10)
1668
+ }
1669
+ end
1670
+
1671
+ def test_equalize_hist
1672
+ mat = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_GRAYSCALE)
1673
+ result = mat.equalize_hist
1674
+ assert_equal(CvMat, result.class)
1675
+ assert_equal(mat.rows, result.rows)
1676
+ assert_equal(mat.cols, result.cols)
1677
+
1678
+ assert_raise(CvStsAssert) {
1679
+ CvMat.new(10, 10, :cv32f, 3).equalize_hist
1680
+ }
1681
+
1682
+ # Uncomment the following lines to show the result
1683
+ # snap mat, result
1684
+ end
1685
+
1686
+ def test_match_template
1687
+ mat = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH)
1688
+ templ = CvMat.load(FILENAME_LENA_EYES, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH)
1689
+
1690
+ expected_pt = CvPoint.new(100, 120)
1691
+
1692
+ # sqdiff
1693
+ result = mat.match_template(templ)
1694
+ pt = result.min_max_loc[2]
1695
+ assert_in_delta(expected_pt.x, pt.x, 20)
1696
+ assert_in_delta(expected_pt.y, pt.y, 20)
1697
+
1698
+ [CV_TM_SQDIFF, :sqdiff].each { |method|
1699
+ result = mat.match_template(templ, method)
1700
+ assert_in_delta(expected_pt.x, pt.x, 20)
1701
+ assert_in_delta(expected_pt.y, pt.y, 20)
1702
+ }
1703
+
1704
+ # sqdiff_normed
1705
+ [CV_TM_SQDIFF_NORMED, :sqdiff_normed].each { |method|
1706
+ result = mat.match_template(templ, method)
1707
+ pt = result.min_max_loc[2]
1708
+ assert_in_delta(expected_pt.x, pt.x, 20)
1709
+ assert_in_delta(expected_pt.y, pt.y, 20)
1710
+ }
1711
+
1712
+ # ccorr
1713
+ [CV_TM_CCORR, :ccorr].each { |method|
1714
+ result = mat.match_template(templ, method)
1715
+ pt = result.min_max_loc[3]
1716
+ assert_in_delta(110, pt.x, 20)
1717
+ assert_in_delta(60, pt.y, 20)
1718
+ }
1719
+
1720
+ # ccorr_normed
1721
+ [CV_TM_CCORR_NORMED, :ccorr_normed].each { |method|
1722
+ result = mat.match_template(templ, method)
1723
+ pt = result.min_max_loc[3]
1724
+ assert_in_delta(expected_pt.x, pt.x, 20)
1725
+ assert_in_delta(expected_pt.y, pt.y, 20)
1726
+ }
1727
+
1728
+ # ccoeff
1729
+ [CV_TM_CCOEFF, :ccoeff].each { |method|
1730
+ result = mat.match_template(templ, method)
1731
+ pt = result.min_max_loc[3]
1732
+ assert_in_delta(expected_pt.x, pt.x, 20)
1733
+ assert_in_delta(expected_pt.y, pt.y, 20)
1734
+ }
1735
+
1736
+ # ccoeff_normed
1737
+ [CV_TM_CCOEFF_NORMED, :ccoeff_normed].each { |method|
1738
+ result = mat.match_template(templ, method)
1739
+ pt = result.min_max_loc[3]
1740
+ assert_in_delta(expected_pt.x, pt.x, 20)
1741
+ assert_in_delta(expected_pt.y, pt.y, 20)
1742
+ }
1743
+
1744
+ # Uncomment the following lines to show the result
1745
+ # result = mat.match_template(templ)
1746
+ # pt1 = result.min_max_loc[2] # minimum location
1747
+ # pt2 = CvPoint.new(pt1.x + templ.width, pt1.y + templ.height)
1748
+ # mat.rectangle!(pt1, pt2, :color => CvColor::Black, :thickness => 3)
1749
+ # snap mat, templ, result
1750
+
1751
+ assert_raise(TypeError) {
1752
+ mat.match_template(DUMMY_OBJ)
1753
+ }
1754
+ assert_raise(TypeError) {
1755
+ mat.match_template(templ, DUMMY_OBJ)
1756
+ }
1757
+ end
1758
+
1759
+ def test_match_shapes
1760
+ mat_cv = CvMat.load(FILENAME_STR_CV, CV_LOAD_IMAGE_GRAYSCALE)
1761
+ mat_ov = CvMat.load(FILENAME_STR_OV, CV_LOAD_IMAGE_GRAYSCALE)
1762
+ mat_cv_rotated = CvMat.load(FILENAME_STR_CV_ROTATED, CV_LOAD_IMAGE_GRAYSCALE)
1763
+
1764
+ [CV_CONTOURS_MATCH_I1, :i1].each { |method|
1765
+ assert_in_delta(0, mat_cv.match_shapes(mat_cv_rotated, method), 0.00001)
1766
+ assert_in_delta(0.0010649, mat_cv.match_shapes(mat_ov, method), 0.00001)
1767
+ }
1768
+
1769
+ [CV_CONTOURS_MATCH_I2, :i2].each { |method|
1770
+ assert_in_delta(0, mat_cv.match_shapes(mat_cv_rotated, method), 0.00001)
1771
+ assert_in_delta(0.0104650, mat_cv.match_shapes(mat_ov, method), 0.00001)
1772
+ }
1773
+
1774
+ [CV_CONTOURS_MATCH_I3, :i3].each { |method|
1775
+ assert_in_delta(0, mat_cv.match_shapes(mat_cv_rotated, method), 0.00001)
1776
+ assert_in_delta(0.0033327, mat_cv.match_shapes(mat_ov, method), 0.00001)
1777
+ }
1778
+ end
1779
+
1780
+ def test_mean_shift
1781
+ flunk('FIXME: CvMat#mean_shift is not tested yet.')
1782
+ end
1783
+
1784
+ def test_cam_shift
1785
+ flunk('FIXME: CvMat#cam_shift is not tested yet.')
1786
+ end
1787
+
1788
+ def test_snake_image
1789
+ radius = 40
1790
+ center = CvPoint.new(128, 128)
1791
+ mat = CvMat.new(center.y * 2, center.x * 2, :cv8u, 1).zero!
1792
+ mat.circle!(center, radius, :color => CvColor::White, :thickness => -1)
1793
+
1794
+ num_points = 10
1795
+ alpha = 0.05
1796
+ beta = 0.05
1797
+ gamma = 0.9
1798
+
1799
+ arr_alpha = [alpha] * num_points
1800
+ arr_beta = [beta] * num_points
1801
+ arr_gamma = [gamma] * num_points
1802
+ size = CvSize.new(3, 3)
1803
+ term_criteria = CvTermCriteria.new(100, num_points / 2)
1804
+
1805
+ # initialize contours
1806
+ points = []
1807
+ num_points.times { |i|
1808
+ x = center.x * Math.cos(2 * Math::PI * i / num_points) + center.x
1809
+ y = center.y * Math.sin(2 * Math::PI * i / num_points) + center.y
1810
+ points << CvPoint.new(x, y)
1811
+ }
1812
+
1813
+ acceptable_error = 50
1814
+
1815
+ # test snake_image
1816
+ # calc_gradient = true
1817
+ [mat.snake_image(points, alpha, beta, gamma, size, term_criteria),
1818
+ mat.snake_image(points, alpha, beta, gamma, size, term_criteria, true),
1819
+ mat.snake_image(points, arr_alpha, arr_beta, arr_gamma, size, term_criteria),
1820
+ mat.snake_image(points, arr_alpha, arr_beta, arr_gamma, size, term_criteria, true)].each { |result|
1821
+ assert_equal(num_points, result.size)
1822
+ result.each { |pt|
1823
+ x = pt.x - center.x
1824
+ y = pt.y - center.y
1825
+ error = Math.sqrt((x * x + y * y - radius * radius).abs)
1826
+ assert(error < acceptable_error)
1827
+ }
1828
+ }
1829
+
1830
+ # calc_gradient = false
1831
+ [mat.snake_image(points, alpha, beta, gamma, size, term_criteria, false),
1832
+ mat.snake_image(points, arr_alpha, arr_beta, arr_gamma, size, term_criteria, false)].each { |result|
1833
+ expected_points = [[149, 102], [139, 144], [95, 144], [56, 124], [17, 105],
1834
+ [25, 61], [63, 39], [101, 17], [145, 17], [158, 59]]
1835
+ assert_equal(num_points, result.size)
1836
+ result.each { |pt|
1837
+ x = pt.x - center.x
1838
+ y = pt.y - center.y
1839
+ error = Math.sqrt((x * x + y * y - radius * radius).abs)
1840
+ assert(error < acceptable_error)
1841
+ }
1842
+ }
1843
+
1844
+ # raise error
1845
+ assert_raise(TypeError) {
1846
+ mat.snake_image(DUMMY_OBJ, arr_alpha, arr_beta, arr_gamma, size, term_criteria)
1847
+ }
1848
+ assert_raise(TypeError) {
1849
+ mat.snake_image(points, DUMMY_OBJ, arr_beta, arr_gamma, size, term_criteria)
1850
+ }
1851
+ assert_raise(TypeError) {
1852
+ mat.snake_image(points, arr_alpha, DUMMY_OBJ, arr_gamma, size, term_criteria)
1853
+ }
1854
+ assert_raise(TypeError) {
1855
+ mat.snake_image(points, arr_alpha, arr_beta, DUMMY_OBJ, size, term_criteria)
1856
+ }
1857
+ assert_raise(TypeError) {
1858
+ mat.snake_image(points, arr_alpha, arr_beta, arr_gamma, DUMMY_OBJ, term_criteria)
1859
+ }
1860
+ assert_raise(TypeError) {
1861
+ mat.snake_image(points, arr_alpha, arr_beta, arr_gamma, size, DUMMY_OBJ)
1862
+ }
1863
+ mat.snake_image(points, arr_alpha, arr_beta, arr_gamma, size, term_criteria, DUMMY_OBJ)
1864
+
1865
+ assert_raise(ArgumentError) {
1866
+ mat.snake_image(points, arr_alpha[0 .. num_points / 2], arr_beta, arr_gamma, size, term_criteria)
1867
+ }
1868
+ assert_raise(CvBadNumChannels) {
1869
+ CvMat.new(10, 10, :cv8u, 3).snake_image(points, alpha, beta, gamma, size, term_criteria)
1870
+ }
1871
+
1872
+ # Uncomment the following lines to show the result
1873
+ # result = mat.clone.GRAY2BGR
1874
+ # pts = mat.snake_image(points, alpha, beta, gamma, size, term_criteria)
1875
+ # w = GUI::Window.new('HoughCircle')
1876
+ # result.poly_line!([pts], :color => CvColor::Red, :is_closed => true, :thickness => 2)
1877
+ # result.poly_line!([points], :color => CvColor::Yellow, :is_closed => true, :thickness => 2)
1878
+ # w.show result
1879
+ # GUI::wait_key
1880
+ end
1881
+
1882
+ def test_optical_flow_hs
1883
+ size = 128
1884
+ prev = create_cvmat(size, size, :cv8u, 1) { |j, i|
1885
+ if ((i - (size / 2)) ** 2 ) + ((j - (size / 2)) ** 2 ) < size
1886
+ CvColor::Black
1887
+ else
1888
+ CvColor::White
1889
+ end
1890
+ }
1891
+ curr = create_cvmat(size, size, :cv8u, 1) { |j, i|
1892
+ if ((i - (size / 2) - 10) ** 2) + ((j - (size / 2) - 7) ** 2 ) < size
1893
+ CvColor::Black
1894
+ else
1895
+ CvColor::White
1896
+ end
1897
+ }
1898
+
1899
+ [curr.optical_flow_hs(prev, nil, nil, :lambda => 0.0005, :criteria => CvTermCriteria.new(1, 0.001)),
1900
+ curr.optical_flow_hs(prev)].each { |velx, vely|
1901
+ assert_in_delta(60, count_threshold(velx, 1), 20)
1902
+ assert_in_delta(50, count_threshold(vely, 1), 20)
1903
+ }
1904
+
1905
+ velx, vely = curr.optical_flow_hs(prev, nil, nil, :lambda => 0.001)
1906
+ assert_in_delta(60, count_threshold(velx, 1), 20)
1907
+ assert_in_delta(50, count_threshold(vely, 1), 20)
1908
+
1909
+ velx, vely = curr.optical_flow_hs(prev, nil, nil, :criteria => CvTermCriteria.new(10, 0.01))
1910
+ assert_in_delta(130, count_threshold(velx, 1), 20)
1911
+ assert_in_delta(110, count_threshold(vely, 1), 20)
1912
+
1913
+ prev_velx, prev_vely = curr.optical_flow_hs(prev)
1914
+ velx, vely = curr.optical_flow_hs(prev, prev_velx, prev_vely)
1915
+ assert_in_delta(70, count_threshold(velx, 1), 20)
1916
+ assert_in_delta(60, count_threshold(vely, 1), 20)
1917
+
1918
+ velx, vely = curr.optical_flow_hs(prev, prev_velx, prev_vely, :lambda => 0.001)
1919
+ assert_in_delta(80, count_threshold(velx, 1), 20)
1920
+ assert_in_delta(70, count_threshold(vely, 1), 20)
1921
+
1922
+ velx, vely = curr.optical_flow_hs(prev, prev_velx, prev_vely, :criteria => CvTermCriteria.new(10, 0.01))
1923
+ assert_in_delta(150, count_threshold(velx, 1), 20)
1924
+ assert_in_delta(130, count_threshold(vely, 1), 20)
1925
+
1926
+ assert_raise(TypeError) {
1927
+ curr.optical_flow_hs(DUMMY_OBJ)
1928
+ }
1929
+ assert_raise(TypeError) {
1930
+ curr.optical_flow_hs(prev, DUMMY_OBJ, prev_vely)
1931
+ }
1932
+ assert_raise(TypeError) {
1933
+ curr.optical_flow_hs(prev, prev_velx, DUMMY_OBJ)
1934
+ }
1935
+ assert_raise(TypeError) {
1936
+ curr.optical_flow_hs(prev, prev_velx, prev_vely, DUMMY_OBJ)
1937
+ }
1938
+ assert_raise(CvStsUnmatchedFormats) {
1939
+ CvMat.new(10, 10, :cv8u, 3).optical_flow_hs(prev)
1940
+ }
1941
+ end
1942
+
1943
+ def test_optical_flow_lk
1944
+ size = 128
1945
+ prev = create_cvmat(size, size, :cv8u, 1) { |j, i|
1946
+ if ((i - (size / 2)) ** 2 ) + ((j - (size / 2)) ** 2 ) < size
1947
+ CvColor::Black
1948
+ else
1949
+ CvColor::White
1950
+ end
1951
+ }
1952
+ curr = create_cvmat(size, size, :cv8u, 1) { |j, i|
1953
+ if ((i - (size / 2) - 10) ** 2) + ((j - (size / 2) - 7) ** 2 ) < size
1954
+ CvColor::Black
1955
+ else
1956
+ CvColor::White
1957
+ end
1958
+ }
1959
+
1960
+ velx, vely = curr.optical_flow_lk(prev, CvSize.new(3, 3))
1961
+ assert_in_delta(100, count_threshold(velx, 1), 20)
1962
+ assert_in_delta(90, count_threshold(vely, 1), 20)
1963
+
1964
+ velx, vely = curr.optical_flow_lk(prev, CvSize.new(5, 5))
1965
+ assert_in_delta(180, count_threshold(velx, 1), 20)
1966
+ assert_in_delta(150, count_threshold(vely, 1), 20)
1967
+
1968
+ assert_raise(TypeError) {
1969
+ curr.optical_flow_lk(DUMMY_OBJ, CvSize.new(3, 3))
1970
+ }
1971
+ assert_raise(TypeError) {
1972
+ curr.optical_flow_lk(prev, DUMMY_OBJ)
1973
+ }
1974
+ assert_raise(CvStsUnmatchedFormats) {
1975
+ CvMat.new(10, 10, :cv8u, 3).optical_flow_lk(prev, CvSize.new(3, 3))
1976
+ }
1977
+ end
1978
+
1979
+ def test_optical_flow_bm
1980
+ size = 128
1981
+ prev = create_cvmat(size, size, :cv8u, 1) { |j, i|
1982
+ if ((i - (size / 2)) ** 2 ) + ((j - (size / 2)) ** 2 ) < size
1983
+ CvColor::Black
1984
+ else
1985
+ CvColor::White
1986
+ end
1987
+ }
1988
+ curr = create_cvmat(size, size, :cv8u, 1) { |j, i|
1989
+ if ((i - (size / 2) - 10) ** 2) + ((j - (size / 2) - 7) ** 2 ) < size
1990
+ CvColor::Black
1991
+ else
1992
+ CvColor::White
1993
+ end
1994
+ }
1995
+
1996
+ [curr.optical_flow_bm(prev, nil, nil, :block_size => CvSize.new(4, 4),
1997
+ :shift_size => CvSize.new(1, 1), :max_range => CvSize.new(4, 4)),
1998
+ curr.optical_flow_bm(prev)].each { |velx, vely|
1999
+ assert_in_delta(350, count_threshold(velx, 1), 30)
2000
+ assert_in_delta(250, count_threshold(vely, 1), 30)
2001
+ }
2002
+
2003
+ velx, vely = curr.optical_flow_bm(prev, nil, nil, :block_size => CvSize.new(3, 3))
2004
+ assert_in_delta(280, count_threshold(velx, 1), 30)
2005
+ assert_in_delta(200, count_threshold(vely, 1), 30)
2006
+
2007
+ velx, vely = curr.optical_flow_bm(prev, nil, nil, :shift_size => CvSize.new(2, 2))
2008
+ assert_in_delta(80, count_threshold(velx, 1), 30)
2009
+ assert_in_delta(60, count_threshold(vely, 1), 30)
2010
+
2011
+ velx, vely = curr.optical_flow_bm(prev, nil, nil, :max_range => CvSize.new(5, 5))
2012
+ assert_in_delta(400, count_threshold(velx, 1), 30)
2013
+ assert_in_delta(300, count_threshold(vely, 1), 30)
2014
+
2015
+ prev_velx, prev_vely = curr.optical_flow_bm(prev)
2016
+ velx, vely = curr.optical_flow_bm(prev, prev_velx, prev_vely)
2017
+ assert_in_delta(350, count_threshold(velx, 1), 30)
2018
+ assert_in_delta(270, count_threshold(vely, 1), 30)
2019
+
2020
+ assert_raise(TypeError) {
2021
+ curr.optical_flow_bm(DUMMY_OBJ)
2022
+ }
2023
+ assert_raise(TypeError) {
2024
+ curr.optical_flow_bm(prev, DUMMY_OBJ, prev_vely)
2025
+ }
2026
+ assert_raise(TypeError) {
2027
+ curr.optical_flow_bm(prev, prev_velx, DUMMY_OBJ)
2028
+ }
2029
+ assert_raise(TypeError) {
2030
+ curr.optical_flow_bm(prev, prev_velx, prev_vely, DUMMY_OBJ)
2031
+ }
2032
+ assert_raise(CvStsUnmatchedFormats) {
2033
+ CvMat.new(10, 10, :cv8u, 3).optical_flow_bm(prev)
2034
+ }
2035
+ end
2036
+
2037
+ def test_extract_surf
2038
+ mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_GRAYSCALE)
2039
+
2040
+ # simple
2041
+ keypoints1, descriptors1 = mat0.extract_surf(CvSURFParams.new(500, true, 2, 3))
2042
+ assert_equal(CvSeq, keypoints1.class)
2043
+ assert_equal(254, keypoints1.size)
2044
+ assert_equal(Array, descriptors1.class)
2045
+ assert_equal(254, descriptors1.size)
2046
+ assert_equal(Array, descriptors1[0].class)
2047
+ assert_equal(128, descriptors1[0].size)
2048
+
2049
+ # use mask
2050
+ mask = create_cvmat(mat0.rows, mat0.cols, :cv8u, 1) { |j, i|
2051
+ if i < mat0.cols / 2
2052
+ CvScalar.new(1)
2053
+ else
2054
+ CvScalar.new(0)
2055
+ end
2056
+ }
2057
+ keypoints2, descriptors2 = mat0.extract_surf(CvSURFParams.new(500, false), mask)
2058
+ assert_equal(CvSeq, keypoints2.class)
2059
+ assert_equal(170, keypoints2.size)
2060
+ assert_equal(Array, descriptors2.class)
2061
+ assert_equal(170, descriptors2.size)
2062
+ assert_equal(Array, descriptors2[0].class)
2063
+ assert_equal(64, descriptors2[0].size)
2064
+
2065
+ # raise exceptions because of invalid arguments
2066
+ assert_raise(TypeError) {
2067
+ mat0.extract_surf(DUMMY_OBJ)
2068
+ }
2069
+ assert_raise(TypeError) {
2070
+ mat0.extract_surf(CvSURFParams.new(500), DUMMY_OBJ)
2071
+ }
2072
+
2073
+ # Uncomment the following lines to show the result
2074
+ # results = []
2075
+ # [keypoints1, keypoints2].each { |kpts|
2076
+ # tmp = mat0.GRAY2BGR
2077
+ # kpts.each { |kp|
2078
+ # tmp.circle!(kp.pt, 3, :color => CvColor::Red, :thickness => 1, :line_type => :aa)
2079
+ # }
2080
+ # results << tmp
2081
+ # }
2082
+ # snap mat0, *results
2083
+ end
2084
+ end
2085
+