ruby-opencv 0.0.9.pre2-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (229) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +25 -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 +722 -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/opencv.rb +12 -0
  136. data/lib/opencv/psyched_yaml.rb +22 -0
  137. data/lib/opencv/version.rb +3 -0
  138. data/ruby-opencv.gemspec +44 -0
  139. data/test/helper.rb +166 -0
  140. data/test/runner.rb +30 -0
  141. data/test/samples/airplane.jpg +0 -0
  142. data/test/samples/baboon.jpg +0 -0
  143. data/test/samples/baboon200.jpg +0 -0
  144. data/test/samples/baboon200_rotated.jpg +0 -0
  145. data/test/samples/blank0.jpg +0 -0
  146. data/test/samples/blank1.jpg +0 -0
  147. data/test/samples/blank2.jpg +0 -0
  148. data/test/samples/blank3.jpg +0 -0
  149. data/test/samples/blank4.jpg +0 -0
  150. data/test/samples/blank5.jpg +0 -0
  151. data/test/samples/blank6.jpg +0 -0
  152. data/test/samples/blank7.jpg +0 -0
  153. data/test/samples/blank8.jpg +0 -0
  154. data/test/samples/blank9.jpg +0 -0
  155. data/test/samples/cat.jpg +0 -0
  156. data/test/samples/chessboard.jpg +0 -0
  157. data/test/samples/contours.jpg +0 -0
  158. data/test/samples/fruits.jpg +0 -0
  159. data/test/samples/haarcascade_frontalface_alt.xml.gz +0 -0
  160. data/test/samples/inpaint-mask.bmp +0 -0
  161. data/test/samples/lena-256x256.jpg +0 -0
  162. data/test/samples/lena-32x32.jpg +0 -0
  163. data/test/samples/lena-eyes.jpg +0 -0
  164. data/test/samples/lena-inpaint.jpg +0 -0
  165. data/test/samples/lena.jpg +0 -0
  166. data/test/samples/lines.jpg +0 -0
  167. data/test/samples/messy0.jpg +0 -0
  168. data/test/samples/messy1.jpg +0 -0
  169. data/test/samples/movie_sample.avi +0 -0
  170. data/test/samples/one_way_train_0000.jpg +0 -0
  171. data/test/samples/one_way_train_0001.jpg +0 -0
  172. data/test/samples/partially_blank0.jpg +0 -0
  173. data/test/samples/partially_blank1.jpg +0 -0
  174. data/test/samples/smooth0.jpg +0 -0
  175. data/test/samples/smooth1.jpg +0 -0
  176. data/test/samples/smooth2.jpg +0 -0
  177. data/test/samples/smooth3.jpg +0 -0
  178. data/test/samples/smooth4.jpg +0 -0
  179. data/test/samples/smooth5.jpg +0 -0
  180. data/test/samples/smooth6.jpg +0 -0
  181. data/test/samples/str-cv-rotated.jpg +0 -0
  182. data/test/samples/str-cv.jpg +0 -0
  183. data/test/samples/str-ov.jpg +0 -0
  184. data/test/samples/stuff.jpg +0 -0
  185. data/test/test_curve.rb +43 -0
  186. data/test/test_cvavgcomp.rb +24 -0
  187. data/test/test_cvbox2d.rb +76 -0
  188. data/test/test_cvcapture.rb +183 -0
  189. data/test/test_cvchain.rb +108 -0
  190. data/test/test_cvcircle32f.rb +41 -0
  191. data/test/test_cvconnectedcomp.rb +61 -0
  192. data/test/test_cvcontour.rb +150 -0
  193. data/test/test_cvcontourtree.rb +43 -0
  194. data/test/test_cverror.rb +50 -0
  195. data/test/test_cvfeaturetree.rb +65 -0
  196. data/test/test_cvfont.rb +58 -0
  197. data/test/test_cvhaarclassifiercascade.rb +63 -0
  198. data/test/test_cvhistogram.rb +271 -0
  199. data/test/test_cvhumoments.rb +83 -0
  200. data/test/test_cvline.rb +50 -0
  201. data/test/test_cvmat.rb +2947 -0
  202. data/test/test_cvmat_drawing.rb +349 -0
  203. data/test/test_cvmat_dxt.rb +150 -0
  204. data/test/test_cvmat_imageprocessing.rb +2025 -0
  205. data/test/test_cvmat_matching.rb +57 -0
  206. data/test/test_cvmoments.rb +180 -0
  207. data/test/test_cvpoint.rb +75 -0
  208. data/test/test_cvpoint2d32f.rb +75 -0
  209. data/test/test_cvpoint3d32f.rb +93 -0
  210. data/test/test_cvrect.rb +144 -0
  211. data/test/test_cvscalar.rb +113 -0
  212. data/test/test_cvseq.rb +295 -0
  213. data/test/test_cvsize.rb +75 -0
  214. data/test/test_cvsize2d32f.rb +75 -0
  215. data/test/test_cvslice.rb +31 -0
  216. data/test/test_cvsurfparams.rb +57 -0
  217. data/test/test_cvsurfpoint.rb +66 -0
  218. data/test/test_cvtermcriteria.rb +56 -0
  219. data/test/test_cvtwopoints.rb +40 -0
  220. data/test/test_cvvideowriter.rb +58 -0
  221. data/test/test_iplconvkernel.rb +54 -0
  222. data/test/test_iplimage.rb +236 -0
  223. data/test/test_mouseevent.rb +17 -0
  224. data/test/test_opencv.rb +324 -0
  225. data/test/test_pointset.rb +126 -0
  226. data/test/test_preliminary.rb +130 -0
  227. data/test/test_trackbar.rb +47 -0
  228. data/test/test_window.rb +115 -0
  229. metadata +386 -0
