ruby-opencv 0.0.10-i386-mingw32

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