afeld-opencv 0.0.8

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