@@ -0,0 +1,2025 @@
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, 128, :cv8u, 1) { |j, i, c|
1173
+ if (i >= 32 and i < 96) and (j >= 32 and j < 96)
1174
+ CvScalar.new(255)
1175
+ elsif (i >= 16 and i < 112) 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('8c6a235fdf4c9c4f6822a45daac5b1af', hash_img(mat1))
1193
+ assert_equal(5120.0, comp1.area)
1194
+ assert_equal(16, comp1.rect.x)
1195
+ assert_equal(16, comp1.rect.y)
1196
+ assert_equal(96, comp1.rect.width)
1197
+ assert_equal(96, comp1.rect.height)
1198
+ assert_cvscalar_equal(CvScalar.new(0, 0, 0, 0), comp1.value)
1199
+ assert_equal('1fd2537966283987b39c8b2c9d778383', hash_img(mask1))
1200
+
1201
+ assert_equal('7456e5de74bb8b4e783d04bbf1904644', hash_img(mat2))
1202
+ assert_equal(12288.0, comp2.area)
1203
+ assert_equal(0, comp2.rect.x)
1204
+ assert_equal(0, comp2.rect.y)
1205
+ assert_equal(128, comp2.rect.width)
1206
+ assert_equal(128, comp2.rect.height)
1207
+ assert_cvscalar_equal(CvScalar.new(0, 0, 0, 0), comp2.value)
1208
+ assert_equal('847934f5170e2072cdfd63e16a1e06ad', hash_img(mask2))
1209
+
1210
+ assert_equal('df720005423762ca1b68e06571f58b21', hash_img(mat3))
1211
+ assert_equal(9216.0, comp3.area)
1212
+ assert_equal(16, comp3.rect.x)
1213
+ assert_equal(16, comp3.rect.y)
1214
+ assert_equal(96, comp3.rect.width)
1215
+ assert_equal(96, comp3.rect.height)
1216
+ assert_cvscalar_equal(CvScalar.new(0, 0, 0, 0), comp3.value)
1217
+
1218
+ assert_equal('7833f4c85c77056db71e33ae8072a1b5', hash_img(mat4))
1219
+ assert_equal(9216.0, comp4.area)
1220
+ assert_equal(16, comp4.rect.x)
1221
+ assert_equal(16, comp4.rect.y)
1222
+ assert_equal(96, comp4.rect.width)
1223
+ assert_equal(96, comp4.rect.height)
1224
+ assert_cvscalar_equal(CvScalar.new(220, 0, 0, 0), comp4.value)
1225
+ assert_equal('b34b0269872fe3acde0e0c73e5cdd23b', hash_img(mask4))
1226
+
1227
+ assert_equal('7833f4c85c77056db71e33ae8072a1b5', hash_img(mat5))
1228
+ assert_equal('7833f4c85c77056db71e33ae8072a1b5', hash_img(mat05))
1229
+ assert_equal(9216.0, comp5.area)
1230
+ assert_equal(16, comp5.rect.x)
1231
+ assert_equal(16, comp5.rect.y)
1232
+ assert_equal(96, comp5.rect.width)
1233
+ assert_equal(96, comp5.rect.height)
1234
+ assert_cvscalar_equal(CvScalar.new(220, 0, 0, 0), comp5.value)
1235
+ assert_equal('b34b0269872fe3acde0e0c73e5cdd23b', hash_img(mask5))
1236
+
1237
+ assert_raise(TypeError) {
1238
+ mat0.flood_fill(DUMMY_OBJ, 0)
1239
+ }
1240
+ assert_raise(TypeError) {
1241
+ mat0.flood_fill(point, DUMMY_OBJ)
1242
+ }
1243
+ assert_raise(TypeError) {
1244
+ mat0.flood_fill(point, 0, DUMMY_OBJ)
1245
+ }
1246
+ assert_raise(TypeError) {
1247
+ mat0.flood_fill(point, 0, CvScalar.new(0), DUMMY_OBJ)
1248
+ }
1249
+ assert_raise(TypeError) {
1250
+ mat0.flood_fill(point, 0, CvScalar.new(0), CvScalar.new(64), DUMMY_OBJ)
1251
+ }
1252
+ end
1253
+
1254
+ def test_find_contours
1255
+ mat0 = CvMat.load(FILENAME_CONTOURS, CV_LOAD_IMAGE_GRAYSCALE)
1256
+
1257
+ # Make binary image
1258
+ mat0.height.times { |j|
1259
+ mat0.width.times { |i|
1260
+ mat0[j, i] = (mat0[j, i][0] < 128) ? CvColor::Black : CvColor::White
1261
+ }
1262
+ }
1263
+
1264
+ [mat0.find_contours, mat0.find_contours(:mode => CV_RETR_LIST),
1265
+ mat0.find_contours(:method => CV_CHAIN_APPROX_SIMPLE),
1266
+ mat0.find_contours(:mode => CV_RETR_LIST, :method => CV_CHAIN_APPROX_SIMPLE)].each { |contours|
1267
+ assert_not_nil(contours)
1268
+ assert_equal(8, contours.total)
1269
+ assert_not_nil(contours.h_next)
1270
+ assert_equal(4, contours.h_next.total)
1271
+ assert_not_nil(contours.h_next.h_next)
1272
+ assert_equal(8, contours.h_next.h_next.total)
1273
+ assert_not_nil(contours.h_next.h_next.h_next)
1274
+ assert_equal(4, contours.h_next.h_next.h_next.total)
1275
+ assert_nil(contours.v_next)
1276
+ assert_nil(contours.h_next.v_next)
1277
+ assert_nil(contours.h_next.h_next.v_next)
1278
+ assert_nil(contours.h_next.h_next.h_next.v_next)
1279
+ }
1280
+
1281
+ contours = mat0.find_contours(:mode => CV_RETR_TREE)
1282
+ assert_not_nil(contours)
1283
+ assert_equal(4, contours.total)
1284
+ assert_not_nil(contours.v_next)
1285
+ assert_equal(8, contours.v_next.total)
1286
+ assert_nil(contours.v_next.v_next)
1287
+ assert_not_nil(contours.h_next)
1288
+ assert_equal(4, contours.h_next.total)
1289
+ assert_not_nil(contours.h_next.v_next)
1290
+ assert_equal(8, contours.h_next.v_next.total)
1291
+ assert_nil(contours.h_next.v_next.v_next)
1292
+
1293
+ contours = mat0.find_contours(:mode => CV_RETR_CCOMP)
1294
+ assert_not_nil(contours)
1295
+ assert_equal(4, contours.total)
1296
+ assert_not_nil(contours.v_next)
1297
+ assert_equal(8, contours.v_next.total)
1298
+ assert_nil(contours.v_next.v_next)
1299
+ assert_not_nil(contours.h_next)
1300
+ assert_equal(4, contours.h_next.total)
1301
+ assert_not_nil(contours.h_next.v_next)
1302
+ assert_equal(8, contours.h_next.v_next.total)
1303
+ assert_nil(contours.h_next.v_next.v_next)
1304
+
1305
+ contours = mat0.find_contours(:mode => CV_RETR_EXTERNAL)
1306
+ assert_not_nil(contours)
1307
+ assert_equal(4, contours.total)
1308
+ assert_nil(contours.v_next)
1309
+ assert_not_nil(contours.h_next)
1310
+ assert_equal(4, contours.h_next.total)
1311
+ assert_nil(contours.h_next.v_next)
1312
+
1313
+ contours = mat0.find_contours(:mode => CV_RETR_TREE, :method => CV_CHAIN_APPROX_NONE)
1314
+ assert_not_nil(contours)
1315
+ assert_equal(474, contours.total)
1316
+ assert_not_nil(contours.v_next)
1317
+ assert_equal(318, contours.v_next.total)
1318
+ assert_nil(contours.v_next.v_next)
1319
+ assert_not_nil(contours.h_next)
1320
+ assert_equal(396, contours.h_next.total)
1321
+ assert_not_nil(contours.h_next.v_next)
1322
+ assert_equal(240, contours.h_next.v_next.total)
1323
+ assert_nil(contours.h_next.v_next.v_next)
1324
+
1325
+ contours = mat0.find_contours(:mode => CV_RETR_EXTERNAL, :method => CV_CHAIN_CODE)
1326
+ assert_equal(474, contours.total)
1327
+ assert_equal(396, contours.h_next.total)
1328
+
1329
+ contours = mat0.find_contours(:mode => CV_RETR_EXTERNAL, :method => CV_CHAIN_APPROX_TC89_L1)
1330
+ assert_equal(4, contours.total)
1331
+ assert_equal(4, contours.h_next.total)
1332
+
1333
+ contours = mat0.find_contours(:mode => CV_RETR_EXTERNAL, :method => CV_CHAIN_APPROX_TC89_KCOS)
1334
+ assert_equal(4, contours.total)
1335
+ assert_equal(4, contours.h_next.total)
1336
+
1337
+ assert_raise(TypeError) {
1338
+ mat0.find_contours(DUMMY_OBJ)
1339
+ }
1340
+ assert_raise(CvStsUnsupportedFormat) {
1341
+ CvMat.new(10, 10, :cv32f, 3).find_contours
1342
+ }
1343
+ end
1344
+
1345
+ def test_pyr_mean_shift_filtering
1346
+ mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH)
1347
+ mat1 = mat0.pyr_mean_shift_filtering(30, 30)
1348
+ mat2 = mat0.pyr_mean_shift_filtering(30, 30, 2)
1349
+ mat3 = mat0.pyr_mean_shift_filtering(30, 30, nil, CvTermCriteria.new(3, 0.01))
1350
+
1351
+ [mat1, mat2, mat3].each { |mat|
1352
+ b, g, r = color_hists(mat)
1353
+ assert_in_delta(6900000, b, 100000)
1354
+ assert_in_delta(6500000, g, 100000)
1355
+ assert_in_delta(11800000, r, 100000)
1356
+ }
1357
+
1358
+ assert_raise(TypeError) {
1359
+ mat0.pyr_mean_shift_filtering(DUMMY_OBJ, 30)
1360
+ }
1361
+ assert_raise(TypeError) {
1362
+ mat0.pyr_mean_shift_filtering(30, DUMMY_OBJ)
1363
+ }
1364
+ assert_raise(TypeError) {
1365
+ mat0.pyr_mean_shift_filtering(30, 30, 2, DUMMY_OBJ)
1366
+ }
1367
+ end
1368
+
1369
+ def test_watershed
1370
+ mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH)
1371
+ marker = CvMat.new(mat0.cols, mat0.rows, :cv32s, 1).set_zero
1372
+ marker[150, 150] = CvScalar.new(1, 1, 1, 1)
1373
+ marker[210, 210] = CvScalar.new(2, 2, 2, 2)
1374
+ marker[40, 90] = CvScalar.new(3, 3, 3, 3)
1375
+
1376
+ mat1 = mat0.watershed(marker)
1377
+ assert_equal('ee6bec03296039c8df1899d3edc4684e', hash_img(mat1))
1378
+
1379
+ assert_raise(TypeError) {
1380
+ mat0.watershed(DUMMY_OBJ)
1381
+ }
1382
+ end
1383
+
1384
+ def test_moments
1385
+ mat = create_cvmat(128, 128, :cv8u, 1) { |j, i|
1386
+ if j >= 32 and j < 96 and i >= 16 and i < 112
1387
+ CvScalar.new(0)
1388
+ elsif j >= 16 and j < 112 and i >= 16 and i < 112
1389
+ CvScalar.new(128)
1390
+ else
1391
+ CvScalar.new(255)
1392
+ end
1393
+ }
1394
+
1395
+ moments1 = mat.moments
1396
+ moments2 = mat.moments(false)
1397
+ moments3 = mat.moments(true)
1398
+
1399
+ [moments1, moments2].each { |m|
1400
+ assert_in_delta(2221056, m.spatial(0, 0), 0.1)
1401
+ assert_in_delta(2221056, m.central(0, 0), 0.1)
1402
+ assert_in_delta(1, m.normalized_central(0, 0), 0.1)
1403
+
1404
+ hu_moments = m.hu
1405
+ assert_equal(CvHuMoments, hu_moments.class)
1406
+ assert_in_delta(0.001771, hu_moments.hu1, 0.000001)
1407
+ hu_moments.to_a[1..7].each { |hu|
1408
+ assert_in_delta(0.0, hu, 0.000001)
1409
+ }
1410
+
1411
+ center = m.gravity_center
1412
+ assert_equal(CvPoint2D32f, center.class)
1413
+ assert_in_delta(63.5, center.x, 0.001)
1414
+ assert_in_delta(63.5, center.y, 0.001)
1415
+
1416
+ assert_in_delta(0, m.angle, 0.001)
1417
+
1418
+ assert_in_delta(2221056, m.m00, 0.001)
1419
+ assert_in_delta(141037056, m.m10, 0.001)
1420
+ assert_in_delta(141037056, m.m01, 0.001)
1421
+ assert_in_delta(13157049856, m.m20, 0.001)
1422
+ assert_in_delta(8955853056, m.m11, 0.001)
1423
+ assert_in_delta(13492594176, m.m02, 0.001)
1424
+ assert_in_delta(1369024659456, m.m30, 0.001)
1425
+ assert_in_delta(835472665856, m.m21, 0.001)
1426
+ assert_in_delta(856779730176, m.m12, 0.001)
1427
+ assert_in_delta(1432945852416, m.m03, 0.001)
1428
+ assert_in_delta(4201196800, m.mu20, 0.001)
1429
+ assert_in_delta(0, m.mu11, 0.001)
1430
+ assert_in_delta(4536741120, m.mu02, 0.001)
1431
+ assert_in_delta(0, m.mu30, 0.001)
1432
+ assert_in_delta(0, m.mu21, 0.001)
1433
+ assert_in_delta(0, m.mu12, 0.001)
1434
+ assert_in_delta(0, m.mu03, 0.001)
1435
+ assert_in_delta(0.000671, m.inv_sqrt_m00, 0.000001)
1436
+ }
1437
+
1438
+ m = moments3
1439
+ assert_in_delta(10240, m.spatial(0, 0), 0.1)
1440
+ assert_in_delta(10240, m.central(0, 0), 0.1)
1441
+ assert_in_delta(1, m.normalized_central(0, 0), 0.1)
1442
+
1443
+ hu_moments = m.hu
1444
+ assert_equal(CvHuMoments, hu_moments.class)
1445
+ assert_in_delta(0.361650, hu_moments.hu1, 0.000001)
1446
+ assert_in_delta(0.000625, hu_moments.hu2, 0.000001)
1447
+ hu_moments.to_a[2..7].each { |hu|
1448
+ assert_in_delta(0.0, hu, 0.000001)
1449
+ }
1450
+
1451
+ center = m.gravity_center
1452
+ assert_equal(CvPoint2D32f, center.class)
1453
+ assert_in_delta(63.5, center.x, 0.001)
1454
+ assert_in_delta(63.5, center.y, 0.001)
1455
+
1456
+ assert_in_delta(0, m.angle, 0.001)
1457
+
1458
+ assert_in_delta(10240, m.m00, 0.001)
1459
+ assert_in_delta(650240, m.m10, 0.001)
1460
+ assert_in_delta(650240, m.m01, 0.001)
1461
+ assert_in_delta(58940416, m.m20, 0.001)
1462
+ assert_in_delta(41290240, m.m11, 0.001)
1463
+ assert_in_delta(61561856, m.m02, 0.001)
1464
+ assert_in_delta(5984288768, m.m30, 0.001)
1465
+ assert_in_delta(3742716416, m.m21, 0.001)
1466
+ assert_in_delta(3909177856, m.m12, 0.001)
1467
+ assert_in_delta(6483673088, m.m03, 0.001)
1468
+ assert_in_delta(17650176, m.mu20, 0.001)
1469
+ assert_in_delta(0, m.mu11, 0.001)
1470
+ assert_in_delta(20271616, m.mu02, 0.001)
1471
+ assert_in_delta(0, m.mu30, 0.001)
1472
+ assert_in_delta(0, m.mu21, 0.001)
1473
+ assert_in_delta(0, m.mu12, 0.001)
1474
+ assert_in_delta(0, m.mu03, 0.001)
1475
+ assert_in_delta(0.009882, m.inv_sqrt_m00, 0.000001)
1476
+ end
1477
+
1478
+ def test_hough_lines
1479
+ mat0 = CvMat.load(FILENAME_LINES, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH)
1480
+ # make a binary image
1481
+ mat = CvMat.new(mat0.rows, mat0.cols, :cv8u, 1)
1482
+ (mat0.rows * mat0.cols).times { |i|
1483
+ mat[i] = (mat0[i][0] <= 100) ? CvScalar.new(0) : CvScalar.new(255);
1484
+ }
1485
+
1486
+ [CV_HOUGH_STANDARD, :standard].each { |method|
1487
+ seq = mat.hough_lines(method, 1, Math::PI / 180, 65)
1488
+ assert_equal(4, seq.size)
1489
+ }
1490
+
1491
+ [CV_HOUGH_PROBABILISTIC, :probabilistic].each { |method|
1492
+ seq = mat.hough_lines(method, 1, Math::PI / 180, 40, 30, 10)
1493
+ assert_equal(4, seq.size)
1494
+ }
1495
+
1496
+ # [CV_HOUGH_MULTI_SCALE, :multi_scale].each { |method|
1497
+ # seq = mat.hough_lines(method, 1, Math::PI / 180, 40, 2, 3)
1498
+ # assert_equal(9, seq.size)
1499
+ # }
1500
+
1501
+ assert_raise(TypeError) {
1502
+ mat.hough_lines(DUMMY_OBJ, 1, Math::PI / 180, 40, 2, 3)
1503
+ }
1504
+ assert_raise(TypeError) {
1505
+ mat.hough_lines(CV_HOUGH_STANDARD, DUMMY_OBJ, Math::PI / 180, 40, 2, 3)
1506
+ }
1507
+ assert_raise(TypeError) {
1508
+ mat.hough_lines(CV_HOUGH_STANDARD, 1, DUMMY_OBJ, 40, 2, 3)
1509
+ }
1510
+ assert_raise(TypeError) {
1511
+ mat.hough_lines(CV_HOUGH_STANDARD, 1, Math::PI / 180, DUMMY_OBJ, 2, 3)
1512
+ }
1513
+ assert_raise(TypeError) {
1514
+ mat.hough_lines(CV_HOUGH_STANDARD, 1, Math::PI / 180, 40, DUMMY_OBJ, 3)
1515
+ }
1516
+ assert_raise(TypeError) {
1517
+ mat.hough_lines(CV_HOUGH_STANDARD, 1, Math::PI / 180, 40, 2, DUMMY_OBJ)
1518
+ }
1519
+ assert_raise(ArgumentError) {
1520
+ mat.hough_lines(:dummy, 1, Math::PI / 180, 40, 2, DUMMY_OBJ)
1521
+ }
1522
+ assert_raise(CvStsBadArg) {
1523
+ CvMat.new(10, 10, :cv32f, 3).hough_lines(:standard, 1, Math::PI / 180, 65)
1524
+ }
1525
+ end
1526
+
1527
+ def test_hough_circles
1528
+ mat0 = CvMat.load(FILENAME_LINES, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH)
1529
+ # make a binary image
1530
+ mat = CvMat.new(mat0.rows, mat0.cols, :cv8u, 1)
1531
+ (mat0.rows * mat0.cols).times { |i|
1532
+ mat[i] = (mat0[i][0] <= 100) ? CvScalar.new(0) : CvScalar.new(255);
1533
+ }
1534
+
1535
+ [mat.hough_circles(CV_HOUGH_GRADIENT, 1.5, 40, 100, 40, 10, 50),
1536
+ mat.hough_circles(:gradient, 1.5, 40, 100, 40, 10, 50),
1537
+ mat.hough_circles(CV_HOUGH_GRADIENT, 1.5, 40, 100, 40),
1538
+ mat.hough_circles(:gradient, 1.5, 40, 100, 40)].each { |seq|
1539
+ assert_equal(2, seq.size)
1540
+ }
1541
+
1542
+ # Uncomment the following lines to show the result
1543
+ # seq = mat.hough_circles(:gradient, 1.5, 40, 100, 40, 10, 50)
1544
+ # seq.each { |circle|
1545
+ # mat0.circle!(circle.center, circle.radius, :color => CvColor::Red, :thickness => 2)
1546
+ # }
1547
+ # snap mat0
1548
+
1549
+ assert_raise(TypeError) {
1550
+ mat.hough_circles(DUMMY_OBJ, 1.5, 40, 100, 50, 10, 50)
1551
+ }
1552
+ assert_raise(TypeError) {
1553
+ mat.hough_circles(CV_HOUGH_GRADIENT, DUMMY_OBJ, 40, 100, 50, 10, 50)
1554
+ }
1555
+ assert_raise(TypeError) {
1556
+ mat.hough_circles(CV_HOUGH_GRADIENT, 1.5, DUMMY_OBJ, 100, 50, 10, 50)
1557
+ }
1558
+ assert_raise(TypeError) {
1559
+ mat.hough_circles(CV_HOUGH_GRADIENT, 1.5, 40, DUMMY_OBJ, 50, 10, 50)
1560
+ }
1561
+ assert_raise(TypeError) {
1562
+ mat.hough_circles(CV_HOUGH_GRADIENT, 1.5, 40, 100, DUMMY_OBJ, 10, 50)
1563
+ }
1564
+ assert_raise(TypeError) {
1565
+ mat.hough_circles(CV_HOUGH_GRADIENT, 1.5, 40, 100, 50, DUMMY_OBJ, 50)
1566
+ }
1567
+ assert_raise(TypeError) {
1568
+ mat.hough_circles(CV_HOUGH_GRADIENT, 1.5, 40, 100, 50, 10, DUMMY_OBJ)
1569
+ }
1570
+ assert_raise(ArgumentError) {
1571
+ mat.hough_circles(:dummy, 1.5, 40, 100, 50, 10, DUMMY_OBJ)
1572
+ }
1573
+ assert_raise(CvStsBadArg) {
1574
+ CvMat.new(10, 10, :cv32f, 3).hough_circles(:gradient, 1.5, 40, 100, 50, 10, 50)
1575
+ }
1576
+ end
1577
+
1578
+ def test_inpaint
1579
+ mat = CvMat.load(FILENAME_LENA_INPAINT, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH)
1580
+ mask = CvMat.load(FILENAME_INPAINT_MASK, CV_LOAD_IMAGE_GRAYSCALE)
1581
+
1582
+ [CV_INPAINT_NS, :ns].each { |method|
1583
+ result_ns = mat.inpaint(method, mask, 10)
1584
+ assert_in_delta(14000, count_threshold(result_ns, 128), 1000)
1585
+ }
1586
+ [CV_INPAINT_TELEA, :telea].each { |method|
1587
+ result_telea = mat.inpaint(method, mask, 10)
1588
+ assert_in_delta(13500, count_threshold(result_telea, 128), 1000)
1589
+ }
1590
+
1591
+ # Uncomment the following lines to show the results
1592
+ # result_ns = mat.inpaint(:ns, mask, 10)
1593
+ # result_telea = mat.inpaint(:telea, mask, 10)
1594
+ # snap mat, result_ns, result_telea
1595
+
1596
+ assert_raise(TypeError) {
1597
+ mat.inpaint(DUMMY_OBJ, mask, 10)
1598
+ }
1599
+ assert_raise(TypeError) {
1600
+ mat.inpaint(:ns, DUMMY_OBJ, 10)
1601
+ }
1602
+ assert_raise(TypeError) {
1603
+ mat.inpaint(:ns, mask, DUMMY_OBJ)
1604
+ }
1605
+ assert_raise(ArgumentError) {
1606
+ mat.inpaint(:dummy, mask, 10)
1607
+ }
1608
+ assert_raise(CvStsUnsupportedFormat) {
1609
+ CvMat.new(10, 10, :cv32f, 3).inpaint(:ns, CvMat.new(10, 10, :cv8u, 1), 10)
1610
+ }
1611
+ end
1612
+
1613
+ def test_equalize_hist
1614
+ mat = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_GRAYSCALE)
1615
+ result = mat.equalize_hist
1616
+ assert_equal('de235065c746193d7f3de9359f63a7af', hash_img(result))
1617
+
1618
+ assert_raise(CvStsAssert) {
1619
+ CvMat.new(10, 10, :cv32f, 3).equalize_hist
1620
+ }
1621
+
1622
+ # Uncomment the following lines to show the result
1623
+ # snap mat, result
1624
+ end
1625
+
1626
+ def test_match_template
1627
+ mat = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH)
1628
+ templ = CvMat.load(FILENAME_LENA_EYES, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH)
1629
+
1630
+ expected_pt = CvPoint.new(100, 120)
1631
+
1632
+ # sqdiff
1633
+ result = mat.match_template(templ)
1634
+ pt = result.min_max_loc[2]
1635
+ assert_in_delta(expected_pt.x, pt.x, 20)
1636
+ assert_in_delta(expected_pt.y, pt.y, 20)
1637
+
1638
+ [CV_TM_SQDIFF, :sqdiff].each { |method|
1639
+ result = mat.match_template(templ, method)
1640
+ assert_in_delta(expected_pt.x, pt.x, 20)
1641
+ assert_in_delta(expected_pt.y, pt.y, 20)
1642
+ }
1643
+
1644
+ # sqdiff_normed
1645
+ [CV_TM_SQDIFF_NORMED, :sqdiff_normed].each { |method|
1646
+ result = mat.match_template(templ, method)
1647
+ pt = result.min_max_loc[2]
1648
+ assert_in_delta(expected_pt.x, pt.x, 20)
1649
+ assert_in_delta(expected_pt.y, pt.y, 20)
1650
+ }
1651
+
1652
+ # ccorr
1653
+ [CV_TM_CCORR, :ccorr].each { |method|
1654
+ result = mat.match_template(templ, method)
1655
+ pt = result.min_max_loc[3]
1656
+ assert_in_delta(110, pt.x, 20)
1657
+ assert_in_delta(60, pt.y, 20)
1658
+ }
1659
+
1660
+ # ccorr_normed
1661
+ [CV_TM_CCORR_NORMED, :ccorr_normed].each { |method|
1662
+ result = mat.match_template(templ, method)
1663
+ pt = result.min_max_loc[3]
1664
+ assert_in_delta(expected_pt.x, pt.x, 20)
1665
+ assert_in_delta(expected_pt.y, pt.y, 20)
1666
+ }
1667
+
1668
+ # ccoeff
1669
+ [CV_TM_CCOEFF, :ccoeff].each { |method|
1670
+ result = mat.match_template(templ, method)
1671
+ pt = result.min_max_loc[3]
1672
+ assert_in_delta(expected_pt.x, pt.x, 20)
1673
+ assert_in_delta(expected_pt.y, pt.y, 20)
1674
+ }
1675
+
1676
+ # ccoeff_normed
1677
+ [CV_TM_CCOEFF_NORMED, :ccoeff_normed].each { |method|
1678
+ result = mat.match_template(templ, method)
1679
+ pt = result.min_max_loc[3]
1680
+ assert_in_delta(expected_pt.x, pt.x, 20)
1681
+ assert_in_delta(expected_pt.y, pt.y, 20)
1682
+ }
1683
+
1684
+ # Uncomment the following lines to show the result
1685
+ # result = mat.match_template(templ)
1686
+ # pt1 = result.min_max_loc[2] # minimum location
1687
+ # pt2 = CvPoint.new(pt1.x + templ.width, pt1.y + templ.height)
1688
+ # mat.rectangle!(pt1, pt2, :color => CvColor::Black, :thickness => 3)
1689
+ # snap mat, templ, result
1690
+
1691
+ assert_raise(TypeError) {
1692
+ mat.match_template(DUMMY_OBJ)
1693
+ }
1694
+ assert_raise(TypeError) {
1695
+ mat.match_template(templ, DUMMY_OBJ)
1696
+ }
1697
+ end
1698
+
1699
+ def test_match_shapes
1700
+ mat_cv = CvMat.load(FILENAME_STR_CV, CV_LOAD_IMAGE_GRAYSCALE)
1701
+ mat_ov = CvMat.load(FILENAME_STR_OV, CV_LOAD_IMAGE_GRAYSCALE)
1702
+ mat_cv_rotated = CvMat.load(FILENAME_STR_CV_ROTATED, CV_LOAD_IMAGE_GRAYSCALE)
1703
+
1704
+ [CV_CONTOURS_MATCH_I1, :i1].each { |method|
1705
+ assert_in_delta(0, mat_cv.match_shapes(mat_cv_rotated, method), 0.00001)
1706
+ assert_in_delta(0.0010649, mat_cv.match_shapes(mat_ov, method), 0.00001)
1707
+ }
1708
+
1709
+ [CV_CONTOURS_MATCH_I2, :i2].each { |method|
1710
+ assert_in_delta(0, mat_cv.match_shapes(mat_cv_rotated, method), 0.00001)
1711
+ assert_in_delta(0.0104650, mat_cv.match_shapes(mat_ov, method), 0.00001)
1712
+ }
1713
+
1714
+ [CV_CONTOURS_MATCH_I3, :i3].each { |method|
1715
+ assert_in_delta(0, mat_cv.match_shapes(mat_cv_rotated, method), 0.00001)
1716
+ assert_in_delta(0.0033327, mat_cv.match_shapes(mat_ov, method), 0.00001)
1717
+ }
1718
+ end
1719
+
1720
+ def test_mean_shift
1721
+ flunk('FIXME: CvMat#mean_shift is not tested yet.')
1722
+ end
1723
+
1724
+ def test_cam_shift
1725
+ flunk('FIXME: CvMat#cam_shift is not tested yet.')
1726
+ end
1727
+
1728
+ def test_snake_image
1729
+ radius = 40
1730
+ center = CvPoint.new(128, 128)
1731
+ mat = CvMat.new(center.y * 2, center.x * 2, :cv8u, 1).zero!
1732
+ mat.circle!(center, radius, :color => CvColor::White, :thickness => -1)
1733
+
1734
+ num_points = 10
1735
+ alpha = 0.05
1736
+ beta = 0.05
1737
+ gamma = 0.9
1738
+
1739
+ arr_alpha = [alpha] * num_points
1740
+ arr_beta = [beta] * num_points
1741
+ arr_gamma = [gamma] * num_points
1742
+ size = CvSize.new(3, 3)
1743
+ term_criteria = CvTermCriteria.new(100, num_points / 2)
1744
+
1745
+ # initialize contours
1746
+ points = []
1747
+ num_points.times { |i|
1748
+ x = center.x * Math.cos(2 * Math::PI * i / num_points) + center.x
1749
+ y = center.y * Math.sin(2 * Math::PI * i / num_points) + center.y
1750
+ points << CvPoint.new(x, y)
1751
+ }
1752
+
1753
+ acceptable_error = 50
1754
+
1755
+ # test snake_image
1756
+ # calc_gradient = true
1757
+ [mat.snake_image(points, alpha, beta, gamma, size, term_criteria),
1758
+ mat.snake_image(points, alpha, beta, gamma, size, term_criteria, true),
1759
+ mat.snake_image(points, arr_alpha, arr_beta, arr_gamma, size, term_criteria),
1760
+ mat.snake_image(points, arr_alpha, arr_beta, arr_gamma, size, term_criteria, true)].each { |result|
1761
+ assert_equal(num_points, result.size)
1762
+ result.each { |pt|
1763
+ x = pt.x - center.x
1764
+ y = pt.y - center.y
1765
+ error = Math.sqrt((x * x + y * y - radius * radius).abs)
1766
+ assert(error < acceptable_error)
1767
+ }
1768
+ }
1769
+
1770
+ # calc_gradient = false
1771
+ [mat.snake_image(points, alpha, beta, gamma, size, term_criteria, false),
1772
+ mat.snake_image(points, arr_alpha, arr_beta, arr_gamma, size, term_criteria, false)].each { |result|
1773
+ expected_points = [[149, 102], [139, 144], [95, 144], [56, 124], [17, 105],
1774
+ [25, 61], [63, 39], [101, 17], [145, 17], [158, 59]]
1775
+ assert_equal(num_points, result.size)
1776
+ result.each { |pt|
1777
+ x = pt.x - center.x
1778
+ y = pt.y - center.y
1779
+ error = Math.sqrt((x * x + y * y - radius * radius).abs)
1780
+ assert(error < acceptable_error)
1781
+ }
1782
+ }
1783
+
1784
+ # raise error
1785
+ assert_raise(TypeError) {
1786
+ mat.snake_image(DUMMY_OBJ, arr_alpha, arr_beta, arr_gamma, size, term_criteria)
1787
+ }
1788
+ assert_raise(TypeError) {
1789
+ mat.snake_image(points, DUMMY_OBJ, arr_beta, arr_gamma, size, term_criteria)
1790
+ }
1791
+ assert_raise(TypeError) {
1792
+ mat.snake_image(points, arr_alpha, DUMMY_OBJ, arr_gamma, size, term_criteria)
1793
+ }
1794
+ assert_raise(TypeError) {
1795
+ mat.snake_image(points, arr_alpha, arr_beta, DUMMY_OBJ, size, term_criteria)
1796
+ }
1797
+ assert_raise(TypeError) {
1798
+ mat.snake_image(points, arr_alpha, arr_beta, arr_gamma, DUMMY_OBJ, term_criteria)
1799
+ }
1800
+ assert_raise(TypeError) {
1801
+ mat.snake_image(points, arr_alpha, arr_beta, arr_gamma, size, DUMMY_OBJ)
1802
+ }
1803
+ mat.snake_image(points, arr_alpha, arr_beta, arr_gamma, size, term_criteria, DUMMY_OBJ)
1804
+
1805
+ assert_raise(ArgumentError) {
1806
+ mat.snake_image(points, arr_alpha[0 .. num_points / 2], arr_beta, arr_gamma, size, term_criteria)
1807
+ }
1808
+ assert_raise(CvBadNumChannels) {
1809
+ CvMat.new(10, 10, :cv8u, 3).snake_image(points, alpha, beta, gamma, size, term_criteria)
1810
+ }
1811
+
1812
+ # Uncomment the following lines to show the result
1813
+ # result = mat.clone.GRAY2BGR
1814
+ # pts = mat.snake_image(points, alpha, beta, gamma, size, term_criteria)
1815
+ # w = GUI::Window.new('HoughCircle')
1816
+ # result.poly_line!([pts], :color => CvColor::Red, :is_closed => true, :thickness => 2)
1817
+ # result.poly_line!([points], :color => CvColor::Yellow, :is_closed => true, :thickness => 2)
1818
+ # w.show result
1819
+ # GUI::wait_key
1820
+ end
1821
+
1822
+ def test_optical_flow_hs
1823
+ size = 128
1824
+ prev = create_cvmat(size, size, :cv8u, 1) { |j, i|
1825
+ if ((i - (size / 2)) ** 2 ) + ((j - (size / 2)) ** 2 ) < size
1826
+ CvColor::Black
1827
+ else
1828
+ CvColor::White
1829
+ end
1830
+ }
1831
+ curr = create_cvmat(size, size, :cv8u, 1) { |j, i|
1832
+ if ((i - (size / 2) - 10) ** 2) + ((j - (size / 2) - 7) ** 2 ) < size
1833
+ CvColor::Black
1834
+ else
1835
+ CvColor::White
1836
+ end
1837
+ }
1838
+
1839
+ [curr.optical_flow_hs(prev, nil, nil, :lambda => 0.0005, :criteria => CvTermCriteria.new(1, 0.001)),
1840
+ curr.optical_flow_hs(prev)].each { |velx, vely|
1841
+ assert_in_delta(60, count_threshold(velx, 1), 20)
1842
+ assert_in_delta(50, count_threshold(vely, 1), 20)
1843
+ }
1844
+
1845
+ velx, vely = curr.optical_flow_hs(prev, nil, nil, :lambda => 0.001)
1846
+ assert_in_delta(60, count_threshold(velx, 1), 20)
1847
+ assert_in_delta(50, count_threshold(vely, 1), 20)
1848
+
1849
+ velx, vely = curr.optical_flow_hs(prev, nil, nil, :criteria => CvTermCriteria.new(10, 0.01))
1850
+ assert_in_delta(130, count_threshold(velx, 1), 20)
1851
+ assert_in_delta(110, count_threshold(vely, 1), 20)
1852
+
1853
+ prev_velx, prev_vely = curr.optical_flow_hs(prev)
1854
+ velx, vely = curr.optical_flow_hs(prev, prev_velx, prev_vely)
1855
+ assert_in_delta(70, count_threshold(velx, 1), 20)
1856
+ assert_in_delta(60, count_threshold(vely, 1), 20)
1857
+
1858
+ velx, vely = curr.optical_flow_hs(prev, prev_velx, prev_vely, :lambda => 0.001)
1859
+ assert_in_delta(80, count_threshold(velx, 1), 20)
1860
+ assert_in_delta(70, count_threshold(vely, 1), 20)
1861
+
1862
+ velx, vely = curr.optical_flow_hs(prev, prev_velx, prev_vely, :criteria => CvTermCriteria.new(10, 0.01))
1863
+ assert_in_delta(150, count_threshold(velx, 1), 20)
1864
+ assert_in_delta(130, count_threshold(vely, 1), 20)
1865
+
1866
+ assert_raise(TypeError) {
1867
+ curr.optical_flow_hs(DUMMY_OBJ)
1868
+ }
1869
+ assert_raise(TypeError) {
1870
+ curr.optical_flow_hs(prev, DUMMY_OBJ, prev_vely)
1871
+ }
1872
+ assert_raise(TypeError) {
1873
+ curr.optical_flow_hs(prev, prev_velx, DUMMY_OBJ)
1874
+ }
1875
+ assert_raise(TypeError) {
1876
+ curr.optical_flow_hs(prev, prev_velx, prev_vely, DUMMY_OBJ)
1877
+ }
1878
+ assert_raise(CvStsUnmatchedFormats) {
1879
+ CvMat.new(10, 10, :cv8u, 3).optical_flow_hs(prev)
1880
+ }
1881
+ end
1882
+
1883
+ def test_optical_flow_lk
1884
+ size = 128
1885
+ prev = create_cvmat(size, size, :cv8u, 1) { |j, i|
1886
+ if ((i - (size / 2)) ** 2 ) + ((j - (size / 2)) ** 2 ) < size
1887
+ CvColor::Black
1888
+ else
1889
+ CvColor::White
1890
+ end
1891
+ }
1892
+ curr = create_cvmat(size, size, :cv8u, 1) { |j, i|
1893
+ if ((i - (size / 2) - 10) ** 2) + ((j - (size / 2) - 7) ** 2 ) < size
1894
+ CvColor::Black
1895
+ else
1896
+ CvColor::White
1897
+ end
1898
+ }
1899
+
1900
+ velx, vely = curr.optical_flow_lk(prev, CvSize.new(3, 3))
1901
+ assert_in_delta(100, count_threshold(velx, 1), 20)
1902
+ assert_in_delta(90, count_threshold(vely, 1), 20)
1903
+
1904
+ velx, vely = curr.optical_flow_lk(prev, CvSize.new(5, 5))
1905
+ assert_in_delta(180, count_threshold(velx, 1), 20)
1906
+ assert_in_delta(150, count_threshold(vely, 1), 20)
1907
+
1908
+ assert_raise(TypeError) {
1909
+ curr.optical_flow_lk(DUMMY_OBJ, CvSize.new(3, 3))
1910
+ }
1911
+ assert_raise(TypeError) {
1912
+ curr.optical_flow_lk(prev, DUMMY_OBJ)
1913
+ }
1914
+ assert_raise(CvStsUnmatchedFormats) {
1915
+ CvMat.new(10, 10, :cv8u, 3).optical_flow_lk(prev, CvSize.new(3, 3))
1916
+ }
1917
+ end
1918
+
1919
+ def test_optical_flow_bm
1920
+ size = 128
1921
+ prev = create_cvmat(size, size, :cv8u, 1) { |j, i|
1922
+ if ((i - (size / 2)) ** 2 ) + ((j - (size / 2)) ** 2 ) < size
1923
+ CvColor::Black
1924
+ else
1925
+ CvColor::White
1926
+ end
1927
+ }
1928
+ curr = create_cvmat(size, size, :cv8u, 1) { |j, i|
1929
+ if ((i - (size / 2) - 10) ** 2) + ((j - (size / 2) - 7) ** 2 ) < size
1930
+ CvColor::Black
1931
+ else
1932
+ CvColor::White
1933
+ end
1934
+ }
1935
+
1936
+ [curr.optical_flow_bm(prev, nil, nil, :block_size => CvSize.new(4, 4),
1937
+ :shift_size => CvSize.new(1, 1), :max_range => CvSize.new(4, 4)),
1938
+ curr.optical_flow_bm(prev)].each { |velx, vely|
1939
+ assert_in_delta(350, count_threshold(velx, 1), 30)
1940
+ assert_in_delta(250, count_threshold(vely, 1), 30)
1941
+ }
1942
+
1943
+ velx, vely = curr.optical_flow_bm(prev, nil, nil, :block_size => CvSize.new(3, 3))
1944
+ assert_in_delta(280, count_threshold(velx, 1), 30)
1945
+ assert_in_delta(200, count_threshold(vely, 1), 30)
1946
+
1947
+ velx, vely = curr.optical_flow_bm(prev, nil, nil, :shift_size => CvSize.new(2, 2))
1948
+ assert_in_delta(80, count_threshold(velx, 1), 30)
1949
+ assert_in_delta(60, count_threshold(vely, 1), 30)
1950
+
1951
+ velx, vely = curr.optical_flow_bm(prev, nil, nil, :max_range => CvSize.new(5, 5))
1952
+ assert_in_delta(400, count_threshold(velx, 1), 30)
1953
+ assert_in_delta(300, count_threshold(vely, 1), 30)
1954
+
1955
+ prev_velx, prev_vely = curr.optical_flow_bm(prev)
1956
+ velx, vely = curr.optical_flow_bm(prev, prev_velx, prev_vely)
1957
+ assert_in_delta(350, count_threshold(velx, 1), 30)
1958
+ assert_in_delta(270, count_threshold(vely, 1), 30)
1959
+
1960
+ assert_raise(TypeError) {
1961
+ curr.optical_flow_bm(DUMMY_OBJ)
1962
+ }
1963
+ assert_raise(TypeError) {
1964
+ curr.optical_flow_bm(prev, DUMMY_OBJ, prev_vely)
1965
+ }
1966
+ assert_raise(TypeError) {
1967
+ curr.optical_flow_bm(prev, prev_velx, DUMMY_OBJ)
1968
+ }
1969
+ assert_raise(TypeError) {
1970
+ curr.optical_flow_bm(prev, prev_velx, prev_vely, DUMMY_OBJ)
1971
+ }
1972
+ assert_raise(CvStsUnmatchedFormats) {
1973
+ CvMat.new(10, 10, :cv8u, 3).optical_flow_bm(prev)
1974
+ }
1975
+ end
1976
+
1977
+ def test_extract_surf
1978
+ mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_GRAYSCALE)
1979
+
1980
+ # simple
1981
+ keypoints1, descriptors1 = mat0.extract_surf(CvSURFParams.new(500, true, 2, 3))
1982
+ assert_equal(CvSeq, keypoints1.class)
1983
+ assert_equal(254, keypoints1.size)
1984
+ assert_equal(Array, descriptors1.class)
1985
+ assert_equal(254, descriptors1.size)
1986
+ assert_equal(Array, descriptors1[0].class)
1987
+ assert_equal(128, descriptors1[0].size)
1988
+
1989
+ # use mask
1990
+ mask = create_cvmat(mat0.rows, mat0.cols, :cv8u, 1) { |j, i|
1991
+ if i < mat0.cols / 2
1992
+ CvScalar.new(1)
1993
+ else
1994
+ CvScalar.new(0)
1995
+ end
1996
+ }
1997
+ keypoints2, descriptors2 = mat0.extract_surf(CvSURFParams.new(500, false), mask)
1998
+ assert_equal(CvSeq, keypoints2.class)
1999
+ assert_equal(170, keypoints2.size)
2000
+ assert_equal(Array, descriptors2.class)
2001
+ assert_equal(170, descriptors2.size)
2002
+ assert_equal(Array, descriptors2[0].class)
2003
+ assert_equal(64, descriptors2[0].size)
2004
+
2005
+ # raise exceptions because of invalid arguments
2006
+ assert_raise(TypeError) {
2007
+ mat0.extract_surf(DUMMY_OBJ)
2008
+ }
2009
+ assert_raise(TypeError) {
2010
+ mat0.extract_surf(CvSURFParams.new(500), DUMMY_OBJ)
2011
+ }
2012
+
2013
+ # Uncomment the following lines to show the result
2014
+ # results = []
2015
+ # [keypoints1, keypoints2].each { |kpts|
2016
+ # tmp = mat0.GRAY2BGR
2017
+ # kpts.each { |kp|
2018
+ # tmp.circle!(kp.pt, 3, :color => CvColor::Red, :thickness => 1, :line_type => :aa)
2019
+ # }
2020
+ # results << tmp
2021
+ # }
2022
+ # snap mat0, *results
2023
+ end
2024
+ end
2025
+