ruby-opencv 0.0.8.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (230) hide show
  1. data/Gemfile +8 -0
  2. data/History.txt +5 -0
  3. data/License.txt +30 -0
  4. data/Manifest.txt +229 -0
  5. data/README.rdoc +149 -0
  6. data/Rakefile +32 -0
  7. data/examples/alpha_blend.rb +21 -0
  8. data/examples/box.png +0 -0
  9. data/examples/box_in_scene.png +0 -0
  10. data/examples/contours/bitmap-contours-with-labels.png +0 -0
  11. data/examples/contours/bitmap-contours.png +0 -0
  12. data/examples/contours/bounding-box-detect-canny.rb +62 -0
  13. data/examples/contours/contour_retrieval_modes.rb +139 -0
  14. data/examples/contours/rotated-boxes-with-detected-bounding-rectangles.jpg +0 -0
  15. data/examples/contours/rotated-boxes.jpg +0 -0
  16. data/examples/convexhull.rb +47 -0
  17. data/examples/face_detect.rb +20 -0
  18. data/examples/find_obj.rb +169 -0
  19. data/examples/houghcircle.rb +22 -0
  20. data/examples/inpaint.png +0 -0
  21. data/examples/inpaint.rb +57 -0
  22. data/examples/lenna-rotated.jpg +0 -0
  23. data/examples/lenna.jpg +0 -0
  24. data/examples/match_kdtree.rb +88 -0
  25. data/examples/matching_to_many_images.rb +16 -0
  26. data/examples/matching_to_many_images/query.png +0 -0
  27. data/examples/matching_to_many_images/train/1.png +0 -0
  28. data/examples/matching_to_many_images/train/2.png +0 -0
  29. data/examples/matching_to_many_images/train/3.png +0 -0
  30. data/examples/matching_to_many_images/train/trainImages.txt +3 -0
  31. data/examples/paint.rb +70 -0
  32. data/examples/snake.rb +43 -0
  33. data/examples/stuff.jpg +0 -0
  34. data/examples/tiffany.jpg +0 -0
  35. data/ext/opencv/GPATH +0 -0
  36. data/ext/opencv/GSYMS +0 -0
  37. data/ext/opencv/curve.cpp +112 -0
  38. data/ext/opencv/curve.h +34 -0
  39. data/ext/opencv/cvavgcomp.cpp +67 -0
  40. data/ext/opencv/cvavgcomp.h +39 -0
  41. data/ext/opencv/cvbox2d.cpp +197 -0
  42. data/ext/opencv/cvbox2d.h +61 -0
  43. data/ext/opencv/cvcapture.cpp +499 -0
  44. data/ext/opencv/cvcapture.h +72 -0
  45. data/ext/opencv/cvchain.cpp +233 -0
  46. data/ext/opencv/cvchain.h +46 -0
  47. data/ext/opencv/cvcircle32f.cpp +116 -0
  48. data/ext/opencv/cvcircle32f.h +52 -0
  49. data/ext/opencv/cvcondensation.cpp +282 -0
  50. data/ext/opencv/cvcondensation.h +49 -0
  51. data/ext/opencv/cvconnectedcomp.cpp +143 -0
  52. data/ext/opencv/cvconnectedcomp.h +49 -0
  53. data/ext/opencv/cvcontour.cpp +296 -0
  54. data/ext/opencv/cvcontour.h +48 -0
  55. data/ext/opencv/cvcontourtree.cpp +91 -0
  56. data/ext/opencv/cvcontourtree.h +41 -0
  57. data/ext/opencv/cvconvexitydefect.cpp +103 -0
  58. data/ext/opencv/cvconvexitydefect.h +42 -0
  59. data/ext/opencv/cverror.cpp +159 -0
  60. data/ext/opencv/cverror.h +28 -0
  61. data/ext/opencv/cvfeaturetree.cpp +125 -0
  62. data/ext/opencv/cvfeaturetree.h +55 -0
  63. data/ext/opencv/cvfont.cpp +208 -0
  64. data/ext/opencv/cvfont.h +64 -0
  65. data/ext/opencv/cvhaarclassifiercascade.cpp +168 -0
  66. data/ext/opencv/cvhaarclassifiercascade.h +39 -0
  67. data/ext/opencv/cvhistogram.cpp +546 -0
  68. data/ext/opencv/cvhistogram.h +73 -0
  69. data/ext/opencv/cvhumoments.cpp +139 -0
  70. data/ext/opencv/cvhumoments.h +51 -0
  71. data/ext/opencv/cvline.cpp +154 -0
  72. data/ext/opencv/cvline.h +54 -0
  73. data/ext/opencv/cvmat.cpp +5848 -0
  74. data/ext/opencv/cvmat.h +284 -0
  75. data/ext/opencv/cvmatnd.cpp +44 -0
  76. data/ext/opencv/cvmatnd.h +28 -0
  77. data/ext/opencv/cvmemstorage.cpp +68 -0
  78. data/ext/opencv/cvmemstorage.h +53 -0
  79. data/ext/opencv/cvmoments.cpp +287 -0
  80. data/ext/opencv/cvmoments.h +75 -0
  81. data/ext/opencv/cvpoint.cpp +228 -0
  82. data/ext/opencv/cvpoint.h +64 -0
  83. data/ext/opencv/cvpoint2d32f.cpp +211 -0
  84. data/ext/opencv/cvpoint2d32f.h +63 -0
  85. data/ext/opencv/cvpoint3d32f.cpp +245 -0
  86. data/ext/opencv/cvpoint3d32f.h +66 -0
  87. data/ext/opencv/cvrect.cpp +333 -0
  88. data/ext/opencv/cvrect.h +79 -0
  89. data/ext/opencv/cvscalar.cpp +236 -0
  90. data/ext/opencv/cvscalar.h +71 -0
  91. data/ext/opencv/cvseq.cpp +599 -0
  92. data/ext/opencv/cvseq.h +74 -0
  93. data/ext/opencv/cvsize.cpp +221 -0
  94. data/ext/opencv/cvsize.h +65 -0
  95. data/ext/opencv/cvsize2d32f.cpp +209 -0
  96. data/ext/opencv/cvsize2d32f.h +64 -0
  97. data/ext/opencv/cvslice.cpp +120 -0
  98. data/ext/opencv/cvslice.h +61 -0
  99. data/ext/opencv/cvsparsemat.cpp +44 -0
  100. data/ext/opencv/cvsparsemat.h +28 -0
  101. data/ext/opencv/cvsurfparams.cpp +199 -0
  102. data/ext/opencv/cvsurfparams.h +58 -0
  103. data/ext/opencv/cvsurfpoint.cpp +223 -0
  104. data/ext/opencv/cvsurfpoint.h +52 -0
  105. data/ext/opencv/cvtermcriteria.cpp +192 -0
  106. data/ext/opencv/cvtermcriteria.h +71 -0
  107. data/ext/opencv/cvtwopoints.cpp +116 -0
  108. data/ext/opencv/cvtwopoints.h +51 -0
  109. data/ext/opencv/cvutils.cpp +194 -0
  110. data/ext/opencv/cvutils.h +29 -0
  111. data/ext/opencv/cvvideowriter.cpp +137 -0
  112. data/ext/opencv/cvvideowriter.h +43 -0
  113. data/ext/opencv/gui.cpp +68 -0
  114. data/ext/opencv/gui.h +30 -0
  115. data/ext/opencv/iplconvkernel.cpp +192 -0
  116. data/ext/opencv/iplconvkernel.h +71 -0
  117. data/ext/opencv/iplimage.cpp +644 -0
  118. data/ext/opencv/iplimage.h +73 -0
  119. data/ext/opencv/lib/opencv.rb +3 -0
  120. data/ext/opencv/lib/opencv/psyched_yaml.rb +22 -0
  121. data/ext/opencv/lib/opencv/version.rb +3 -0
  122. data/ext/opencv/mouseevent.cpp +181 -0
  123. data/ext/opencv/mouseevent.h +56 -0
  124. data/ext/opencv/opencv.cpp +722 -0
  125. data/ext/opencv/opencv.h +400 -0
  126. data/ext/opencv/pointset.cpp +274 -0
  127. data/ext/opencv/pointset.h +68 -0
  128. data/ext/opencv/trackbar.cpp +121 -0
  129. data/ext/opencv/trackbar.h +69 -0
  130. data/ext/opencv/window.cpp +357 -0
  131. data/ext/opencv/window.h +66 -0
  132. data/extconf.rb +75 -0
  133. data/images/CvMat_sobel.png +0 -0
  134. data/images/CvMat_sub_rect.png +0 -0
  135. data/images/CvSeq_relationmap.png +0 -0
  136. data/images/face_detect_from_lena.jpg +0 -0
  137. data/ruby-opencv.gemspec +43 -0
  138. data/test/helper.rb +166 -0
  139. data/test/log.txt +55 -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. data/test/videowriter_result.avi +0 -0
  230. metadata +394 -0
@@ -0,0 +1,2947 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- mode: ruby; coding: utf-8-unix -*-
3
+ require 'test/unit'
4
+ require 'opencv'
5
+ require File.expand_path(File.dirname(__FILE__)) + '/helper'
6
+
7
+ include OpenCV
8
+
9
+ # Tests for OpenCV::CvMat
10
+ class TestCvMat < OpenCVTestCase
11
+ def test_initialize
12
+ m = CvMat.new(10, 20)
13
+ assert_equal(10, m.rows)
14
+ assert_equal(20, m.cols)
15
+ assert_equal(:cv8u, m.depth)
16
+ assert_equal(3, m.channel)
17
+
18
+ depth_table = {
19
+ CV_8U => :cv8u,
20
+ CV_8S => :cv8s,
21
+ CV_16U => :cv16u,
22
+ CV_16S => :cv16s,
23
+ CV_32S => :cv32s,
24
+ CV_32F => :cv32f,
25
+ CV_64F => :cv64f
26
+ }
27
+
28
+ [CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F,
29
+ :cv8u, :cv8s, :cv16u, :cv16s, :cv32s, :cv32f, :cv64f].each { |depth|
30
+ [1, 2, 3, 4].each { |ch|
31
+ m = CvMat.new(10, 20, depth, ch)
32
+ assert_equal(10, m.rows)
33
+ assert_equal(20, m.cols)
34
+ depth = depth_table[depth] unless depth.is_a? Symbol
35
+ assert_equal(depth, m.depth)
36
+ assert_equal(ch, m.channel)
37
+ }
38
+ }
39
+
40
+ assert_raise(TypeError) {
41
+ m = CvMat.new(DUMMY_OBJ, 20, :cv8u, 1)
42
+ }
43
+ assert_raise(TypeError) {
44
+ m = CvMat.new(10, DUMMY_OBJ, :cv8u, 1)
45
+ }
46
+ assert_raise(TypeError) {
47
+ m = CvMat.new(10, 20, :cv8u, DUMMY_OBJ)
48
+ }
49
+ end
50
+
51
+ def test_load
52
+ mat = CvMat.load(FILENAME_CAT)
53
+ assert_equal(CvMat, mat.class)
54
+ assert_equal(375, mat.cols)
55
+ assert_equal(500, mat.rows)
56
+ assert_equal(:cv8u, mat.depth)
57
+ assert_equal(3, mat.channel)
58
+ assert_equal('ebc0b85d3ac44ea60181c997f35d13df', hash_img(mat))
59
+
60
+ mat = CvMat.load(FILENAME_CAT, CV_LOAD_IMAGE_GRAYSCALE)
61
+ assert_equal(CvMat, mat.class)
62
+ assert_equal(375, mat.cols)
63
+ assert_equal(500, mat.rows)
64
+ assert_equal(:cv8u, mat.depth)
65
+ assert_equal(1, mat.channel)
66
+ assert_equal('f0ae1d7f2d6b3a64d093e3181361f3a4', hash_img(mat))
67
+
68
+ mat = CvMat.load(FILENAME_CAT, CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR)
69
+ assert_equal(CvMat, mat.class)
70
+ assert_equal(375, mat.cols)
71
+ assert_equal(500, mat.rows)
72
+ assert_equal(:cv8u, mat.depth)
73
+ assert_equal(3, mat.channel)
74
+ assert_equal('ebc0b85d3ac44ea60181c997f35d13df', hash_img(mat))
75
+
76
+ assert_raise(ArgumentError) {
77
+ CvMat.load
78
+ }
79
+ assert_raise(TypeError) {
80
+ CvMat.load(DUMMY_OBJ)
81
+ }
82
+ assert_raise(TypeError) {
83
+ CvMat.load(FILENAME_CAT, DUMMY_OBJ)
84
+ }
85
+ assert_raise(StandardError) {
86
+ CvMat.load('file/does/not/exist')
87
+ }
88
+ end
89
+
90
+ def test_save_image
91
+ filename_jpg = 'save_image_test.jpg'
92
+ filename_png = 'save_image_test.png'
93
+ m = CvMat.new(20, 20, :cv8u, 1)
94
+
95
+ File.delete filename_jpg if File.exists? filename_jpg
96
+ m.save_image filename_jpg
97
+ assert(File.exists? filename_jpg)
98
+
99
+ File.delete filename_jpg if File.exists? filename_jpg
100
+ m.save_image(filename_jpg, CV_IMWRITE_JPEG_QUALITY => 10)
101
+ assert(File.exists? filename_jpg)
102
+
103
+ File.delete filename_png if File.exists? filename_png
104
+ m.save_image(filename_png, CV_IMWRITE_PNG_COMPRESSION => 9)
105
+ assert(File.exists? filename_png)
106
+
107
+ # Alias
108
+ File.delete filename_jpg if File.exists? filename_jpg
109
+ m.save filename_jpg
110
+ assert(File.exists? filename_jpg)
111
+
112
+ assert_raise(TypeError) {
113
+ m.save_image(DUMMY_OBJ)
114
+ }
115
+ assert_raise(TypeError) {
116
+ m.save_image(filename_jpg, DUMMY_OBJ)
117
+ }
118
+
119
+ File.delete filename_jpg if File.exists? filename_jpg
120
+ File.delete filename_png if File.exists? filename_png
121
+ end
122
+
123
+ def test_encode
124
+ mat = CvMat.load(FILENAME_CAT);
125
+
126
+ jpg = mat.encode('.jpg')
127
+ assert_equal('JFIF', jpg[6, 4].map(&:chr).join) # Is jpeg format?
128
+
129
+ jpg = mat.encode('.jpg', CV_IMWRITE_JPEG_QUALITY => 10)
130
+ assert_equal('JFIF', jpg[6, 4].map(&:chr).join)
131
+
132
+ png = mat.encode('.png')
133
+ assert_equal('PNG', png[1, 3].map(&:chr).join) # Is png format?
134
+
135
+ png = mat.encode('.png', CV_IMWRITE_PNG_COMPRESSION => 9)
136
+ assert_equal('PNG', png[1, 3].map(&:chr).join)
137
+
138
+ assert_raise(TypeError) {
139
+ mat.encode(DUMMY_OBJ)
140
+ }
141
+ assert_raise(TypeError) {
142
+ mat.encode('.jpg', DUMMY_OBJ)
143
+ }
144
+
145
+ # Uncomment the following lines to see the result images
146
+ #
147
+ # open('test-jpeg.jpg', 'wb') { |f|
148
+ # f.write jpg.pack("c*")
149
+ # }
150
+ # open('test-png.png', 'wb') { |f|
151
+ # f.write png.pack("c*")
152
+ # }
153
+ end
154
+
155
+ def test_decode
156
+ data = nil
157
+ open(FILENAME_CAT, 'rb') { |f|
158
+ data = f.read
159
+ }
160
+ data_ary = data.unpack("c*")
161
+ data_mat = CvMat.new(1, data_ary.size).set_data(data_ary)
162
+ expected = CvMat.load(FILENAME_CAT)
163
+
164
+ mat1 = CvMat.decode(data)
165
+ mat2 = CvMat.decode(data_ary)
166
+ mat3 = CvMat.decode(data_mat)
167
+ mat4 = CvMat.decode(data, CV_LOAD_IMAGE_COLOR)
168
+ mat5 = CvMat.decode(data_ary, CV_LOAD_IMAGE_COLOR)
169
+ mat6 = CvMat.decode(data_mat, CV_LOAD_IMAGE_COLOR)
170
+ expected_hash = hash_img(expected)
171
+
172
+ [mat1, mat2, mat3, mat4, mat5, mat6].each { |mat|
173
+ assert_equal(CvMat, mat.class)
174
+ assert_equal(expected.rows, mat.rows)
175
+ assert_equal(expected.cols, mat.cols)
176
+ assert_equal(expected.channel, mat.channel)
177
+ assert_equal(expected_hash, hash_img(mat))
178
+ }
179
+
180
+ expected_c1 = CvMat.load(FILENAME_CAT, CV_LOAD_IMAGE_GRAYSCALE)
181
+ mat1c1 = CvMat.decode(data, CV_LOAD_IMAGE_GRAYSCALE)
182
+ mat2c1 = CvMat.decode(data_ary, CV_LOAD_IMAGE_GRAYSCALE)
183
+ mat3c1 = CvMat.decode(data_mat, CV_LOAD_IMAGE_GRAYSCALE)
184
+ expected_hash_c1 = hash_img(expected_c1)
185
+
186
+ [mat1c1, mat2c1, mat3c1].each { |mat|
187
+ assert_equal(CvMat, mat.class)
188
+ assert_equal(expected_c1.rows, mat.rows)
189
+ assert_equal(expected_c1.cols, mat.cols)
190
+ assert_equal(expected_c1.channel, mat.channel)
191
+ assert_equal(expected_hash_c1, hash_img(mat))
192
+ }
193
+
194
+ assert_raise(TypeError) {
195
+ CvMat.decode(DUMMY_OBJ)
196
+ }
197
+ assert_raise(TypeError) {
198
+ CvMat.decode(data, DUMMY_OBJ)
199
+ }
200
+
201
+ # Uncomment the following line to show the result images
202
+ # snap mat1, mat2, mat3
203
+ end
204
+
205
+ def test_GOOD_FEATURES_TO_TRACK_OPTION
206
+ assert_equal(0xff, CvMat::GOOD_FEATURES_TO_TRACK_OPTION[:max])
207
+ assert_nil(CvMat::GOOD_FEATURES_TO_TRACK_OPTION[:mask])
208
+ assert_equal(3, CvMat::GOOD_FEATURES_TO_TRACK_OPTION[:block_size])
209
+ assert((not CvMat::GOOD_FEATURES_TO_TRACK_OPTION[:use_harris]))
210
+ assert_in_delta(0.04, CvMat::GOOD_FEATURES_TO_TRACK_OPTION[:k], 0.01)
211
+ end
212
+
213
+ def test_FIND_CONTOURS_OPTION
214
+ assert_equal(1, CvMat::FIND_CONTOURS_OPTION[:mode])
215
+ assert_equal(2, CvMat::FIND_CONTOURS_OPTION[:method])
216
+ assert_equal(0, CvMat::FIND_CONTOURS_OPTION[:offset].x)
217
+ assert_equal(0, CvMat::FIND_CONTOURS_OPTION[:offset].y)
218
+ end
219
+
220
+ def test_OPTICAL_FLOW_HS_OPTION
221
+ assert_in_delta(0.0005, CvMat::OPTICAL_FLOW_HS_OPTION[:lambda], 0.000001)
222
+ assert_equal(1, CvMat::OPTICAL_FLOW_HS_OPTION[:criteria].max)
223
+ assert_in_delta(0.001, CvMat::OPTICAL_FLOW_HS_OPTION[:criteria].eps, 0.00001)
224
+ end
225
+
226
+ def test_OPTICAL_FLOW_BM_OPTION
227
+ assert_equal(4, CvMat::OPTICAL_FLOW_BM_OPTION[:block_size].width)
228
+ assert_equal(4, CvMat::OPTICAL_FLOW_BM_OPTION[:block_size].height)
229
+ assert_equal(1, CvMat::OPTICAL_FLOW_BM_OPTION[:shift_size].width)
230
+ assert_equal(1, CvMat::OPTICAL_FLOW_BM_OPTION[:shift_size].height)
231
+ assert_equal(4, CvMat::OPTICAL_FLOW_BM_OPTION[:max_range].width)
232
+ assert_equal(4, CvMat::OPTICAL_FLOW_BM_OPTION[:max_range].height)
233
+ end
234
+
235
+ def test_FIND_FUNDAMENTAL_MAT_OPTION
236
+ assert((not CvMat::FIND_FUNDAMENTAL_MAT_OPTION[:with_status]))
237
+ assert_in_delta(1.0, CvMat::FIND_FUNDAMENTAL_MAT_OPTION[:maximum_distance], 0.01)
238
+ assert_in_delta(0.99, CvMat::FIND_FUNDAMENTAL_MAT_OPTION[:desirable_level], 0.01)
239
+ end
240
+
241
+ def test_to_s
242
+ m = CvMat.new(10, 20)
243
+ assert_equal('<OpenCV::CvMat:20x10,depth=cv8u,channel=3>', m.to_s)
244
+ m = CvMat.new(10, 20, :cv16s)
245
+ assert_equal('<OpenCV::CvMat:20x10,depth=cv16s,channel=3>', m.to_s)
246
+ m = CvMat.new(10, 20, :cv32f, 1)
247
+ assert_equal('<OpenCV::CvMat:20x10,depth=cv32f,channel=1>', m.to_s)
248
+ end
249
+
250
+ def test_inside
251
+ m = CvMat.new(20, 10)
252
+ assert(m.inside? CvPoint.new(0, 0))
253
+ assert(m.inside? CvPoint.new(9, 19))
254
+ assert((not m.inside? CvPoint.new(10, 0)))
255
+ assert((not m.inside? CvPoint.new(0, 20)))
256
+ assert((not m.inside? CvPoint.new(10, 20)))
257
+ end
258
+
259
+ def test_to_IplConvKernel
260
+ kernel = CvMat.new(10, 20).to_IplConvKernel(CvPoint.new(2, 3))
261
+ assert_equal(10, kernel.rows)
262
+ assert_equal(20, kernel.cols)
263
+ assert_equal(2, kernel.anchor.x)
264
+ assert_equal(3, kernel.anchor.y)
265
+ assert_equal(2, kernel.anchor_x)
266
+ assert_equal(3, kernel.anchor_y)
267
+ end
268
+
269
+ def test_create_mask
270
+ mask = CvMat.new(10, 20).create_mask
271
+ assert_equal(20, mask.width)
272
+ assert_equal(10, mask.height)
273
+ assert_equal(:cv8u, mask.depth)
274
+ assert_equal(1, mask.channel)
275
+ end
276
+
277
+ def test_fields
278
+ m = CvMat.new(20, 10)
279
+ assert_equal(10, m.width)
280
+ assert_equal(10, m.columns)
281
+ assert_equal(10, m.cols)
282
+ assert_equal(20, m.height)
283
+ assert_equal(20, m.rows)
284
+ assert_equal(:cv8u, m.depth)
285
+ assert_equal(3, m.channel)
286
+
287
+ m = CvMat.new(20, 10, :cv16s, 1)
288
+ assert_equal(10, m.width)
289
+ assert_equal(10, m.columns)
290
+ assert_equal(10, m.cols)
291
+ assert_equal(20, m.height)
292
+ assert_equal(20, m.rows)
293
+ assert_equal(:cv16s, m.depth)
294
+ assert_equal(1, m.channel)
295
+ end
296
+
297
+ def test_clone
298
+ m1 = create_cvmat(10, 20)
299
+ m2 = m1.clone
300
+ assert_equal(m1.data, m2.data)
301
+ end
302
+
303
+ def test_copy
304
+ m1 = create_cvmat(10, 20, CV_32F, 1) { |j, i, c| CvScalar.new(c) }
305
+
306
+ m2 = m1.copy
307
+ assert_equal(m1.data, m2.data)
308
+
309
+ m2 = create_cvmat(10, 20, CV_32F, 1).zero
310
+ m3 = m1.copy(m2)
311
+ assert_equal(m1.data, m2.data)
312
+ assert_equal(m1.data, m3.data)
313
+
314
+ rows, cols = m1.rows, m1.cols
315
+ mask = create_cvmat(rows, cols, CV_8U, 1) { |j, i, c|
316
+ val = (i > cols / 2) ? 0 : 255
317
+ CvScalar.new(val)
318
+ }
319
+
320
+ m2_orig = m2.copy
321
+ m3 = m1.copy(m2, mask)
322
+ rows.times { |j|
323
+ cols.times { |i|
324
+ expected = (mask[j, i][0] == 0) ? m2_orig[j, i] : m1[j, i]
325
+ assert_cvscalar_equal(expected, m2[j, i])
326
+ assert_cvscalar_equal(expected, m3[j, i])
327
+ }
328
+ }
329
+
330
+ assert_raise(TypeError) {
331
+ m1.copy(DUMMY_OBJ)
332
+ }
333
+ end
334
+
335
+ def test_convert_depth
336
+ m = CvMat.new(10, 20, :cv32f)
337
+ assert_equal(:cv8u, m.to_8u.depth)
338
+ assert_equal(:cv8s, m.to_8s.depth)
339
+ assert_equal(:cv16u, m.to_16u.depth)
340
+ assert_equal(:cv16s, m.to_16s.depth)
341
+ assert_equal(:cv32s, m.to_32s.depth)
342
+ assert_equal(:cv32f, m.to_32f.depth)
343
+ assert_equal(:cv64f, m.to_64f.depth)
344
+ end
345
+
346
+ def test_vector
347
+ m = CvMat.new(1, 2)
348
+ assert(m.vector?)
349
+
350
+ m = CvMat.new(2, 2)
351
+ assert((not m.vector?))
352
+ end
353
+
354
+ def test_square
355
+ m = CvMat.new(2, 2)
356
+ assert(m.square?)
357
+ m = CvMat.new(1, 2)
358
+ assert((not m.square?))
359
+ end
360
+
361
+ def test_to_CvMat
362
+ m1 = CvMat.new(2, 3, :cv32f, 4)
363
+ m2 = m1.to_CvMat
364
+ assert_equal(CvMat, m2.class)
365
+ assert_equal(m1.rows, m2.rows)
366
+ assert_equal(m1.cols, m2.cols)
367
+ assert_equal(m1.depth, m2.depth)
368
+ assert_equal(m1.channel, m2.channel)
369
+ assert_equal(m1.data, m2.data)
370
+ end
371
+
372
+ def test_sub_rect
373
+ m1 = create_cvmat(10, 10)
374
+
375
+ assert_raise(ArgumentError) {
376
+ m1.sub_rect
377
+ }
378
+
379
+ m2 = m1.sub_rect(CvRect.new(0, 0, 2, 3))
380
+ assert_equal(2, m2.width)
381
+ assert_equal(3, m2.height)
382
+ m2.height.times { |j|
383
+ m2.width.times { |i|
384
+ assert_cvscalar_equal(m1[j, i], m2[j, i])
385
+ }
386
+ }
387
+
388
+ topleft = CvPoint.new(2, 3)
389
+ m2 = m1.sub_rect(topleft, CvSize.new(4, 5))
390
+ assert_equal(4, m2.width)
391
+ assert_equal(5, m2.height)
392
+ m2.height.times { |j|
393
+ m2.width.times { |i|
394
+ assert_cvscalar_equal(m1[topleft.y + j, topleft.x + i], m2[j, i])
395
+ }
396
+ }
397
+
398
+ topleft = CvPoint.new(1, 2)
399
+ m2 = m1.sub_rect(topleft.x, topleft.y, 3, 4)
400
+ assert_equal(3, m2.width)
401
+ assert_equal(4, m2.height)
402
+ m2.height.times { |j|
403
+ m2.width.times { |i|
404
+ assert_cvscalar_equal(m1[topleft.y + j, topleft.x + i], m2[j, i])
405
+ }
406
+ }
407
+
408
+ # Alias
409
+ m2 = m1.subrect(CvRect.new(0, 0, 2, 3))
410
+ assert_equal(2, m2.width)
411
+ assert_equal(3, m2.height)
412
+ m2.height.times { |j|
413
+ m2.width.times { |i|
414
+ assert_cvscalar_equal(m1[j, i], m2[j, i])
415
+ }
416
+ }
417
+
418
+ assert_raise(TypeError) {
419
+ m1.sub_rect(DUMMY_OBJ)
420
+ }
421
+ assert_raise(TypeError) {
422
+ m1.sub_rect(DUMMY_OBJ, CvSize.new(1, 2))
423
+ }
424
+ assert_raise(TypeError) {
425
+ m1.sub_rect(CvPoint.new(1, 2), DUMMY_OBJ)
426
+ }
427
+ assert_raise(TypeError) {
428
+ m1.sub_rect(DUMMY_OBJ, 2, 3, 4)
429
+ }
430
+ assert_raise(TypeError) {
431
+ m1.sub_rect(1, DUMMY_OBJ, 3, 4)
432
+ }
433
+ assert_raise(TypeError) {
434
+ m1.sub_rect(1, 2, DUMMY_OBJ, 4)
435
+ }
436
+ assert_raise(TypeError) {
437
+ m1.sub_rect(1, 2, 3, DUMMY_OBJ)
438
+ }
439
+ end
440
+
441
+ def test_get_rows
442
+ m1 = create_cvmat(10, 20) { |j, i, c| CvScalar.new(c) }
443
+
444
+ row = 2
445
+ m2 = m1.get_rows(row)
446
+ assert_equal(1, m2.rows)
447
+ assert_equal(m1.cols, m2.cols)
448
+ m1.cols.times { |i|
449
+ assert_cvscalar_equal(m1[row, i], m2[i])
450
+ }
451
+
452
+ row1 = 3..7
453
+ row2 = 2...8
454
+ [row1, row2].each { |row|
455
+ m3 = m1.get_rows(row)
456
+ w = (row.exclude_end?) ? row.last - row.begin : row.last - row.begin + 1
457
+ assert_equal(w, m3.rows)
458
+ assert_equal(m1.cols, m3.cols)
459
+
460
+ m3.rows.times { |j|
461
+ m3.cols.times { |i|
462
+ assert_cvscalar_equal(m1[row.begin + j, i], m3[j, i])
463
+ }
464
+ }
465
+ }
466
+
467
+ [row1, row2].each { |row|
468
+ delta = 2
469
+ m3 = m1.get_rows(row, 2)
470
+ w = (((row.exclude_end?) ? row.last - row.begin : row.last - row.begin + 1).to_f / delta).ceil
471
+ assert_equal(w, m3.rows)
472
+ assert_equal(m1.cols, m3.cols)
473
+
474
+ m3.rows.times { |j|
475
+ m3.cols.times { |i|
476
+ assert_cvscalar_equal(m1[row.begin + j * delta, i], m3[j, i])
477
+ }
478
+ }
479
+ }
480
+
481
+ assert_raise(TypeError) {
482
+ m1.get_rows(DUMMY_OBJ)
483
+ }
484
+ assert_raise(TypeError) {
485
+ m1.get_rows(1, DUMMY_OBJ)
486
+ }
487
+ end
488
+
489
+ def test_get_cols
490
+ m1 = create_cvmat(10, 20) { |j, i, c| CvScalar.new(c) }
491
+
492
+ col = 2
493
+ m2 = m1.get_cols(col)
494
+ assert_equal(m1.rows, m2.rows)
495
+ assert_equal(1, m2.cols)
496
+ m1.height.times { |j|
497
+ assert_cvscalar_equal(m1[j, col], m2[j])
498
+ }
499
+
500
+ col1 = 3..7
501
+ col2 = 2...8
502
+ [col1, col2].each { |col|
503
+ m3 = m1.get_cols(col)
504
+ w = (col.exclude_end?) ? col.last - col.begin : col.last - col.begin + 1
505
+ assert_equal(m1.rows, m3.rows)
506
+ assert_equal(w, m3.cols)
507
+
508
+ m3.rows.times { |j|
509
+ m3.cols.times { |i|
510
+ assert_cvscalar_equal(m1[j, col.begin + i], m3[j, i])
511
+ }
512
+ }
513
+ }
514
+
515
+ assert_raise(TypeError) {
516
+ m1.get_cols(DUMMY_OBJ)
517
+ }
518
+ end
519
+
520
+ def test_each_row
521
+ m1 = create_cvmat(2, 3)
522
+ a = [[1, 2, 3], [4, 5, 6]]
523
+ a.map! { |a1|
524
+ a1.map! { |a2|
525
+ CvScalar.new(a2, a2, a2, a2).to_ary
526
+ }
527
+ }
528
+
529
+ j = 0
530
+ m1.each_row { |r|
531
+ a[j].size.times { |i|
532
+ assert_cvscalar_equal(a[j][i], r[i])
533
+ }
534
+ j += 1
535
+ }
536
+ end
537
+
538
+ def test_each_col
539
+ m1 = create_cvmat(2, 3)
540
+ a = [[1, 4], [2, 5], [3, 6]]
541
+ a.map! { |a1|
542
+ a1.map! { |a2|
543
+ CvScalar.new(a2, a2, a2, a2).to_ary
544
+ }
545
+ }
546
+
547
+ j = 0
548
+ m1.each_col { |c|
549
+ a[j].size.times { |i|
550
+ assert_cvscalar_equal(a[j][i], c[i])
551
+ }
552
+ j += 1
553
+ }
554
+
555
+ # Alias
556
+ j = 0
557
+ m1.each_column { |c|
558
+ a[j].size.times { |i|
559
+ assert_cvscalar_equal(a[j][i], c[i])
560
+ }
561
+ j += 1
562
+ }
563
+ end
564
+
565
+ def test_diag
566
+ m = create_cvmat(5, 5)
567
+ a = [1, 7, 13, 19, 25].map { |x| CvScalar.new(x, x, x, x) }
568
+ d = m.diag
569
+
570
+ a.each_with_index { |s, i|
571
+ assert_cvscalar_equal(s, d[i])
572
+ }
573
+
574
+ a = [2, 8, 14, 20].map { |x| CvScalar.new(x, x, x, x) }
575
+ d = m.diag(1)
576
+ a.each_with_index { |s, i|
577
+ assert_cvscalar_equal(s, d[i])
578
+ }
579
+
580
+ a = [6, 12, 18, 24].map { |x| CvScalar.new(x, x, x, x) }
581
+ d = m.diag(-1)
582
+ a.each_with_index { |s, i|
583
+ assert_cvscalar_equal(s, d[i])
584
+ }
585
+
586
+ # Alias
587
+ a = [1, 7, 13, 19, 25].map { |x| CvScalar.new(x, x, x, x) }
588
+ d = m.diagonal
589
+ a.each_with_index { |s, i|
590
+ assert_cvscalar_equal(s, d[i])
591
+ }
592
+
593
+ [m.rows, m.cols, -m.rows, -m.cols].each { |d|
594
+ assert_raise(CvStsOutOfRange) {
595
+ m.diag(d)
596
+ }
597
+ }
598
+ end
599
+
600
+ def test_size
601
+ m = CvMat.new(2, 3)
602
+ assert_equal(3, m.size.width)
603
+ assert_equal(2, m.size.height)
604
+ end
605
+
606
+ def test_dims
607
+ m = CvMat.new(2, 3)
608
+ assert_equal([2, 3], m.dims)
609
+ end
610
+
611
+ def test_dim_size
612
+ m = CvMat.new(2, 3)
613
+ assert_equal(2, m.dim_size(0))
614
+ assert_equal(3, m.dim_size(1))
615
+
616
+ assert_raise(TypeError) {
617
+ m.dim_size(DUMMY_OBJ)
618
+ }
619
+ end
620
+
621
+ def test_aref
622
+ m = create_cvmat(2, 3)
623
+ assert_cvscalar_equal(CvScalar.new(1, 1, 1, 1), m[0])
624
+ assert_cvscalar_equal(CvScalar.new(5, 5, 5, 5), m[4])
625
+ assert_cvscalar_equal(CvScalar.new(2, 2, 2, 2), m[0, 1])
626
+ assert_cvscalar_equal(CvScalar.new(4, 4, 4, 4), m[1, 0])
627
+ assert_cvscalar_equal(CvScalar.new(2, 2, 2, 2), m[0, 1, 2])
628
+ assert_cvscalar_equal(CvScalar.new(4, 4, 4, 4), m[1, 0, 3, 4])
629
+
630
+ # Alias
631
+ assert_cvscalar_equal(CvScalar.new(1, 1, 1, 1), m.at(0))
632
+
633
+ assert_raise(TypeError) {
634
+ m[DUMMY_OBJ]
635
+ }
636
+
637
+ assert_raise(CvStsOutOfRange) {
638
+ m[-1]
639
+ }
640
+ assert_raise(CvStsOutOfRange) {
641
+ m[6]
642
+ }
643
+ assert_raise(CvStsOutOfRange) {
644
+ m[2, 2]
645
+ }
646
+ assert_raise(CvStsOutOfRange) {
647
+ m[1, 3]
648
+ }
649
+ assert_raise(CvStsOutOfRange) {
650
+ m[2, 2, 1]
651
+ }
652
+ assert_raise(CvStsOutOfRange) {
653
+ m[1, 3, 1]
654
+ }
655
+ end
656
+
657
+ def test_aset
658
+ m = create_cvmat(2, 3)
659
+ m[0] = CvScalar.new(10, 10, 10, 10)
660
+ assert_cvscalar_equal(CvScalar.new(10, 10, 10, 10), m[0])
661
+ m[1, 0] = CvScalar.new(20, 20, 20, 20)
662
+ assert_cvscalar_equal(CvScalar.new(20, 20, 20, 20), m[1, 0])
663
+ m[1, 0, 2] = CvScalar.new(4, 4, 4, 4)
664
+ assert_cvscalar_equal(CvScalar.new(4, 4, 4, 4), m[1, 0])
665
+ m[1, 0, 2, 4] = CvScalar.new(5, 5, 5, 5)
666
+ assert_cvscalar_equal(CvScalar.new(5, 5, 5, 5), m[1, 0])
667
+
668
+ assert_raise(TypeError) {
669
+ m[DUMMY_OBJ] = CvScalar.new(10, 10, 10, 10)
670
+ }
671
+ assert_raise(TypeError) {
672
+ m[0] = DUMMY_OBJ
673
+ }
674
+
675
+ assert_raise(CvStsOutOfRange) {
676
+ m[-1]
677
+ }
678
+ assert_raise(CvStsOutOfRange) {
679
+ m[6]
680
+ }
681
+ assert_raise(CvStsOutOfRange) {
682
+ m[2, 2]
683
+ }
684
+ assert_raise(CvStsOutOfRange) {
685
+ m[1, 3]
686
+ }
687
+ assert_raise(CvStsOutOfRange) {
688
+ m[2, 2, 1]
689
+ }
690
+ assert_raise(CvStsOutOfRange) {
691
+ m[1, 3, 1]
692
+ }
693
+ end
694
+
695
+ def test_set_data
696
+ [CV_8U, CV_8S, CV_16U, CV_16S, CV_32S].each { |depth|
697
+ a = [10, 20, 30, 40, 50, 60]
698
+ m = CvMat.new(2, 3, depth, 1)
699
+ m.set_data(a)
700
+ (m.rows * m.cols).times { |i|
701
+ assert_equal(a[i], m[i][0])
702
+ }
703
+ }
704
+
705
+ [CV_32F, CV_64F].each { |depth|
706
+ a = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
707
+ m = CvMat.new(2, 3, depth, 1)
708
+ m.set_data(a)
709
+ (m.rows * m.cols).times { |i|
710
+ assert_in_delta(a[i], m[i][0], 1.0e-5)
711
+ }
712
+ }
713
+
714
+ a = [[10, 20, 30], [40, 50, 60]]
715
+ m = CvMat.new(2, 3, CV_8U, 1)
716
+ m.set_data(a)
717
+ (m.rows * m.cols).times { |i|
718
+ assert_equal(a.flatten[i], m[i][0])
719
+ }
720
+
721
+ [CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F].each { |depth|
722
+ m = CvMat.new(2, 3, depth, 1)
723
+ assert_raise(TypeError) {
724
+ a = [DUMMY_OBJ] * 6
725
+ m.set_data(a)
726
+ }
727
+ }
728
+ end
729
+
730
+ def test_fill
731
+ m1 = create_cvmat(2, 3)
732
+ m2 = m1.fill(CvScalar.new(1, 2, 3, 4))
733
+ m1.fill!(CvScalar.new(1, 2, 3, 4))
734
+ m2.height.times { |j|
735
+ m2.width.times { |i|
736
+ assert_cvscalar_equal(CvScalar.new(1, 2, 3, 4), m1[j, i])
737
+ assert_cvscalar_equal(CvScalar.new(1, 2, 3, 4), m2[j, i])
738
+ }
739
+ }
740
+
741
+ m1 = create_cvmat(5, 5)
742
+ m0 = m1.clone
743
+ mask = CvMat.new(m1.height, m1.width, :cv8u, 1).clear
744
+ 2.times { |j|
745
+ 2.times { |i|
746
+ mask[j, i] = CvScalar.new(1, 1, 1, 1)
747
+ }
748
+ }
749
+
750
+ m2 = m1.fill(CvScalar.new(1, 2, 3, 4), mask)
751
+ m1.fill!(CvScalar.new(1, 2, 3, 4), mask)
752
+ m2.height.times { |j|
753
+ m2.width.times { |i|
754
+ if i < 2 and j < 2
755
+ assert_cvscalar_equal(CvScalar.new(1, 2, 3, 4), m1[j, i])
756
+ assert_cvscalar_equal(CvScalar.new(1, 2, 3, 4), m2[j, i])
757
+ else
758
+ assert_cvscalar_equal(m0[j, i], m1[j, i])
759
+ assert_cvscalar_equal(m0[j, i], m2[j, i])
760
+ end
761
+ }
762
+ }
763
+
764
+ # Alias
765
+ m1 = create_cvmat(2, 3)
766
+ m2 = m1.set(CvScalar.new(1, 2, 3, 4))
767
+ m1.set!(CvScalar.new(1, 2, 3, 4))
768
+ m2.height.times { |j|
769
+ m2.width.times { |i|
770
+ assert_cvscalar_equal(CvScalar.new(1, 2, 3, 4), m1[j, i])
771
+ assert_cvscalar_equal(CvScalar.new(1, 2, 3, 4), m2[j, i])
772
+ }
773
+ }
774
+
775
+ m1 = create_cvmat(5, 5)
776
+ m0 = m1.clone
777
+ mask = CvMat.new(m1.height, m1.width, CV_8U, 1).clear
778
+ 2.times { |j|
779
+ 2.times { |i|
780
+ mask[j, i] = CvScalar.new(1, 1, 1, 1)
781
+ }
782
+ }
783
+
784
+ m2 = m1.set(CvScalar.new(1, 2, 3, 4), mask)
785
+ m1.set!(CvScalar.new(1, 2, 3, 4), mask)
786
+ m2.height.times { |j|
787
+ m2.width.times { |i|
788
+ if i < 2 and j < 2
789
+ assert_cvscalar_equal(CvScalar.new(1, 2, 3, 4), m1[j, i])
790
+ assert_cvscalar_equal(CvScalar.new(1, 2, 3, 4), m2[j, i])
791
+ else
792
+ assert_cvscalar_equal(m0[j, i], m1[j, i])
793
+ assert_cvscalar_equal(m0[j, i], m2[j, i])
794
+ end
795
+ }
796
+ }
797
+
798
+ assert_raise(TypeError) {
799
+ m1.fill(DUMMY_OBJ)
800
+ }
801
+ assert_raise(TypeError) {
802
+ m1.fill(CvScalar.new(1), DUMMY_OBJ)
803
+ }
804
+ end
805
+
806
+ def test_clear
807
+ m1 = create_cvmat(2, 3)
808
+ m2 = m1.clear
809
+ m1.clear!
810
+ m2.height.times { |j|
811
+ m2.width.times { |i|
812
+ assert_cvscalar_equal(CvScalar.new(0, 0, 0, 0), m1[j, i])
813
+ assert_cvscalar_equal(CvScalar.new(0, 0, 0, 0), m2[j, i])
814
+ }
815
+ }
816
+
817
+ # Alias
818
+ m1 = create_cvmat(2, 3)
819
+ m2 = m1.set_zero
820
+ m1.set_zero!
821
+ m3 = create_cvmat(2, 3)
822
+ m4 = m3.zero
823
+ m3.zero!
824
+ m2.height.times { |j|
825
+ m2.width.times { |i|
826
+ assert_cvscalar_equal(CvScalar.new(0, 0, 0, 0), m1[j, i])
827
+ assert_cvscalar_equal(CvScalar.new(0, 0, 0, 0), m2[j, i])
828
+ assert_cvscalar_equal(CvScalar.new(0, 0, 0, 0), m3[j, i])
829
+ assert_cvscalar_equal(CvScalar.new(0, 0, 0, 0), m4[j, i])
830
+ }
831
+ }
832
+ end
833
+
834
+ def test_identity
835
+ m1 = create_cvmat(5, 5)
836
+ m2 = m1.identity
837
+ m1.identity!
838
+ m2.height.times { |j|
839
+ m2.width.times { |i|
840
+ if i == j
841
+ assert_cvscalar_equal(CvScalar.new(1, 0, 0, 0), m1[j, i])
842
+ assert_cvscalar_equal(CvScalar.new(1, 0, 0, 0), m2[j, i])
843
+ else
844
+ assert_cvscalar_equal(CvScalar.new(0, 0, 0, 0), m1[j, i])
845
+ assert_cvscalar_equal(CvScalar.new(0, 0, 0, 0), m2[j, i])
846
+ end
847
+ }
848
+ }
849
+
850
+ m1 = CvMat.new(5, 5, :cv8u, 4)
851
+ s = CvScalar.new(1, 2, 3, 4)
852
+ m2 = m1.identity(s)
853
+ m1.identity!(s)
854
+ m2.height.times { |j|
855
+ m2.width.times { |i|
856
+ if i == j
857
+ assert_cvscalar_equal(s, m1[j, i])
858
+ assert_cvscalar_equal(s, m2[j, i])
859
+ else
860
+ assert_cvscalar_equal(CvScalar.new(0, 0, 0, 0), m1[j, i])
861
+ assert_cvscalar_equal(CvScalar.new(0, 0, 0, 0), m2[j, i])
862
+ end
863
+ }
864
+ }
865
+
866
+ assert_raise(TypeError) {
867
+ m1.identity(DUMMY_OBJ)
868
+ }
869
+ end
870
+
871
+ def test_range
872
+ m1 = CvMat.new(1, 10, CV_32S, 1)
873
+ m2 = m1.range(0, m1.cols)
874
+ m1.range!(0, m1.cols)
875
+ m2.width.times { |i|
876
+ assert_cvscalar_equal(CvScalar.new(i, 0, 0, 0), m1[0, i])
877
+ assert_cvscalar_equal(CvScalar.new(i, 0, 0, 0), m2[0, i])
878
+ }
879
+
880
+ assert_raise(TypeError) {
881
+ m1.range(DUMMY_OBJ, 2)
882
+ }
883
+ assert_raise(TypeError) {
884
+ m1.range(1, DUMMY_OBJ)
885
+ }
886
+ end
887
+
888
+ def test_reshape
889
+ m = create_cvmat(2, 3, CV_8U, 3)
890
+
891
+ vec = m.reshape(:rows => 1)
892
+ assert_equal(6, vec.width)
893
+ assert_equal(1, vec.height)
894
+ size = m.width * m.height
895
+ size.times { |i|
896
+ assert_cvscalar_equal(m[i], vec[i])
897
+ }
898
+
899
+ ch1 = m.reshape(:channel => 1)
900
+ assert_equal(9, ch1.width)
901
+ assert_equal(2, ch1.height)
902
+
903
+ m.height.times { |j|
904
+ m.width.times { |i|
905
+ s1 = ch1[j, i * 3][0]
906
+ s2 = ch1[j, i * 3 + 1][0]
907
+ s3 = ch1[j, i * 3 + 2][0]
908
+ assert_cvscalar_equal(m[j, i], CvScalar.new(s1, s2, s3, 0))
909
+ }
910
+ }
911
+
912
+ [DUMMY_OBJ, { :rows => DUMMY_OBJ }, { :channel => DUMMY_OBJ }].each { |arg|
913
+ assert_raise(TypeError) {
914
+ m.reshape(arg)
915
+ }
916
+ }
917
+ end
918
+
919
+ def test_repeat
920
+ m1 = create_cvmat(2, 3, :cv8u, 3)
921
+ m2 = CvMat.new(6, 9, :cv8u, 3)
922
+ m2 = m1.repeat(m2)
923
+ m2.height.times { |j|
924
+ m2.width.times { |i|
925
+ a = m1[j % m1.height, i % m1.width]
926
+ assert_cvscalar_equal(m2[j, i], a)
927
+ }
928
+ }
929
+ assert_raise(TypeError) {
930
+ m1.repeat(DUMMY_OBJ)
931
+ }
932
+ end
933
+
934
+ def test_flip
935
+ m0 = create_cvmat(2, 3)
936
+
937
+ m1 = m0.clone
938
+ m1.flip!(:x)
939
+ m2 = m0.flip(:x)
940
+ m3 = m0.clone
941
+ m3.flip!(:y)
942
+ m4 = m0.flip(:y)
943
+ m5 = m0.clone
944
+ m5.flip!(:xy)
945
+ m6 = m0.flip(:xy)
946
+ m7 = m0.clone
947
+ m7.flip!
948
+ m8 = m0.flip
949
+
950
+ [m1, m2, m3, m4, m5, m6, m7, m8].each { |m|
951
+ assert_equal(m0.height, m.height)
952
+ assert_equal(m0.width, m.width)
953
+ }
954
+ m0.height.times { |j|
955
+ m0.width.times { |i|
956
+ ri = m0.width - i - 1
957
+ rj = m0.height - j - 1
958
+ assert_cvscalar_equal(m0[j, ri], m1[j, i])
959
+ assert_cvscalar_equal(m0[j, ri], m2[j, i])
960
+ assert_cvscalar_equal(m0[rj, i], m3[j, i])
961
+ assert_cvscalar_equal(m0[rj, i], m4[j, i])
962
+ assert_cvscalar_equal(m0[rj, ri], m5[j, i])
963
+ assert_cvscalar_equal(m0[rj, ri], m6[j, i])
964
+ assert_cvscalar_equal(m0[j, ri], m7[j, i])
965
+ assert_cvscalar_equal(m0[j, ri], m8[j, i])
966
+ }
967
+ }
968
+
969
+ assert_raise(TypeError) {
970
+ m0.flip(DUMMY_OBJ)
971
+ }
972
+ assert_raise(TypeError) {
973
+ m0.flip!(DUMMY_OBJ)
974
+ }
975
+ end
976
+
977
+ def test_split
978
+ m0 = create_cvmat(2, 3, :cv8u, 3) { |j, i, c|
979
+ CvScalar.new(c * 10, c * 20, c * 30)
980
+ }
981
+
982
+ splitted = m0.split
983
+ assert_equal(m0.channel, splitted.size)
984
+ splitted.each_with_index { |m, idx|
985
+ assert_equal(CvMat, m.class)
986
+ assert_equal(m0.height, m.height)
987
+ assert_equal(m0.width, m.width)
988
+ assert_equal(1, m.channel)
989
+
990
+ c = 0
991
+ m0.height.times { |j|
992
+ m0.width.times { |i|
993
+ val = c * 10 * (idx + 1)
994
+ assert_cvscalar_equal(CvScalar.new(val), m[j, i])
995
+ c += 1
996
+ }
997
+ }
998
+ }
999
+
1000
+ # IplImage#split should return Array<IplImage>
1001
+ image = create_iplimage(2, 3, :cv8u, 3) { |j, i, c|
1002
+ CvScalar.new(c * 10, c * 20, c * 30)
1003
+ }
1004
+
1005
+ splitted = image.split
1006
+ assert_equal(3, splitted.size)
1007
+ splitted.each_with_index { |img, channel|
1008
+ assert_equal(IplImage, img.class)
1009
+ assert_equal(image.height, img.height)
1010
+ assert_equal(image.width, img.width)
1011
+ assert_equal(1, img.channel)
1012
+
1013
+ img.height.times { |j|
1014
+ img.width.times { |i|
1015
+ val = image[j, i][channel]
1016
+ assert_cvscalar_equal(CvScalar.new(val), img[j, i])
1017
+ }
1018
+ }
1019
+ }
1020
+ end
1021
+
1022
+ def test_merge
1023
+ m0 = create_cvmat(2, 3, :cv8u, 4) { |j, i, c|
1024
+ CvScalar.new(c * 10, c * 20, c * 30, c * 40)
1025
+ }
1026
+ m1 = create_cvmat(2, 3, :cv8u, 1) { |j, i, c|
1027
+ CvScalar.new(c * 10)
1028
+ }
1029
+ m2 = create_cvmat(2, 3, :cv8u, 1) { |j, i, c|
1030
+ CvScalar.new(c * 20)
1031
+ }
1032
+ m3 = create_cvmat(2, 3, :cv8u, 1) { |j, i, c|
1033
+ CvScalar.new(c * 30)
1034
+ }
1035
+ m4 = create_cvmat(2, 3, :cv8u, 1) { |j, i, c|
1036
+ CvScalar.new(c * 40)
1037
+ }
1038
+
1039
+ m = CvMat.merge(m1, m2, m3, m4)
1040
+
1041
+ assert_equal(m0.height, m.height)
1042
+ assert_equal(m0.width, m.width)
1043
+ m0.height.times { |j|
1044
+ m0.width.times { |i|
1045
+ assert_cvscalar_equal(m0[j, i], m[j, i])
1046
+ }
1047
+ }
1048
+
1049
+ m5 = create_cvmat(2, 3, :cv8u, 1) { |j, i, c|
1050
+ CvScalar.new(c * 50)
1051
+ }
1052
+
1053
+ assert_raise(TypeError) {
1054
+ CvMat.merge(DUMMY_OBJ)
1055
+ }
1056
+ assert_raise(ArgumentError) {
1057
+ CvMat.merge
1058
+ }
1059
+ assert_raise(ArgumentError) {
1060
+ CvMat.merge(m1, m2, m3, m4, m5)
1061
+ }
1062
+ assert_raise(ArgumentError) {
1063
+ CvMat.merge(CvMat.new(1, 2, :cv8u, 2))
1064
+ }
1065
+ assert_raise(ArgumentError) {
1066
+ CvMat.merge(CvMat.new(1, 2, :cv8u, 1),
1067
+ CvMat.new(2, 2, :cv8u, 1))
1068
+ }
1069
+ assert_raise(ArgumentError) {
1070
+ CvMat.merge(CvMat.new(1, 2, :cv8u, 1),
1071
+ CvMat.new(1, 2, :cv32f, 1))
1072
+ }
1073
+ end
1074
+
1075
+ def test_rand_shuffle
1076
+ m0 = create_cvmat(2, 3)
1077
+ m1 = m0.clone
1078
+ m1.rand_shuffle!
1079
+ m2 = m0.rand_shuffle
1080
+ m3 = m0.clone
1081
+ m3.rand_shuffle!(123, 234)
1082
+ m4 = m0.rand_shuffle(123, 234)
1083
+
1084
+ assert_shuffled_equal = lambda { |src, shuffled|
1085
+ assert_equal(src.width, shuffled.width)
1086
+ assert_equal(src.height, shuffled.height)
1087
+ mat0, mat1 = [], []
1088
+ src.height { |j|
1089
+ src.width { |i|
1090
+ mat0 << src[j, i].to_s
1091
+ mat1 << shuffled[j, i].to_s
1092
+ }
1093
+ }
1094
+ assert_equal(0, (mat0 - mat1).size)
1095
+ }
1096
+
1097
+ [m1, m2, m3, m4].each { |m|
1098
+ assert_shuffled_equal.call(m0, m)
1099
+ }
1100
+
1101
+ assert_raise(TypeError) {
1102
+ m0.rand_shuffle(DUMMY_OBJ)
1103
+ }
1104
+ assert_raise(TypeError) {
1105
+ m0.rand_shuffle(123, DUMMY_OBJ)
1106
+ }
1107
+ end
1108
+
1109
+ def test_lut
1110
+ m0 = create_cvmat(2, 3, :cv8u, 3)
1111
+ lut_mat = create_cvmat(1, 256, :cv8u, 3) { |j, i, c|
1112
+ CvScalar.new(255 - c, 255 - c, 255 - c)
1113
+ }
1114
+
1115
+ m = m0.lut(lut_mat)
1116
+ assert_equal(m0.height, m.height)
1117
+ assert_equal(m0.width, m.width)
1118
+ m0.height.times { |j|
1119
+ m0.width.times { |i|
1120
+ r, g, b = m0[j, i].to_ary.map { |c| 255 - c }
1121
+ assert_cvscalar_equal(CvScalar.new(r, g, b, 0), m[j, i])
1122
+ }
1123
+ }
1124
+
1125
+ assert_raise(TypeError) {
1126
+ m0.lut(DUMMY_OBJ)
1127
+ }
1128
+ end
1129
+
1130
+ def test_convert_scale
1131
+ m0 = create_cvmat(2, 3, :cv32f, 4) { |j, i, c|
1132
+ CvScalar.new(-c, -c, -c, -c)
1133
+ }
1134
+
1135
+ m1 = m0.convert_scale(:depth => :cv8u)
1136
+ m2 = m0.convert_scale(:scale => 1.5)
1137
+ m3 = m0.convert_scale(:shift => 10.0)
1138
+ m4 = m0.convert_scale(:depth => CV_16U)
1139
+
1140
+ [m1, m2, m3, m4].each { |m|
1141
+ assert_equal(m0.height, m.height)
1142
+ assert_equal(m0.width, m.width)
1143
+ }
1144
+ m0.height.times { |j|
1145
+ m0.width.times { |i|
1146
+ assert_cvscalar_equal(CvScalar.new(0, 0, 0, 0), m1[j, i])
1147
+ a = m0[j, i].to_ary.map { |x| x * 1.5 }
1148
+ assert_in_delta(a, m2[j, i], 0.001)
1149
+ a = m0[j, i].to_ary.map { |x| x + 10.0 }
1150
+ assert_in_delta(a, m3[j, i], 0.001)
1151
+ assert_cvscalar_equal(CvScalar.new(0, 0, 0, 0), m4[j, i])
1152
+ }
1153
+ }
1154
+
1155
+ assert_raise(TypeError) {
1156
+ m0.convert_scale(DUMMY_OBJ)
1157
+ }
1158
+ end
1159
+
1160
+ def test_convert_scale_abs
1161
+ m0 = create_cvmat(2, 3, :cv8u, 4) { |j, i, c|
1162
+ CvScalar.new(c, c, c, c)
1163
+ }
1164
+
1165
+ m1 = m0.convert_scale_abs(:depth => :cv64f)
1166
+ m2 = m0.convert_scale_abs(:scale => 2)
1167
+ m3 = m0.convert_scale_abs(:shift => 10.0)
1168
+ m4 = m0.convert_scale_abs(:depth => CV_64F)
1169
+
1170
+ [m1, m2, m3, m4].each { |m|
1171
+ assert_equal(m0.height, m.height)
1172
+ assert_equal(m0.width, m.width)
1173
+ }
1174
+ m0.height.times { |j|
1175
+ m0.width.times { |i|
1176
+ assert_cvscalar_equal(m0[j, i], m1[j, i])
1177
+ a = m0[j, i].to_ary.map { |x| (x * 2).abs }
1178
+ assert_in_delta(a, m2[j, i], 0.001)
1179
+ a = m0[j, i].to_ary.map { |x| (x + 10.0).abs }
1180
+ assert_in_delta(a, m3[j, i], 0.001)
1181
+ assert_cvscalar_equal(m0[j, i], m4[j, i])
1182
+ }
1183
+ }
1184
+
1185
+ assert_raise(TypeError) {
1186
+ m0.convert_scale(DUMMY_OBJ)
1187
+ }
1188
+ end
1189
+
1190
+ def test_add
1191
+ m1 = create_cvmat(6, 4, :cv32f, 4) { |j, i, c|
1192
+ CvScalar.new(c * 0.1, c * 0.2, c * 0.3, c * 0.4)
1193
+ }
1194
+ m2 = create_cvmat(6, 4, :cv32f, 4) { |j, i, c|
1195
+ CvScalar.new(c * 1, c * 2, c * 3, c * 4)
1196
+ }
1197
+
1198
+ # CvMat + CvMat
1199
+ m3 = m1.add(m2)
1200
+ assert_equal(m1.height, m3.height)
1201
+ assert_equal(m1.width, m3.width)
1202
+ n = 0
1203
+ m1.height.times { |j|
1204
+ m1.width.times { |i|
1205
+ s = CvScalar.new(n * 1.1, n * 2.2, n * 3.3, n * 4.4)
1206
+ assert_in_delta(s, m3[j, i], 0.001)
1207
+ n += 1
1208
+ }
1209
+ }
1210
+
1211
+ # CvMat + CvScalar
1212
+ s1 = CvScalar.new(1, 2, 3, 4)
1213
+ m3 = m1.add(s1)
1214
+ assert_equal(m1.height, m3.height)
1215
+ assert_equal(m1.width, m3.width)
1216
+ n = 0
1217
+ m1.height.times { |j|
1218
+ m1.width.times { |i|
1219
+ s = CvScalar.new(n * 0.1 + 1, n * 0.2 + 2, n * 0.3 + 3, n * 0.4 + 4)
1220
+ assert_in_delta(s, m3[j, i], 0.001)
1221
+ n += 1
1222
+ }
1223
+ }
1224
+
1225
+ # Alias
1226
+ m3 = m1 + m2
1227
+ assert_equal(m1.height, m3.height)
1228
+ assert_equal(m1.width, m3.width)
1229
+ n = 0
1230
+ m1.height.times { |j|
1231
+ m1.width.times { |i|
1232
+ s = CvScalar.new(n * 1.1, n * 2.2, n * 3.3, n * 4.4)
1233
+ assert_in_delta(s, m3[j, i], 0.001)
1234
+ n += 1
1235
+ }
1236
+ }
1237
+
1238
+ # CvMat + CvMat with Mask
1239
+ mask = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
1240
+ (i < 3 and j < 2) ? 1 : 0
1241
+ }
1242
+
1243
+ m4 = m1.add(m2, mask)
1244
+ assert_equal(m1.height, m4.height)
1245
+ assert_equal(m1.width, m4.width)
1246
+ n = 0
1247
+ m1.height.times { |j|
1248
+ m1.width.times { |i|
1249
+ if i < 3 and j < 2
1250
+ s = CvScalar.new(n * 1.1, n * 2.2, n * 3.3, n * 4.4)
1251
+ else
1252
+ s = m1[j, i]
1253
+ end
1254
+ assert_in_delta(s, m4[j, i], 0.001)
1255
+ n += 1
1256
+ }
1257
+ }
1258
+
1259
+ # CvMat + CvScalar with Mask
1260
+ m4 = m1.add(s1, mask)
1261
+ assert_equal(m1.height, m4.height)
1262
+ assert_equal(m1.width, m4.width)
1263
+ n = 0
1264
+ m1.height.times { |j|
1265
+ m1.width.times { |i|
1266
+ if i < 3 and j < 2
1267
+ s = CvScalar.new(n * 0.1 + 1, n * 0.2 + 2, n * 0.3 + 3, n * 0.4 + 4)
1268
+ else
1269
+ s = m1[j, i]
1270
+ end
1271
+ assert_in_delta(s, m4[j, i], 0.001)
1272
+ n += 1
1273
+ }
1274
+ }
1275
+
1276
+ assert_raise(TypeError) {
1277
+ m1.add(DUMMY_OBJ)
1278
+ }
1279
+ assert_raise(TypeError) {
1280
+ m1.add(CvScalar.new(1), DUMMY_OBJ)
1281
+ }
1282
+ end
1283
+
1284
+ def test_sub
1285
+ m1 = create_cvmat(6, 4, :cv32f, 4) { |j, i, c|
1286
+ CvScalar.new(c * 0.1, c * 0.2, c * 0.3, c * 0.4)
1287
+ }
1288
+ m2 = create_cvmat(6, 4, :cv32f, 4) { |j, i, c|
1289
+ CvScalar.new(c * 1, c * 2, c * 3, c * 4)
1290
+ }
1291
+
1292
+ # CvMat - CvMat
1293
+ m3 = m1.sub(m2)
1294
+ assert_equal(m1.height, m3.height)
1295
+ assert_equal(m1.width, m3.width)
1296
+ n = 0
1297
+ m1.height.times { |j|
1298
+ m1.width.times { |i|
1299
+ s = CvScalar.new(-n * 0.9, -n * 1.8, -n * 2.7, -n * 3.6)
1300
+ assert_in_delta(s, m3[j, i], 0.001)
1301
+ n += 1
1302
+ }
1303
+ }
1304
+
1305
+ # CvMat - CvScalar
1306
+ s1 = CvScalar.new(1, 2, 3, 4)
1307
+ m3 = m1.sub(s1)
1308
+ assert_equal(m1.height, m3.height)
1309
+ assert_equal(m1.width, m3.width)
1310
+ n = 0
1311
+ m1.height.times { |j|
1312
+ m1.width.times { |i|
1313
+ s = CvScalar.new(n * 0.1 - 1, n * 0.2 - 2, n * 0.3 - 3, n * 0.4 - 4)
1314
+ assert_in_delta(s, m3[j, i], 0.001)
1315
+ n += 1
1316
+ }
1317
+ }
1318
+
1319
+ # Alias
1320
+ m3 = m1 - m2
1321
+ assert_equal(m1.height, m3.height)
1322
+ assert_equal(m1.width, m3.width)
1323
+ n = 0
1324
+ m1.height.times { |j|
1325
+ m1.width.times { |i|
1326
+ s = CvScalar.new(-n * 0.9, -n * 1.8, -n * 2.7, -n * 3.6)
1327
+ assert_in_delta(s, m3[j, i], 0.001)
1328
+ n += 1
1329
+ }
1330
+ }
1331
+
1332
+ mask = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
1333
+ (i < 3 and j < 2) ? 1 : 0
1334
+ }
1335
+
1336
+ # CvMat - CvMat with Mask
1337
+ m4 = m1.sub(m2, mask)
1338
+ assert_equal(m1.height, m4.height)
1339
+ assert_equal(m1.width, m4.width)
1340
+ n = 0
1341
+ m1.height.times { |j|
1342
+ m1.width.times { |i|
1343
+ if i < 3 and j < 2
1344
+ s = CvScalar.new(-n * 0.9, -n * 1.8, -n * 2.7, -n * 3.6)
1345
+ else
1346
+ s = m1[j, i]
1347
+ end
1348
+ assert_in_delta(s, m4[j, i], 0.001)
1349
+ n += 1
1350
+ }
1351
+ }
1352
+
1353
+ # CvMat - CvScalar with Mask
1354
+ m4 = m1.sub(s1, mask)
1355
+ assert_equal(m1.height, m4.height)
1356
+ assert_equal(m1.width, m4.width)
1357
+ n = 0
1358
+ m1.height.times { |j|
1359
+ m1.width.times { |i|
1360
+ if i < 3 and j < 2
1361
+ s = CvScalar.new(n * 0.1 - 1, n * 0.2 - 2, n * 0.3 - 3, n * 0.4 - 4)
1362
+ else
1363
+ s = m1[j, i]
1364
+ end
1365
+ assert_in_delta(s, m4[j, i], 0.001)
1366
+ n += 1
1367
+ }
1368
+ }
1369
+
1370
+ assert_raise(TypeError) {
1371
+ m1.sub(DUMMY_OBJ)
1372
+ }
1373
+ assert_raise(TypeError) {
1374
+ m1.sub(CvScalar.new(1), DUMMY_OBJ)
1375
+ }
1376
+ end
1377
+
1378
+ def test_mul
1379
+ m1 = create_cvmat(3, 3, :cv32f)
1380
+ s1 = CvScalar.new(0.1, 0.2, 0.3, 0.4)
1381
+ m2 = create_cvmat(3, 3, :cv32f) { s1 }
1382
+
1383
+ # CvMat * CvMat
1384
+ m3 = m1.mul(m2)
1385
+ assert_equal(m1.height, m3.height)
1386
+ assert_equal(m1.width, m3.width)
1387
+ assert_each_cvscalar(m3, 0.001) { |j, i, c|
1388
+ n = c + 1
1389
+ CvScalar.new(n * 0.1, n * 0.2, n * 0.3, n * 0.4)
1390
+ }
1391
+
1392
+ # CvMat * CvMat * scale
1393
+ scale = 2.5
1394
+ m3 = m1.mul(m2, scale)
1395
+ assert_equal(m1.height, m3.height)
1396
+ assert_equal(m1.width, m3.width)
1397
+ assert_each_cvscalar(m3, 0.001) { |j, i, c|
1398
+ n = (c + 1) * scale
1399
+ CvScalar.new(n * 0.1, n * 0.2, n * 0.3, n * 0.4)
1400
+ }
1401
+
1402
+ # CvMat * CvScalar
1403
+ scale = 2.5
1404
+ m3 = m1.mul(s1)
1405
+ assert_equal(m1.height, m3.height)
1406
+ assert_equal(m1.width, m3.width)
1407
+ assert_each_cvscalar(m3, 0.001) { |j, i, c|
1408
+ n = c + 1
1409
+ CvScalar.new(n * 0.1, n * 0.2, n * 0.3, n * 0.4)
1410
+ }
1411
+
1412
+ # CvMat * CvScalar * scale
1413
+ m3 = m1.mul(s1, scale)
1414
+ assert_equal(m1.height, m3.height)
1415
+ assert_equal(m1.width, m3.width)
1416
+ assert_each_cvscalar(m3, 0.001) { |j, i, c|
1417
+ n = (c + 1) * scale
1418
+ CvScalar.new(n * 0.1, n * 0.2, n * 0.3, n * 0.4)
1419
+ }
1420
+
1421
+ assert_raise(TypeError) {
1422
+ m1.mul(DUMMY_OBJ)
1423
+ }
1424
+ assert_raise(TypeError) {
1425
+ m1.mul(m2, DUMMY_OBJ)
1426
+ }
1427
+ end
1428
+
1429
+ def test_mat_mul
1430
+ m0 = create_cvmat(3, 3, :cv32f, 1) { |j, i, c|
1431
+ CvScalar.new(c * 0.1)
1432
+ }
1433
+ m1 = create_cvmat(3, 3, :cv32f, 1) { |j, i, c|
1434
+ CvScalar.new(c)
1435
+ }
1436
+ m2 = create_cvmat(3, 3, :cv32f, 1) { |j, i, c|
1437
+ CvScalar.new(c + 1)
1438
+ }
1439
+
1440
+ m3 = m0.mat_mul(m1)
1441
+ m4 = m0 * m1
1442
+
1443
+ [m3, m4].each { |m|
1444
+ assert_equal(m1.width, m.width)
1445
+ assert_equal(m1.height, m.height)
1446
+ assert_in_delta(1.5, m[0, 0][0], 0.001)
1447
+ assert_in_delta(1.8, m[0, 1][0], 0.001)
1448
+ assert_in_delta(2.1, m[0, 2][0], 0.001)
1449
+ assert_in_delta(4.2, m[1, 0][0], 0.001)
1450
+ assert_in_delta(5.4, m[1, 1][0], 0.001)
1451
+ assert_in_delta(6.6, m[1, 2][0], 0.001)
1452
+ assert_in_delta(6.9, m[2, 0][0], 0.001)
1453
+ assert_in_delta(9, m[2, 1][0], 0.001)
1454
+ assert_in_delta(11.1, m[2, 2][0], 0.001)
1455
+ }
1456
+
1457
+ m5 = m0.mat_mul(m1, m2)
1458
+ [m5].each { |m|
1459
+ assert_equal(m1.width, m.width)
1460
+ assert_equal(m1.height, m.height)
1461
+ assert_in_delta(2.5, m[0, 0][0], 0.001)
1462
+ assert_in_delta(3.8, m[0, 1][0], 0.001)
1463
+ assert_in_delta(5.1, m[0, 2][0], 0.001)
1464
+ assert_in_delta(8.2, m[1, 0][0], 0.001)
1465
+ assert_in_delta(10.4, m[1, 1][0], 0.001)
1466
+ assert_in_delta(12.6, m[1, 2][0], 0.001)
1467
+ assert_in_delta(13.9, m[2, 0][0], 0.001)
1468
+ assert_in_delta(17, m[2, 1][0], 0.001)
1469
+ assert_in_delta(20.1, m[2, 2][0], 0.001)
1470
+ }
1471
+
1472
+ assert_raise(TypeError) {
1473
+ m0.mat_mul(DUMMY_OBJ)
1474
+ }
1475
+ assert_raise(TypeError) {
1476
+ m0.mat_mul(m1, DUMMY_OBJ)
1477
+ }
1478
+ end
1479
+
1480
+ def test_div
1481
+ m1 = create_cvmat(3, 3, :cv32f)
1482
+ s1 = CvScalar.new(0.1, 0.2, 0.3, 0.4)
1483
+ m2 = create_cvmat(3, 3, :cv32f) { s1 }
1484
+
1485
+ # CvMat / CvMat
1486
+ m3 = m1.div(m2)
1487
+ assert_equal(m1.height, m3.height)
1488
+ assert_equal(m1.width, m3.width)
1489
+ assert_each_cvscalar(m3, 0.001) { |j, i, c|
1490
+ n = c + 1
1491
+ CvScalar.new(n / 0.1, n / 0.2, n / 0.3, n / 0.4)
1492
+ }
1493
+
1494
+ # scale * CvMat / CvMat
1495
+ scale = 2.5
1496
+ m3 = m1.div(m2, scale)
1497
+ assert_equal(m1.height, m3.height)
1498
+ assert_equal(m1.width, m3.width)
1499
+ assert_each_cvscalar(m3, 0.001) { |j, i, c|
1500
+ n = (c + 1) * scale
1501
+ CvScalar.new(n / 0.1, n / 0.2, n / 0.3, n / 0.4)
1502
+ }
1503
+
1504
+ # CvMat / CvScalar
1505
+ scale = 2.5
1506
+ m3 = m1.div(s1)
1507
+ assert_equal(m1.height, m3.height)
1508
+ assert_equal(m1.width, m3.width)
1509
+ assert_each_cvscalar(m3, 0.001) { |j, i, c|
1510
+ n = c + 1
1511
+ CvScalar.new(n / 0.1, n / 0.2, n / 0.3, n / 0.4)
1512
+ }
1513
+
1514
+ # scale * CvMat / CvScalar
1515
+ m3 = m1.div(s1, scale)
1516
+ assert_equal(m1.height, m3.height)
1517
+ assert_equal(m1.width, m3.width)
1518
+ assert_each_cvscalar(m3, 0.001) { |j, i, c|
1519
+ n = (c + 1) * scale
1520
+ CvScalar.new(n / 0.1, n / 0.2, n / 0.3, n / 0.4)
1521
+ }
1522
+
1523
+ # Alias
1524
+ m3 = m1 / m2
1525
+ assert_equal(m1.height, m3.height)
1526
+ assert_equal(m1.width, m3.width)
1527
+ assert_each_cvscalar(m3, 0.001) { |j, i, c|
1528
+ n = c + 1
1529
+ CvScalar.new(n / 0.1, n / 0.2, n / 0.3, n / 0.4)
1530
+ }
1531
+
1532
+ assert_raise(TypeError) {
1533
+ m1.div(DUMMY_OBJ)
1534
+ }
1535
+ assert_raise(TypeError) {
1536
+ m1.div(m2, DUMMY_OBJ)
1537
+ }
1538
+ end
1539
+
1540
+ def test_add_weighted
1541
+ m1 = create_cvmat(3, 2, :cv8u) { |j, i, c| c + 1 }
1542
+ m2 = create_cvmat(3, 2, :cv8u) { |j, i, c| (c + 1) * 10 }
1543
+ a = 2.0
1544
+ b = 0.1
1545
+ g = 100
1546
+ m3 = CvMat.add_weighted(m1, a, m2, b, g)
1547
+ assert_equal(m1.class, m3.class)
1548
+ assert_equal(m1.rows, m3.rows)
1549
+ assert_equal(m1.cols, m3.cols)
1550
+ assert_equal(m1.depth, m3.depth)
1551
+ assert_equal(m1.channel, m3.channel)
1552
+
1553
+ m1.rows.times { |j|
1554
+ m1.cols.times { |i|
1555
+ expected = m1[j, i][0] * a + m2[j, i][0] * b + g
1556
+ assert_equal(expected, m3[j, i][0])
1557
+ }
1558
+ }
1559
+
1560
+ assert_raise(TypeError) {
1561
+ CvMat.add_weighted(DUMMY_OBJ, a, m2, b, g)
1562
+ }
1563
+ assert_raise(TypeError) {
1564
+ CvMat.add_weighted(m1, DUMMY_OBJ, m2, b, g)
1565
+ }
1566
+ assert_raise(TypeError) {
1567
+ CvMat.add_weighted(m1, a, DUMMY_OBJ, b, g)
1568
+ }
1569
+ assert_raise(TypeError) {
1570
+ CvMat.add_weighted(m1, a, m2, DUMMY_OBJ, g)
1571
+ }
1572
+ assert_raise(TypeError) {
1573
+ CvMat.add_weighted(m1, a, m2, b, DUMMY_OBJ)
1574
+ }
1575
+ end
1576
+
1577
+ def test_and
1578
+ m1 = create_cvmat(6, 4)
1579
+ s1 = CvScalar.new(1, 2, 3, 4)
1580
+ m2 = create_cvmat(6, 4) { s1 }
1581
+ mask = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
1582
+ s = (i < 3 and j < 2) ? 1 : 0
1583
+ CvScalar.new(s)
1584
+ }
1585
+
1586
+ # CvMat & CvMat
1587
+ m3 = m1.and(m2)
1588
+ assert_equal(m1.height, m3.height)
1589
+ assert_equal(m1.width, m3.width)
1590
+ assert_each_cvscalar(m3) { |j, i, c|
1591
+ n = c + 1
1592
+ CvScalar.new(n & 1, n & 2, n & 3, n & 4)
1593
+ }
1594
+
1595
+ # CvMat & CvMat with mask
1596
+ m3 = m1.and(m2, mask)
1597
+ assert_equal(m1.height, m3.height)
1598
+ assert_equal(m1.width, m3.width)
1599
+ assert_each_cvscalar(m3) { |j, i, c|
1600
+ n = c + 1
1601
+ if i < 3 and j < 2
1602
+ CvScalar.new(n & 1, n & 2, n & 3, n & 4)
1603
+ else
1604
+ CvScalar.new(n, n, n, n)
1605
+ end
1606
+ }
1607
+
1608
+ # CvMat & CvScalar
1609
+ m3 = m1.and(s1)
1610
+ assert_equal(m1.height, m3.height)
1611
+ assert_equal(m1.width, m3.width)
1612
+ assert_each_cvscalar(m3) { |j, i, c|
1613
+ n = c + 1
1614
+ CvScalar.new(n & 1, n & 2, n & 3, n & 4)
1615
+ }
1616
+
1617
+ # CvMat & CvScalar with mask
1618
+ m3 = m1.and(s1, mask)
1619
+ assert_equal(m1.height, m3.height)
1620
+ assert_equal(m1.width, m3.width)
1621
+ assert_each_cvscalar(m3) { |j, i, c|
1622
+ n = c + 1
1623
+ if i < 3 and j < 2
1624
+ CvScalar.new(n & 1, n & 2, n & 3, n & 4)
1625
+ else
1626
+ CvScalar.new(n, n, n, n)
1627
+ end
1628
+ }
1629
+
1630
+ # Alias
1631
+ m3 = m1 & m2
1632
+ assert_equal(m1.height, m3.height)
1633
+ assert_equal(m1.width, m3.width)
1634
+ assert_each_cvscalar(m3) { |j, i, c|
1635
+ n = c + 1
1636
+ CvScalar.new(n & 1, n & 2, n & 3, n & 4)
1637
+ }
1638
+
1639
+ m3 = m1 & s1
1640
+ assert_equal(m1.height, m3.height)
1641
+ assert_equal(m1.width, m3.width)
1642
+ assert_each_cvscalar(m3) { |j, i, c|
1643
+ n = c + 1
1644
+ CvScalar.new(n & 1, n & 2, n & 3, n & 4)
1645
+ }
1646
+
1647
+ assert_raise(TypeError) {
1648
+ m1.and(DUMMY_OBJ)
1649
+ }
1650
+ assert_raise(TypeError) {
1651
+ m1.and(m2, DUMMY_OBJ)
1652
+ }
1653
+ end
1654
+
1655
+ def test_or
1656
+ m1 = create_cvmat(6, 4)
1657
+ s1 = CvScalar.new(1, 2, 3, 4)
1658
+ m2 = create_cvmat(6, 4) { s1 }
1659
+ mask = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
1660
+ s = (i < 3 and j < 2) ? 1 : 0
1661
+ CvScalar.new(s)
1662
+ }
1663
+
1664
+ # CvMat | CvMat
1665
+ m3 = m1.or(m2)
1666
+ assert_equal(m1.height, m3.height)
1667
+ assert_equal(m1.width, m3.width)
1668
+ assert_each_cvscalar(m3) { |j, i, c|
1669
+ n = c + 1
1670
+ CvScalar.new(n | 1, n | 2, n | 3, n | 4)
1671
+ }
1672
+
1673
+ # CvMat | CvMat with mask
1674
+ m3 = m1.or(m2, mask)
1675
+ assert_equal(m1.height, m3.height)
1676
+ assert_equal(m1.width, m3.width)
1677
+ assert_each_cvscalar(m3) { |j, i, c|
1678
+ n = c + 1
1679
+ if i < 3 and j < 2
1680
+ CvScalar.new(n | 1, n | 2, n | 3, n | 4)
1681
+ else
1682
+ CvScalar.new(n, n, n, n)
1683
+ end
1684
+ }
1685
+
1686
+ # CvMat | CvScalar
1687
+ m3 = m1.or(s1)
1688
+ assert_equal(m1.height, m3.height)
1689
+ assert_equal(m1.width, m3.width)
1690
+ assert_each_cvscalar(m3) { |j, i, c|
1691
+ n = c + 1
1692
+ CvScalar.new(n | 1, n | 2, n | 3, n | 4)
1693
+ }
1694
+
1695
+ # CvMat | CvScalar with mask
1696
+ m3 = m1.or(s1, mask)
1697
+ assert_equal(m1.height, m3.height)
1698
+ assert_equal(m1.width, m3.width)
1699
+ assert_each_cvscalar(m3) { |j, i, c|
1700
+ n = c + 1
1701
+ if i < 3 and j < 2
1702
+ CvScalar.new(n | 1, n | 2, n | 3, n | 4)
1703
+ else
1704
+ CvScalar.new(n, n, n, n)
1705
+ end
1706
+ }
1707
+
1708
+ # Alias
1709
+ m3 = m1 | m2
1710
+ assert_equal(m1.height, m3.height)
1711
+ assert_equal(m1.width, m3.width)
1712
+ assert_each_cvscalar(m3) { |j, i, c|
1713
+ n = c + 1
1714
+ CvScalar.new(n | 1, n | 2, n | 3, n | 4)
1715
+ }
1716
+
1717
+ m3 = m1 | s1
1718
+ assert_equal(m1.height, m3.height)
1719
+ assert_equal(m1.width, m3.width)
1720
+ assert_each_cvscalar(m3) { |j, i, c|
1721
+ n = c + 1
1722
+ CvScalar.new(n | 1, n | 2, n | 3, n | 4)
1723
+ }
1724
+
1725
+ assert_raise(TypeError) {
1726
+ m1.or(DUMMY_OBJ)
1727
+ }
1728
+ assert_raise(TypeError) {
1729
+ m1.or(m2, DUMMY_OBJ)
1730
+ }
1731
+ end
1732
+
1733
+ def test_xor
1734
+ m1 = create_cvmat(6, 4)
1735
+ s1 = CvScalar.new(1, 2, 3, 4)
1736
+ m2 = create_cvmat(6, 4) { s1 }
1737
+ mask = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
1738
+ s = (i < 3 and j < 2) ? 1 : 0
1739
+ CvScalar.new(s)
1740
+ }
1741
+
1742
+ # CvMat ^ CvMat
1743
+ m3 = m1.xor(m2)
1744
+ assert_equal(m1.height, m3.height)
1745
+ assert_equal(m1.width, m3.width)
1746
+ assert_each_cvscalar(m3) { |j, i, c|
1747
+ n = c + 1
1748
+ CvScalar.new(n ^ 1, n ^ 2, n ^ 3, n ^ 4)
1749
+ }
1750
+
1751
+ # CvMat ^ CvMat with mask
1752
+ m3 = m1.xor(m2, mask)
1753
+ assert_equal(m1.height, m3.height)
1754
+ assert_equal(m1.width, m3.width)
1755
+ assert_each_cvscalar(m3) { |j, i, c|
1756
+ n = c + 1
1757
+ if i < 3 and j < 2
1758
+ CvScalar.new(n ^ 1, n ^ 2, n ^ 3, n ^ 4)
1759
+ else
1760
+ CvScalar.new(n, n, n, n)
1761
+ end
1762
+ }
1763
+
1764
+ # CvMat ^ CvScalar
1765
+ m3 = m1.xor(s1)
1766
+ assert_equal(m1.height, m3.height)
1767
+ assert_equal(m1.width, m3.width)
1768
+ assert_each_cvscalar(m3) { |j, i, c|
1769
+ n = c + 1
1770
+ CvScalar.new(n ^ 1, n ^ 2, n ^ 3, n ^ 4)
1771
+ }
1772
+
1773
+ # CvMat ^ CvScalar with mask
1774
+ m3 = m1.xor(s1, mask)
1775
+ assert_equal(m1.height, m3.height)
1776
+ assert_equal(m1.width, m3.width)
1777
+ assert_each_cvscalar(m3) { |j, i, c|
1778
+ n = c + 1
1779
+ if i < 3 and j < 2
1780
+ CvScalar.new(n ^ 1, n ^ 2, n ^ 3, n ^ 4)
1781
+ else
1782
+ CvScalar.new(n, n, n, n)
1783
+ end
1784
+ }
1785
+
1786
+ # Alias
1787
+ m3 = m1 ^ m2
1788
+ assert_equal(m1.height, m3.height)
1789
+ assert_equal(m1.width, m3.width)
1790
+ assert_each_cvscalar(m3) { |j, i, c|
1791
+ n = c + 1
1792
+ CvScalar.new(n ^ 1, n ^ 2, n ^ 3, n ^ 4)
1793
+ }
1794
+
1795
+ m3 = m1 ^ s1
1796
+ assert_equal(m1.height, m3.height)
1797
+ assert_equal(m1.width, m3.width)
1798
+ assert_each_cvscalar(m3) { |j, i, c|
1799
+ n = c + 1
1800
+ CvScalar.new(n ^ 1, n ^ 2, n ^ 3, n ^ 4)
1801
+ }
1802
+
1803
+ assert_raise(TypeError) {
1804
+ m1.xor(DUMMY_OBJ)
1805
+ }
1806
+ assert_raise(TypeError) {
1807
+ m1.xor(m2, DUMMY_OBJ)
1808
+ }
1809
+ end
1810
+
1811
+ def test_not
1812
+ m1 = create_cvmat(6, 4, :cv8s)
1813
+ m2 = m1.not;
1814
+ m3 = m1.clone
1815
+ m3.not!
1816
+ [m2, m3].each { |m|
1817
+ assert_equal(m1.height, m.height)
1818
+ assert_equal(m1.width, m.width)
1819
+ assert_each_cvscalar(m) { |j, i, c|
1820
+ n = c + 1
1821
+ CvScalar.new(~n, ~n, ~n, ~n)
1822
+ }
1823
+ }
1824
+ end
1825
+
1826
+ def test_eq
1827
+ m1 = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
1828
+ n = (c.even?) ? 10 : c
1829
+ CvScalar.new(n, 0, 0, 0)
1830
+ }
1831
+ m2 = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
1832
+ CvScalar.new(10, 0, 0, 0)
1833
+ }
1834
+ s1 = CvScalar.new(10, 0, 0, 0)
1835
+ m3 = m1.eq(m2)
1836
+ m4 = m1.eq(s1)
1837
+ m5 = m1.eq(10)
1838
+
1839
+ [m3, m4, m5].each { |m|
1840
+ assert_equal(m1.height, m.height)
1841
+ assert_equal(m1.width, m.width)
1842
+ assert_each_cvscalar(m) { |j, i, c|
1843
+ n = (c.even?) ? 0xff : 0
1844
+ CvScalar.new(n, 0, 0, 0)
1845
+ }
1846
+ }
1847
+
1848
+ assert_raise(TypeError) {
1849
+ m1.eq(DUMMY_OBJ)
1850
+ }
1851
+ end
1852
+
1853
+ def test_gt
1854
+ m1 = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
1855
+ CvScalar.new(c, 0, 0, 0)
1856
+ }
1857
+ m2 = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
1858
+ CvScalar.new(10, 0, 0, 0)
1859
+ }
1860
+ s1 = CvScalar.new(10, 0, 0, 0)
1861
+ m3 = m1.gt(m2)
1862
+ m4 = m1.gt(s1)
1863
+ m5 = m1.gt(10)
1864
+
1865
+ [m3, m4, m5].each { |m|
1866
+ assert_equal(m1.height, m.height)
1867
+ assert_equal(m1.width, m.width)
1868
+ assert_each_cvscalar(m) { |j, i, c|
1869
+ n = (c > 10) ? 0xff : 0
1870
+ CvScalar.new(n, 0, 0, 0)
1871
+ }
1872
+ }
1873
+
1874
+ assert_raise(TypeError) {
1875
+ m1.gt(DUMMY_OBJ)
1876
+ }
1877
+ end
1878
+
1879
+ def test_ge
1880
+ m1 = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
1881
+ CvScalar.new(c, 0, 0, 0)
1882
+ }
1883
+ m2 = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
1884
+ CvScalar.new(10, 0, 0, 0)
1885
+ }
1886
+ s1 = CvScalar.new(10, 0, 0, 0)
1887
+ m3 = m1.ge(m2)
1888
+ m4 = m1.ge(s1)
1889
+ m5 = m1.ge(10)
1890
+
1891
+ [m3, m4, m5].each { |m|
1892
+ assert_equal(m1.height, m.height)
1893
+ assert_equal(m1.width, m.width)
1894
+ assert_each_cvscalar(m) { |j, i, c|
1895
+ n = (c >= 10) ? 0xff : 0
1896
+ CvScalar.new(n, 0, 0, 0)
1897
+ }
1898
+ }
1899
+
1900
+ assert_raise(TypeError) {
1901
+ m1.ge(DUMMY_OBJ)
1902
+ }
1903
+ end
1904
+
1905
+ def test_lt
1906
+ m1 = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
1907
+ CvScalar.new(c, 0, 0, 0)
1908
+ }
1909
+ m2 = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
1910
+ CvScalar.new(10, 0, 0, 0)
1911
+ }
1912
+ s1 = CvScalar.new(10, 0, 0, 0)
1913
+ m3 = m1.lt(m2)
1914
+ m4 = m1.lt(s1)
1915
+ m5 = m1.lt(10)
1916
+
1917
+ [m3, m4, m5].each { |m|
1918
+ assert_equal(m1.height, m.height)
1919
+ assert_equal(m1.width, m.width)
1920
+ assert_each_cvscalar(m) { |j, i, c|
1921
+ n = (c < 10) ? 0xff : 0
1922
+ CvScalar.new(n, 0, 0, 0)
1923
+ }
1924
+ }
1925
+
1926
+ assert_raise(TypeError) {
1927
+ m1.lt(DUMMY_OBJ)
1928
+ }
1929
+ end
1930
+
1931
+ def test_le
1932
+ m1 = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
1933
+ CvScalar.new(c, 0, 0, 0)
1934
+ }
1935
+ m2 = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
1936
+ CvScalar.new(10, 0, 0, 0)
1937
+ }
1938
+ s1 = CvScalar.new(10, 0, 0, 0)
1939
+ m3 = m1.le(m2)
1940
+ m4 = m1.le(s1)
1941
+ m5 = m1.le(10)
1942
+
1943
+ [m3, m4, m5].each { |m|
1944
+ assert_equal(m1.height, m.height)
1945
+ assert_equal(m1.width, m.width)
1946
+ assert_each_cvscalar(m) { |j, i, c|
1947
+ n = (c <= 10) ? 0xff : 0
1948
+ CvScalar.new(n, 0, 0, 0)
1949
+ }
1950
+ }
1951
+
1952
+ assert_raise(TypeError) {
1953
+ m1.le(DUMMY_OBJ)
1954
+ }
1955
+ end
1956
+
1957
+ def test_in_range
1958
+ lower, upper = 10, 20
1959
+ m0 = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
1960
+ CvScalar.new(c + 5, 0, 0, 0)
1961
+ }
1962
+ m1 = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
1963
+ CvScalar.new(lower, 0, 0, 0)
1964
+ }
1965
+ m2 = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
1966
+ CvScalar.new(upper, 0, 0, 0)
1967
+ }
1968
+ s1 = CvScalar.new(lower, 0, 0, 0)
1969
+ s2 = CvScalar.new(upper, 0, 0, 0)
1970
+
1971
+ m3 = m0.in_range(m1, m2)
1972
+ m4 = m0.in_range(s1, s2)
1973
+ m5 = m0.in_range(lower, upper)
1974
+
1975
+ [m3, m4, m5].each { |m|
1976
+ assert_equal(m0.height, m.height)
1977
+ assert_equal(m0.width, m.width)
1978
+ assert_each_cvscalar(m) { |j, i, c|
1979
+ val = m0[j, i][0]
1980
+ n = ((lower..upper).include? val) ? 0xff : 0
1981
+ CvScalar.new(n, 0, 0, 0)
1982
+ }
1983
+ }
1984
+
1985
+ assert_raise(TypeError) {
1986
+ m0.in_range(DUMMY_OBJ, m2)
1987
+ }
1988
+ assert_raise(TypeError) {
1989
+ m0.in_range(m1, DUMMY_OBJ)
1990
+ }
1991
+ end
1992
+
1993
+ def test_abs_diff
1994
+ m0 = create_cvmat(6, 4, :cv32f, 4) { |j, i, c|
1995
+ CvScalar.new(-10 + 10.5, 20 + 10.5, -30 + 10.5, 40 - 10.5)
1996
+ }
1997
+ m1 = create_cvmat(6, 4, :cv32f, 4) { |j, i, c|
1998
+ CvScalar.new(c + 10.5, c - 10.5, c + 10.5, c - 10.5)
1999
+ }
2000
+ m2 = create_cvmat(6, 4, :cv32f, 4) { |j, i, c|
2001
+ CvScalar.new(c, c, c, c)
2002
+ }
2003
+
2004
+ s1 = CvScalar.new(-10, 20, -30, 40)
2005
+ m3 = m1.abs_diff(m2)
2006
+ m4 = m0.abs_diff(s1)
2007
+
2008
+ [m3, m4].each { |m|
2009
+ assert_equal(m1.width, m.width)
2010
+ assert_equal(m1.height, m.height)
2011
+ assert_each_cvscalar(m, 0.001) {
2012
+ CvScalar.new(10.5, 10.5, 10.5, 10.5)
2013
+ }
2014
+ }
2015
+
2016
+ assert_raise(TypeError) {
2017
+ m0.abs_diff(DUMMY_OBJ)
2018
+ }
2019
+ end
2020
+
2021
+ def test_normalize
2022
+ mat = create_cvmat(2, 2, :cv32f, 1) { |j, i, c|
2023
+ CvScalar.new(c, 0, 0, 0)
2024
+ }
2025
+
2026
+ m = mat.normalize
2027
+ expected = [0.0, 0.267, 0.534, 0.801]
2028
+ expected.each_with_index { |x, i|
2029
+ assert_in_delta(x, m[i][0], 0.001)
2030
+ }
2031
+
2032
+ minf = mat.normalize(1, 0, CV_NORM_INF)
2033
+ expected = [0.0, 0.333, 0.666, 1.0]
2034
+ expected.each_with_index { |x, i|
2035
+ assert_in_delta(x, minf[i][0], 0.001)
2036
+ }
2037
+
2038
+ ml1 = mat.normalize(1, 0, CV_NORM_L1)
2039
+ expected = [0.0, 0.166, 0.333, 0.5]
2040
+ expected.each_with_index { |x, i|
2041
+ assert_in_delta(x, ml1[i][0], 0.001)
2042
+ }
2043
+
2044
+ ml2 = mat.normalize(1, 0, CV_NORM_L2)
2045
+ expected = [0.0, 0.267, 0.534, 0.801]
2046
+ expected.each_with_index { |x, i|
2047
+ assert_in_delta(x, ml2[i][0], 0.001)
2048
+ }
2049
+
2050
+ mminmax = mat.normalize(10, 5, CV_NORM_MINMAX)
2051
+ expected = [5.0, 6.666, 8.333, 10.0]
2052
+ expected.each_with_index { |x, i|
2053
+ assert_in_delta(x, mminmax[i][0], 0.001)
2054
+ }
2055
+
2056
+ mask = mat.to_8u.zero
2057
+ mask[0, 0] = CvScalar.new(255, 0, 0)
2058
+ mask[1, 0] = CvScalar.new(255, 0, 0)
2059
+ minf = mat.normalize(1, 0, CV_NORM_INF, mask)
2060
+ expected = [0.0, 0.0, 1.0, 0.0]
2061
+ expected.each_with_index { |x, i|
2062
+ assert_in_delta(x, minf[i][0], 0.001)
2063
+ }
2064
+
2065
+ assert_raise(TypeError) {
2066
+ mat.normalize(DUMMY_OBJ, 0, CV_NORM_INF)
2067
+ }
2068
+ assert_raise(TypeError) {
2069
+ mat.normalize(1, DUMMY_OBJ, CV_NORM_INF)
2070
+ }
2071
+ assert_raise(TypeError) {
2072
+ mat.normalize(1, 0, DUMMY_OBJ)
2073
+ }
2074
+ assert_raise(TypeError) {
2075
+ mat.normalize(1, 0, CV_NORM_INF, DUMMY_OBJ)
2076
+ }
2077
+ end
2078
+
2079
+ def test_count_non_zero
2080
+ m0 = create_cvmat(6, 4, :cv32f, 1) { |j, i, c|
2081
+ n = 0
2082
+ n = 1 if i == 0
2083
+ CvScalar.new(n, 0, 0, 0)
2084
+ }
2085
+ assert_equal(6, m0.count_non_zero)
2086
+ end
2087
+
2088
+ def test_sum
2089
+ m0 = create_cvmat(6, 4, :cv32f, 1) { |j, i, c|
2090
+ CvScalar.new(c, c, c, c)
2091
+ }
2092
+ assert_cvscalar_equal(CvScalar.new(276, 0, 0, 0), m0.sum)
2093
+
2094
+ m0 = create_cvmat(6, 4, :cv32f, 1) { |j, i, c|
2095
+ CvScalar.new(-c)
2096
+ }
2097
+ assert_cvscalar_equal(CvScalar.new(-276, 0, 0, 0), m0.sum)
2098
+ end
2099
+
2100
+ def test_avg_sdv
2101
+ m0 = create_cvmat(6, 4, :cv32f, 4) { |j, i, c|
2102
+ CvScalar.new(c * 0.1, -c * 0.1, c, -c)
2103
+ }
2104
+ # CvMat#avg
2105
+ assert_in_delta(CvScalar.new(1.15, -1.15, 11.5, -11.5), m0.avg, 0.001)
2106
+ # CvMat#sdv
2107
+ assert_in_delta(CvScalar.new(0.69221, 0.69221, 6.9221, 6.9221), m0.sdv, 0.001)
2108
+ # CvMat#avg_sdv
2109
+ avg, sdv = m0.avg_sdv
2110
+ assert_in_delta(CvScalar.new(1.15, -1.15, 11.5, -11.5), avg, 0.001)
2111
+ assert_in_delta(CvScalar.new(0.69221, 0.69221, 6.9221, 6.9221), sdv, 0.001)
2112
+
2113
+ mask = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
2114
+ n = (i == j) ? 1 : 0
2115
+ CvScalar.new(n)
2116
+ }
2117
+ # CvMat#avg
2118
+ assert_in_delta(CvScalar.new(0.75, -0.75, 7.5, -7.5), m0.avg(mask), 0.001)
2119
+ # CvMat#sdv
2120
+ assert_in_delta(CvScalar.new(0.55901, 0.55901, 5.5901, 5.5901), m0.sdv(mask), 0.001)
2121
+ # CvMat#avg_sdv
2122
+ avg, sdv = m0.avg_sdv(mask)
2123
+ assert_in_delta(CvScalar.new(0.75, -0.75, 7.5, -7.5), avg, 0.001)
2124
+ assert_in_delta(CvScalar.new(0.55901, 0.55901, 5.5901, 5.5901), sdv, 0.001)
2125
+
2126
+ assert_raise(TypeError) {
2127
+ m0.avg(DUMMY_OBJ)
2128
+ }
2129
+ assert_raise(TypeError) {
2130
+ m0.sdv(DUMMY_OBJ)
2131
+ }
2132
+ assert_raise(TypeError) {
2133
+ m0.avg_sdv(DUMMY_OBJ)
2134
+ }
2135
+ end
2136
+
2137
+ def test_min_max_loc
2138
+ m0 = create_cvmat(6, 4, :cv32f, 1) { |j, i, c|
2139
+ CvScalar.new(c * 0.5)
2140
+ }
2141
+ m0[2, 3] = CvScalar.new(100.5) # Max
2142
+ m0[5, 1] = CvScalar.new(-100.5) # Min
2143
+
2144
+ min_val, max_val, min_loc, max_loc = m0.min_max_loc
2145
+ assert_equal(-100.5, min_val)
2146
+ assert_equal(5, min_loc.y)
2147
+ assert_equal(1, min_loc.x)
2148
+ assert_equal(100.5, max_val)
2149
+ assert_equal(2, max_loc.y)
2150
+ assert_equal(3, max_loc.x)
2151
+
2152
+ assert_raise(TypeError) {
2153
+ m0.min_max_loc(DUMMY_OBJ)
2154
+ }
2155
+ end
2156
+
2157
+ def test_dot_product
2158
+ m1 = create_cvmat(2, 2, :cv32f, 1) { |j, i, c|
2159
+ CvScalar.new(c * 0.5)
2160
+ }
2161
+ m2 = create_cvmat(2, 2, :cv32f, 1) { |j, i, c|
2162
+ CvScalar.new(c * 1.5)
2163
+ }
2164
+ assert_in_delta(10.5, m1.dot_product(m2), 0.001)
2165
+
2166
+ m1 = create_cvmat(2, 2, :cv32f) { |j, i, c|
2167
+ CvScalar.new(c * 0.5, c * 0.6, c * 0.7, c * 0.8)
2168
+ }
2169
+ m2 = create_cvmat(2, 2, :cv32f) { |j, i, c|
2170
+ CvScalar.new(c * 1.5, c * 2.0, c * 2.5, c * 3.0)
2171
+ }
2172
+ assert_in_delta(85.39999, m1.dot_product(m2), 0.001)
2173
+
2174
+ assert_raise(TypeError) {
2175
+ m1.dot_product(DUMMY_OBJ)
2176
+ }
2177
+ end
2178
+
2179
+ def test_cross_product
2180
+ m1 = create_cvmat(1, 3, :cv32f, 1) { |j, i, c|
2181
+ CvScalar.new(c * 0.5)
2182
+ }
2183
+ m2 = create_cvmat(1, 3, :cv32f, 1) { |j, i, c|
2184
+ CvScalar.new(c + 1)
2185
+ }
2186
+ m3 = m1.cross_product(m2)
2187
+
2188
+ assert_in_delta(CvScalar.new(-0.5), m3[0, 0], 0.001)
2189
+ assert_in_delta(CvScalar.new(1), m3[0, 1], 0.001)
2190
+ assert_in_delta(CvScalar.new(-0.5), m3[0, 2], 0.001)
2191
+
2192
+ assert_raise(TypeError) {
2193
+ m1.cross_product(DUMMY_OBJ)
2194
+ }
2195
+ end
2196
+
2197
+ def test_transform
2198
+ m0 = create_cvmat(5, 5, :cv32f, 3) { |j, i, c|
2199
+ CvScalar.new(c * 0.5, c * 1.0, c * 1.5)
2200
+ }
2201
+ transmat = CvMat.new(3, 3, :cv32f, 1);
2202
+ transmat[0, 0] = CvScalar.new(0.0)
2203
+ transmat[1, 0] = CvScalar.new(0.0)
2204
+ transmat[2, 0] = CvScalar.new(0.0)
2205
+
2206
+ transmat[0, 1] = CvScalar.new(0.0)
2207
+ transmat[1, 1] = CvScalar.new(0.0)
2208
+ transmat[2, 1] = CvScalar.new(1.0)
2209
+
2210
+ transmat[0, 2] = CvScalar.new(1.0)
2211
+ transmat[1, 2] = CvScalar.new(0.0)
2212
+ transmat[2, 2] = CvScalar.new(0.0)
2213
+
2214
+ m1 = m0.transform(transmat)
2215
+ assert_each_cvscalar(m1, 0.01) { |j, i, c|
2216
+ CvScalar.new(c * 1.5, 0, c, 0)
2217
+ }
2218
+
2219
+ stf = CvMat.new(3, 1, :cv32f, 1)
2220
+ stf[0, 0] = CvScalar.new(-10)
2221
+ stf[1, 0] = CvScalar.new(0.0)
2222
+ stf[2, 0] = CvScalar.new(5)
2223
+
2224
+ m1 = m0.transform(transmat, stf)
2225
+ assert_each_cvscalar(m1, 0.01) { |j, i, c|
2226
+ CvScalar.new(c * 1.5 - 10, 0, c + 5, 0)
2227
+ }
2228
+
2229
+ assert_raise(TypeError) {
2230
+ m0.transform(DUMMY_OBJ)
2231
+ }
2232
+ assert_raise(TypeError) {
2233
+ m0.transform(transmat, DUMMY_OBJ)
2234
+ }
2235
+ end
2236
+
2237
+ def test_perspective_transform
2238
+ mat = CvMat.new(1, 1, :cv32f, 2)
2239
+ mat[0] = CvScalar.new(2, 3)
2240
+ transmat = CvMat.new(3, 3, :cv32f, 1).clear
2241
+ mat.channel.times { |c|
2242
+ transmat[c, c] = CvScalar.new(1.0)
2243
+ }
2244
+ transmat[2, 2] = CvScalar.new(0.5)
2245
+
2246
+ m = mat.perspective_transform(transmat)
2247
+ assert_equal(1, m.height)
2248
+ assert_equal(1, m.width)
2249
+ assert_equal(:cv32f, m.depth)
2250
+ assert_equal(2, m.channel)
2251
+ assert_in_delta(CvScalar.new(4, 6), m[0], 0.001);
2252
+
2253
+ mat = CvMat.new(1, 1, :cv32f, 3)
2254
+ mat[0] = CvScalar.new(2, 3, 4)
2255
+ transmat = CvMat.new(4, 4, :cv32f, 1).clear
2256
+ mat.channel.times { |c|
2257
+ transmat[c, c] = CvScalar.new(1.0)
2258
+ }
2259
+ transmat[3, 3] = CvScalar.new(0.5)
2260
+
2261
+ m = mat.perspective_transform(transmat)
2262
+ assert_equal(1, m.height)
2263
+ assert_equal(1, m.width)
2264
+ assert_equal(:cv32f, m.depth)
2265
+ assert_equal(3, m.channel)
2266
+ assert_in_delta(CvScalar.new(4, 6, 8), m[0], 0.001);
2267
+
2268
+ assert_raise(TypeError) {
2269
+ mat.perspective_transform(DUMMY_OBJ)
2270
+ }
2271
+ assert_raise(CvStsAssert) {
2272
+ mat.perspective_transform(CvMat.new(3, 3, :cv32f, 3))
2273
+ }
2274
+ end
2275
+
2276
+ def test_mul_transposed
2277
+ mat0 = create_cvmat(2, 2, :cv32f, 1) { |j, i, c|
2278
+ CvScalar.new((c + 1) * 2)
2279
+ }
2280
+ delta = create_cvmat(2, 2, :cv32f, 1) { |j, i, c|
2281
+ CvScalar.new(c + 1)
2282
+ }
2283
+
2284
+ [mat0.mul_transposed,
2285
+ mat0.mul_transposed(:delta => nil),
2286
+ mat0.mul_transposed(:order => 0),
2287
+ mat0.mul_transposed(:scale => 1.0)].each { |m|
2288
+ expected = [20, 44,
2289
+ 44, 100]
2290
+ assert_equal(2, m.rows)
2291
+ assert_equal(2, m.cols)
2292
+ assert_equal(:cv32f, m.depth)
2293
+ expected.each_with_index { |x, i|
2294
+ assert_in_delta(x, m[i][0], 0.1)
2295
+ }
2296
+ }
2297
+
2298
+ m = mat0.mul_transposed(:delta => delta)
2299
+ expected = [5, 11,
2300
+ 11, 25]
2301
+ assert_equal(2, m.rows)
2302
+ assert_equal(2, m.cols)
2303
+ assert_equal(:cv32f, m.depth)
2304
+ expected.each_with_index { |x, i|
2305
+ assert_in_delta(x, m[i][0], 0.1)
2306
+ }
2307
+
2308
+ m = mat0.mul_transposed(:delta => delta, :order => 1, :scale => 2.0)
2309
+ expected = [20, 28,
2310
+ 28, 40]
2311
+ assert_equal(2, m.rows)
2312
+ assert_equal(2, m.cols)
2313
+ assert_equal(:cv32f, m.depth)
2314
+ expected.each_with_index { |x, i|
2315
+ assert_in_delta(x, m[i][0], 0.1)
2316
+ }
2317
+ end
2318
+
2319
+ def test_trace
2320
+ m0 = create_cvmat(5, 5, :cv32f, 4) { |j, i, c|
2321
+ CvScalar.new(c * 0.5, c * 1.0, c * 1.5, c * 2.0)
2322
+ }
2323
+ assert_in_delta(CvScalar.new(30, 60, 90, 120), m0.trace, 0.001)
2324
+ end
2325
+
2326
+ def test_transpose
2327
+ m0 = create_cvmat(2, 3, :cv32f, 4) { |j, i, c|
2328
+ CvScalar.new(c * 0.5, c * 1.0, c * 1.5, c * 2.0)
2329
+ }
2330
+ m1 = m0.transpose
2331
+ m2 = m0.t
2332
+
2333
+ [m1, m2].each { |m|
2334
+ assert_equal(m0.rows, m.cols)
2335
+ assert_equal(m0.cols, m.rows)
2336
+ assert_each_cvscalar(m, 0.001) { |j, i, c|
2337
+ m0[i, j]
2338
+ }
2339
+ }
2340
+ end
2341
+
2342
+ def test_det
2343
+ elems = [2.5, 4.5, 2.0,
2344
+ 3.0, 2.5, -0.5,
2345
+ 1.0, 0.5, 1.5]
2346
+ m0 = create_cvmat(3, 3, :cv32f, 1) { |j, i, c|
2347
+ CvScalar.new(elems[c])
2348
+ }
2349
+ assert_in_delta(-14.5, m0.det, 0.001)
2350
+ end
2351
+
2352
+ def test_invert
2353
+ elems = [1, 2, 3,
2354
+ 2, 6, 9,
2355
+ 1, 4, 7]
2356
+ m0 = create_cvmat(3, 3, :cv32f, 1) { |j, i, c|
2357
+ CvScalar.new(elems[c])
2358
+ }
2359
+ m1 = m0.invert
2360
+ m2 = m0.invert(:lu)
2361
+ m3 = m0.invert(:svd)
2362
+ m4 = m0.invert(:svd_sym)
2363
+ m5 = m0.invert(:svd_symmetric)
2364
+
2365
+ expected = [3, -1, 0, -2.5, 2, -1.5, 1, -1, 1]
2366
+ [m1, m2, m3].each { |m|
2367
+ assert_equal(m0.width, m.width)
2368
+ assert_equal(m0.height, m.height)
2369
+ assert_each_cvscalar(m, 0.001) { |j, i, c|
2370
+ CvScalar.new(expected[c])
2371
+ }
2372
+ }
2373
+
2374
+ expected = [3, -1, 0, -1.0, 0.15, 0.23, 0, 0.23, -0.15]
2375
+ [m4, m5].each { |m|
2376
+ assert_equal(m0.width, m.width)
2377
+ assert_equal(m0.height, m.height)
2378
+ assert_each_cvscalar(m, 0.1) { |j, i, c|
2379
+ CvScalar.new(expected[c])
2380
+ }
2381
+ }
2382
+
2383
+ assert_raise(TypeError) {
2384
+ m0.invert(DUMMY_OBJ)
2385
+ }
2386
+ end
2387
+
2388
+ def test_solve
2389
+ elems1 = [3, 4, 5,
2390
+ 8, 9, 6,
2391
+ 3, 5, 9]
2392
+ elems2 = [3,
2393
+ 4,
2394
+ 5]
2395
+ a = create_cvmat(3, 3, :cv32f, 1) { |j, i, c|
2396
+ CvScalar.new(elems1[c])
2397
+ }
2398
+ b = create_cvmat(3, 1, :cv32f, 1) { |j, i, c|
2399
+ CvScalar.new(elems2[c])
2400
+ }
2401
+
2402
+ m1 = CvMat.solve(a, b)
2403
+ m2 = CvMat.solve(a, b, :lu)
2404
+ m3 = CvMat.solve(a, b, :svd)
2405
+ m4 = CvMat.solve(a, b, :svd_sym)
2406
+ m5 = CvMat.solve(a, b, :svd_symmetric)
2407
+ expected = [2, -2, 1]
2408
+ [m1, m2, m3].each { |m|
2409
+ assert_equal(b.width, m.width)
2410
+ assert_equal(a.height, m.height)
2411
+ assert_each_cvscalar(m, 0.001) { |j, i, c|
2412
+ CvScalar.new(expected[c])
2413
+ }
2414
+ }
2415
+
2416
+ assert_raise(TypeError) {
2417
+ CvMat.solve(DUMMY_OBJ, b)
2418
+ }
2419
+ assert_raise(TypeError) {
2420
+ CvMat.solve(a, DUMMY_OBJ)
2421
+ }
2422
+ assert_raise(TypeError) {
2423
+ CvMat.solve(a, b, DUMMY_OBJ)
2424
+ }
2425
+ end
2426
+
2427
+ def test_svd
2428
+ rows = 2
2429
+ cols = 3
2430
+ m0 = create_cvmat(rows, cols, :cv32f, 1) { |j, i, c|
2431
+ CvScalar.new(c + 1)
2432
+ }
2433
+
2434
+ [m0.svd, m0.clone.svd(CV_SVD_MODIFY_A)].each { |w, u, v|
2435
+ expected = [0.38632, -0.92237,
2436
+ 0.92237, 0.38632]
2437
+ assert_equal(rows, u.rows)
2438
+ assert_equal(rows, u.cols)
2439
+ expected.each_with_index { |x, i|
2440
+ assert_in_delta(x, u[i][0], 0.0001)
2441
+ }
2442
+
2443
+ assert_equal(rows, w.rows)
2444
+ assert_equal(cols, w.cols)
2445
+ expected = [9.50803, 0, 0,
2446
+ 0, 0.77287, 0]
2447
+ expected.each_with_index { |x, i|
2448
+ assert_in_delta(x, w[i][0], 0.0001)
2449
+ }
2450
+
2451
+ assert_equal(cols, v.rows)
2452
+ assert_equal(rows, v.cols)
2453
+ expected = [0.42867, 0.80596,
2454
+ 0.56631, 0.11238,
2455
+ 0.70395, -0.58120]
2456
+
2457
+ expected.each_with_index { |x, i|
2458
+ assert_in_delta(x, v[i][0], 0.0001)
2459
+ }
2460
+ }
2461
+
2462
+ w, ut, v = m0.svd(CV_SVD_U_T)
2463
+ expected = [0.38632, 0.92237,
2464
+ -0.92237, 0.38632]
2465
+ assert_equal(rows, ut.rows)
2466
+ assert_equal(rows, ut.cols)
2467
+ expected.each_with_index { |x, i|
2468
+ assert_in_delta(x, ut[i][0], 0.0001)
2469
+ }
2470
+
2471
+ assert_equal(rows, w.rows)
2472
+ assert_equal(cols, w.cols)
2473
+ expected = [9.50803, 0, 0,
2474
+ 0, 0.77287, 0]
2475
+ expected.each_with_index { |x, i|
2476
+ assert_in_delta(x, w[i][0], 0.0001)
2477
+ }
2478
+
2479
+ assert_equal(cols, v.rows)
2480
+ assert_equal(rows, v.cols)
2481
+ expected = [0.42867, 0.80596,
2482
+ 0.56631, 0.11238,
2483
+ 0.70395, -0.58120]
2484
+
2485
+ expected.each_with_index { |x, i|
2486
+ assert_in_delta(x, v[i][0], 0.0001)
2487
+ }
2488
+
2489
+ w, u, vt = m0.svd(CV_SVD_V_T)
2490
+ expected = [0.38632, -0.92237,
2491
+ 0.92237, 0.38632]
2492
+ assert_equal(rows, u.rows)
2493
+ assert_equal(rows, u.cols)
2494
+ expected.each_with_index { |x, i|
2495
+ assert_in_delta(x, u[i][0], 0.0001)
2496
+ }
2497
+
2498
+ assert_equal(rows, w.rows)
2499
+ assert_equal(cols, w.cols)
2500
+ expected = [9.50803, 0, 0,
2501
+ 0, 0.77287, 0]
2502
+ expected.each_with_index { |x, i|
2503
+ assert_in_delta(x, w[i][0], 0.0001)
2504
+ }
2505
+
2506
+ assert_equal(rows, vt.rows)
2507
+ assert_equal(cols, vt.cols)
2508
+ expected = [0.42867, 0.56631, 0.70395,
2509
+ 0.80596, 0.11238, -0.58120]
2510
+ expected.each_with_index { |x, i|
2511
+ assert_in_delta(x, vt[i][0], 0.0001)
2512
+ }
2513
+ end
2514
+
2515
+ def test_eigenvv
2516
+ elems = [6, -2, -3, 7]
2517
+ m0 = create_cvmat(2, 2, :cv32f, 1) { |j, i, c|
2518
+ CvScalar.new(elems[c])
2519
+ }
2520
+
2521
+ v1 = m0.eigenvv
2522
+ v2 = m0.eigenvv(10 ** -15)
2523
+ v3 = m0.eigenvv(10 ** -15, 1, 1)
2524
+
2525
+ [v1, v2].each { |vec, val|
2526
+ assert_in_delta(-0.615, vec[0, 0][0], 0.01)
2527
+ assert_in_delta(0.788, vec[0, 1][0], 0.01)
2528
+ assert_in_delta(0.788, vec[1, 0][0], 0.01)
2529
+ assert_in_delta(0.615, vec[1, 1][0], 0.01)
2530
+ assert_in_delta(8.562, val[0][0], 0.01)
2531
+ assert_in_delta(4.438, val[1][0], 0.01)
2532
+ }
2533
+
2534
+ vec3, val3 = v3
2535
+ assert_in_delta(-0.615, vec3[0, 0][0], 0.01)
2536
+ assert_in_delta(0.788, vec3[0, 1][0], 0.01)
2537
+ assert_in_delta(8.562, val3[0][0], 0.01)
2538
+
2539
+ assert_raise(TypeError) {
2540
+ m0.eigenvv(DUMMY_OBJ)
2541
+ }
2542
+ assert_raise(TypeError) {
2543
+ m0.eigenvv(nil, DUMMY_OBJ)
2544
+ }
2545
+ assert_raise(TypeError) {
2546
+ m0.eigenvv(nil, nil, DUMMY_OBJ)
2547
+ }
2548
+ end
2549
+
2550
+ def test_find_homography
2551
+ # Nx2
2552
+ src = CvMat.new(4, 2, :cv32f, 1)
2553
+ dst = CvMat.new(4, 2, :cv32f, 1)
2554
+
2555
+ # Nx3 (Homogeneous coordinates)
2556
+ src2 = CvMat.new(4, 3, :cv32f, 1)
2557
+ dst2 = CvMat.new(4, 3, :cv32f, 1)
2558
+
2559
+ # Homography
2560
+ # <src> => <dst>
2561
+ # (0, 0) => (50, 0)
2562
+ # (255, 0) => (205, 0)
2563
+ # (255, 255) => (255, 220)
2564
+ # (0, 255) => (0, 275)
2565
+ [[0, 0], [255, 0], [255, 255], [0, 255]].each_with_index { |coord, i|
2566
+ src[i, 0] = coord[0]
2567
+ src[i, 1] = coord[1]
2568
+
2569
+ src2[i, 0] = coord[0] * 2
2570
+ src2[i, 1] = coord[1] * 2
2571
+ src2[i, 2] = 2
2572
+ }
2573
+ [[50, 0], [205, 0], [255, 220], [0, 275]].each_with_index { |coord, i|
2574
+ dst[i, 0] = coord[0]
2575
+ dst[i, 1] = coord[1]
2576
+
2577
+ dst2[i, 0] = coord[0] * 2
2578
+ dst2[i, 1] = coord[1] * 2
2579
+ dst2[i, 2] = 2
2580
+ }
2581
+
2582
+ mat1 = CvMat.find_homography(src, dst)
2583
+ mat2 = CvMat.find_homography(src, dst, :all)
2584
+ mat3 = CvMat.find_homography(src, dst, :ransac)
2585
+ mat4 = CvMat.find_homography(src, dst, :lmeds)
2586
+ mat5, status5 = CvMat.find_homography(src, dst, :ransac, 5, true)
2587
+ mat6, status6 = CvMat.find_homography(src, dst, :ransac, 5, true)
2588
+ mat7 = CvMat.find_homography(src, dst, :ransac, 5, false)
2589
+ mat8 = CvMat.find_homography(src, dst, :ransac, 5, nil)
2590
+ mat9 = CvMat.find_homography(src, dst, :all, 5, true)
2591
+ mat10, status10 = CvMat.find_homography(src2, dst2, :ransac, 5, true)
2592
+
2593
+ [mat1, mat2, mat3, mat4, mat5, mat6, mat7, mat8, mat9, mat10].each { |mat|
2594
+ assert_equal(3, mat.rows)
2595
+ assert_equal(3, mat.cols)
2596
+ assert_equal(:cv32f, mat.depth)
2597
+ assert_equal(1, mat.channel)
2598
+ [0.72430, -0.19608, 50.0,
2599
+ 0.0, 0.62489, 0.0,
2600
+ 0.00057, -0.00165, 1.0].each_with_index { |x, i|
2601
+ assert_in_delta(x, mat[i][0], 0.0001)
2602
+ }
2603
+ }
2604
+
2605
+ [status5, status6, status10].each { |status|
2606
+ assert_equal(1, status.rows)
2607
+ assert_equal(4, status.cols)
2608
+ assert_equal(:cv8u, status.depth)
2609
+ assert_equal(1, status.channel)
2610
+ 4.times { |i|
2611
+ assert_in_delta(1.0, status[i][0], 0.0001)
2612
+ }
2613
+ }
2614
+
2615
+ assert_raise(TypeError) {
2616
+ CvMat.find_homography(DUMMY_OBJ, dst, :ransac, 5, true)
2617
+ }
2618
+ assert_raise(TypeError) {
2619
+ CvMat.find_homography(src, DUMMY_OBJ, :ransac, 5, true)
2620
+ }
2621
+ assert_raise(TypeError) {
2622
+ CvMat.find_homography(src, dst, DUMMY_OBJ, 5, true)
2623
+ }
2624
+ assert_raise(TypeError) {
2625
+ CvMat.find_homography(src, dst, :ransac, DUMMY_OBJ, true)
2626
+ }
2627
+ CvMat.find_homography(src, dst, :ransac, 5, DUMMY_OBJ)
2628
+ end
2629
+
2630
+ def test_find_fundamental_mat
2631
+ points1 = [[488.362, 169.911],
2632
+ [449.488, 174.44],
2633
+ [408.565, 179.669],
2634
+ [364.512, 184.56],
2635
+ [491.483, 122.366],
2636
+ [451.512, 126.56],
2637
+ [409.502, 130.342],
2638
+ [365.5, 134.0],
2639
+ [494.335, 74.544],
2640
+ [453.5, 76.5],
2641
+ [411.646, 79.5901],
2642
+ [366.498, 81.6577],
2643
+ [453.5, 76.5],
2644
+ [411.646, 79.5901],
2645
+ [366.498, 81.6577]]
2646
+
2647
+ points2 = [[526.605, 213.332],
2648
+ [470.485, 207.632],
2649
+ [417.5, 201.0],
2650
+ [367.485, 195.632],
2651
+ [530.673, 156.417],
2652
+ [473.749, 151.39],
2653
+ [419.503, 146.656],
2654
+ [368.669, 142.565],
2655
+ [534.632, 97.5152],
2656
+ [475.84, 94.6777],
2657
+ [421.16, 90.3223],
2658
+ [368.5, 87.5],
2659
+ [475.84, 94.6777],
2660
+ [421.16, 90.3223],
2661
+ [368.5, 87.5]]
2662
+
2663
+ # 7 point
2664
+ num_points = 7
2665
+ mat1 = CvMat.new(num_points, 2, :cv64f, 1)
2666
+ mat2 = CvMat.new(num_points, 2, :cv64f, 1)
2667
+
2668
+ points1[0...num_points].each_with_index { |pt, i|
2669
+ mat1[i, 0] = CvScalar.new(pt[0])
2670
+ mat1[i, 1] = CvScalar.new(pt[1])
2671
+ }
2672
+ points2[0...num_points].each_with_index { |pt, i|
2673
+ mat2[i, 0] = CvScalar.new(pt[0])
2674
+ mat2[i, 1] = CvScalar.new(pt[1])
2675
+ }
2676
+ f_mat1 = CvMat.find_fundamental_mat(mat1, mat2, CV_FM_7POINT)
2677
+ f_mat2, status = CvMat.find_fundamental_mat(mat1, mat2, CV_FM_7POINT, :with_status => true)
2678
+
2679
+ expected = [0.000009, 0.000029, -0.010343,
2680
+ -0.000033, 0.000000, 0.014590,
2681
+ 0.004415, -0.013420, 1.000000,
2682
+ 0.000000, 0.000001, -0.000223,
2683
+ -0.000001, 0.000036, -0.005309,
2684
+ -0.000097, -0.006463, 1.000000,
2685
+ 0.000002, 0.000005, -0.001621,
2686
+ -0.000005, 0.000031, -0.002559,
2687
+ 0.000527, -0.007424, 1.000000]
2688
+ [f_mat1, f_mat2].each { |f_mat|
2689
+ assert_equal(9, f_mat.rows)
2690
+ assert_equal(3, f_mat.cols)
2691
+ expected.each_with_index { |val, i|
2692
+ assert_in_delta(val, f_mat[i][0], 1.0e-5)
2693
+ }
2694
+ }
2695
+ assert_equal(num_points, status.cols)
2696
+ num_points.times { |i|
2697
+ assert_in_delta(1, status[i][0], 1.0e-5)
2698
+ }
2699
+
2700
+ # 8 point
2701
+ num_points = 8
2702
+ mat1 = CvMat.new(num_points, 2, :cv64f, 1)
2703
+ mat2 = CvMat.new(num_points, 2, :cv64f, 1)
2704
+
2705
+ points1[0...num_points].each_with_index { |pt, i|
2706
+ mat1[i, 0] = CvScalar.new(pt[0])
2707
+ mat1[i, 1] = CvScalar.new(pt[1])
2708
+ }
2709
+ points2[0...num_points].each_with_index { |pt, i|
2710
+ mat2[i, 0] = CvScalar.new(pt[0])
2711
+ mat2[i, 1] = CvScalar.new(pt[1])
2712
+ }
2713
+
2714
+ f_mat1 = CvMat.find_fundamental_mat(mat1, mat2, CV_FM_8POINT)
2715
+ f_mat2, status = CvMat.find_fundamental_mat(mat1, mat2, CV_FM_8POINT, :with_status => true)
2716
+
2717
+ expected = [0.000001, 0.000004, -0.001127,
2718
+ -0.000005, 0.000038, -0.003778,
2719
+ 0.000819, -0.008325, 1.000000]
2720
+ [f_mat1, f_mat2].each { |f_mat|
2721
+ assert_equal(3, f_mat.rows)
2722
+ assert_equal(3, f_mat.cols)
2723
+ expected.each_with_index { |val, i|
2724
+ assert_in_delta(val, f_mat[i][0], 1.0e-5)
2725
+ }
2726
+ }
2727
+ assert_equal(num_points, status.cols)
2728
+ num_points.times { |i|
2729
+ assert_in_delta(1, status[i][0], 1.0e-5)
2730
+ }
2731
+
2732
+ # RANSAC default
2733
+ num_points = points1.size
2734
+ mat1 = CvMat.new(num_points, 2, :cv64f, 1)
2735
+ mat2 = CvMat.new(num_points, 2, :cv64f, 1)
2736
+
2737
+ points1[0...num_points].each_with_index { |pt, i|
2738
+ mat1[i, 0] = CvScalar.new(pt[0])
2739
+ mat1[i, 1] = CvScalar.new(pt[1])
2740
+ }
2741
+ points2[0...num_points].each_with_index { |pt, i|
2742
+ mat2[i, 0] = CvScalar.new(pt[0])
2743
+ mat2[i, 1] = CvScalar.new(pt[1])
2744
+ }
2745
+
2746
+ [CvMat.find_fundamental_mat(mat1, mat2, CV_FM_RANSAC, :with_status => false,
2747
+ :maximum_distance => 1.0, :desirable_level => 0.99),
2748
+ CvMat.find_fundamental_mat(mat1, mat2, CV_FM_RANSAC)].each { |f_mat|
2749
+ assert_equal(3, f_mat.rows)
2750
+ assert_equal(3, f_mat.cols)
2751
+ expected = [0.000010, 0.000039, -0.011141,
2752
+ -0.000045, -0.000001, 0.019631,
2753
+ 0.004873, -0.017604, 1.000000]
2754
+ expected.each_with_index { |val, i|
2755
+ assert_in_delta(val, f_mat[i][0], 1.0e-5)
2756
+ }
2757
+ }
2758
+
2759
+ # RANSAC with options
2760
+ f_mat, status = CvMat.find_fundamental_mat(mat1, mat2, CV_FM_RANSAC, :with_status => true,
2761
+ :maximum_distance => 2.0, :desirable_level => 0.8)
2762
+ assert_equal(3, f_mat.rows)
2763
+ assert_equal(3, f_mat.cols)
2764
+ assert_equal(1, status.rows)
2765
+ assert_equal(num_points, status.cols)
2766
+
2767
+ expected_f_mat = [0.000009, 0.000030, -0.010692,
2768
+ -0.000039, 0.000000, 0.020567,
2769
+ 0.004779, -0.018064, 1.000000]
2770
+ expected_f_mat.each_with_index { |val, i|
2771
+ assert_in_delta(val, f_mat[i][0], 1.0e-5)
2772
+ }
2773
+ expected_status = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
2774
+ expected_status.each_with_index { |val, i|
2775
+ assert_in_delta(val, status[i][0], 1.0e-5)
2776
+ }
2777
+
2778
+ # LMedS default
2779
+ num_points = 12
2780
+ mat1 = CvMat.new(num_points, 2, :cv64f, 1)
2781
+ mat2 = CvMat.new(num_points, 2, :cv64f, 1)
2782
+
2783
+ points1[0...num_points].each_with_index { |pt, i|
2784
+ mat1[i, 0] = CvScalar.new(pt[0])
2785
+ mat1[i, 1] = CvScalar.new(pt[1])
2786
+ }
2787
+ points2[0...num_points].each_with_index { |pt, i|
2788
+ mat2[i, 0] = CvScalar.new(pt[0])
2789
+ mat2[i, 1] = CvScalar.new(pt[1])
2790
+ }
2791
+
2792
+ [CvMat.find_fundamental_mat(mat1, mat2, CV_FM_LMEDS, :with_status => false,
2793
+ :maximum_distance => 1.0, :desirable_level => 0.99),
2794
+ CvMat.find_fundamental_mat(mat1, mat2, CV_FM_LMEDS)].each { |f_mat|
2795
+ assert_equal(3, f_mat.rows)
2796
+ assert_equal(3, f_mat.cols)
2797
+ expected = [-2.79e-05, -0.0009362, 0.0396139,
2798
+ 0.0010285, -2.48e-05, -0.3946452,
2799
+ -0.0322220, 0.3695115, 1.0]
2800
+ expected.each_with_index { |val, i|
2801
+ assert_in_delta(val, f_mat[i][0], 1.0e-5)
2802
+ }
2803
+ }
2804
+
2805
+ # LMedS with options
2806
+ f_mat, status = CvMat.find_fundamental_mat(mat1, mat2, CV_FM_LMEDS, :with_status => true,
2807
+ :desirable_level => 0.8)
2808
+ assert_equal(3, f_mat.rows)
2809
+ assert_equal(3, f_mat.cols)
2810
+ assert_equal(1, status.rows)
2811
+ assert_equal(num_points, status.cols)
2812
+
2813
+ expected_f_mat = [6.48e-05, 0.001502, -0.086036,
2814
+ -0.001652, 3.86e-05, 0.638690,
2815
+ 0.059998, -0.597778, 1.0]
2816
+ expected_f_mat.each_with_index { |val, i|
2817
+ assert_in_delta(val, f_mat[i][0], 1.0e-5)
2818
+ }
2819
+ expected_status = [1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0]
2820
+ expected_status.each_with_index { |val, i|
2821
+ assert_equal(val, status[i][0].to_i)
2822
+ }
2823
+
2824
+ [CV_FM_7POINT, CV_FM_8POINT, CV_FM_RANSAC, CV_FM_LMEDS].each { |method|
2825
+ assert_raise(TypeError) {
2826
+ CvMat.find_fundamental_mat(DUMMY_OBJ, mat2, method, :with_status => true)
2827
+ }
2828
+ assert_raise(TypeError) {
2829
+ CvMat.find_fundamental_mat(mat1, DUMMY_OBJ, method, :with_status => true)
2830
+ }
2831
+ assert_raise(TypeError) {
2832
+ CvMat.find_fundamental_mat(mat1, mat2, method, DUMMY_OBJ)
2833
+ }
2834
+ }
2835
+ assert_raise(TypeError) {
2836
+ CvMat.find_fundamental_mat(mat1, mat2, DUMMY_OBJ, :with_status => true)
2837
+ }
2838
+ end
2839
+
2840
+ def test_compute_correspond_epilines
2841
+ test_func = lambda { |mat1, mat2, f_mat_arr, num_points|
2842
+ f_mat = CvMat.new(3, 3, CV_64F, 1)
2843
+ f_mat_arr.each_with_index { |a, i|
2844
+ f_mat[i] = CvScalar.new(a)
2845
+ }
2846
+
2847
+ line = CvMat.compute_correspond_epilines(mat1, 1, f_mat)
2848
+ assert_equal(num_points, line.rows)
2849
+ assert_equal(3, line.cols)
2850
+
2851
+ expected = [[-0.221257, -0.975215, 6.03758],
2852
+ [0.359337, -0.933208, -3.61419],
2853
+ [0.958304, -0.28575, -15.0573],
2854
+ [0.73415, -0.678987, -10.4037],
2855
+ [0.0208539, -0.999783, 2.11625],
2856
+ [0.284451, -0.958691, -2.31993],
2857
+ [0.624647, -0.780907, -8.35208],
2858
+ [0.618494, -0.785789, -8.23888],
2859
+ [0.766694, -0.642012, -11.0298],
2860
+ [0.700293, -0.713855, -9.76109]]
2861
+
2862
+ expected.size.times { |i|
2863
+ assert_in_delta(expected[i][0], line[i, 0][0], 1.0e-3)
2864
+ assert_in_delta(expected[i][1], line[i, 1][0], 1.0e-3)
2865
+ assert_in_delta(expected[i][2], line[i, 2][0], 1.0e-3)
2866
+ }
2867
+
2868
+ assert_raise(ArgumentError) {
2869
+ m = CvMat.new(10, 10, CV_32F, 1)
2870
+ CvMat.compute_correspond_epilines(m, 1, f_mat)
2871
+ }
2872
+ }
2873
+
2874
+ num_points = 10
2875
+ # input points are Nx2 matrix
2876
+ points1 =[[17, 175],
2877
+ [370, 24],
2878
+ [192, 456],
2879
+ [614, 202],
2880
+ [116, 111],
2881
+ [305, 32],
2882
+ [249, 268],
2883
+ [464, 157],
2884
+ [259, 333],
2885
+ [460, 224]]
2886
+
2887
+ points2 = [[295, 28],
2888
+ [584, 221],
2889
+ [67, 172],
2890
+ [400, 443],
2891
+ [330, 9],
2892
+ [480, 140],
2893
+ [181, 140],
2894
+ [350, 265],
2895
+ [176, 193],
2896
+ [333, 313]]
2897
+
2898
+ mat1 = CvMat.new(num_points, 2, CV_64F, 1)
2899
+ mat2 = CvMat.new(num_points, 2, CV_64F, 1)
2900
+ points1.flatten.each_with_index { |pt, i|
2901
+ mat1[i] = CvScalar.new(pt)
2902
+ }
2903
+ points2.flatten.each_with_index { |pt, i|
2904
+ mat2[i] = CvScalar.new(pt)
2905
+ }
2906
+
2907
+ # pre computed f matrix from points1, points2
2908
+ # f_mat = CvMat.find_fundamental_mat(mat1, mat2, CV_FM_LMEDS)
2909
+ f_mat_arr = [0.000266883, 0.000140277, -0.0445223,
2910
+ -0.00012592, 0.000245543, -0.108868,
2911
+ -0.00407942, -0.00291097, 1]
2912
+ test_func.call(mat1, mat2, f_mat_arr, num_points)
2913
+
2914
+ # input points are 2xN matrix
2915
+ points1 = [[17, 370, 192, 614, 116, 305, 249, 464, 259, 460],
2916
+ [175, 24, 456, 202, 111, 32, 268, 157, 333, 224]]
2917
+
2918
+ points2 = [[295, 584, 67, 400, 330, 480, 181, 350, 176, 333],
2919
+ [28, 221, 172, 443, 9, 140, 140, 265, 193, 313]]
2920
+
2921
+ mat1 = CvMat.new(2, num_points, CV_64F, 1)
2922
+ mat2 = CvMat.new(2, num_points, CV_64F, 1)
2923
+ points1.flatten.each_with_index { |pt, i|
2924
+ mat1[i] = CvScalar.new(pt)
2925
+ }
2926
+ points2.flatten.each_with_index { |pt, i|
2927
+ mat2[i] = CvScalar.new(pt)
2928
+ }
2929
+ test_func.call(mat1, mat2, f_mat_arr, num_points)
2930
+
2931
+
2932
+ f_mat = CvMat.new(3, 3, CV_64F, 1)
2933
+ f_mat_arr.each_with_index { |a, i|
2934
+ f_mat[i] = CvScalar.new(a)
2935
+ }
2936
+ assert_raise(TypeError) {
2937
+ CvMat.compute_correspond_epilines(DUMMY_OBJ, 1, f_mat)
2938
+ }
2939
+ assert_raise(TypeError) {
2940
+ CvMat.compute_correspond_epilines(mat1, DUMMY_OBJ, f_mat)
2941
+ }
2942
+ assert_raise(TypeError) {
2943
+ CvMat.compute_correspond_epilines(mat1, 1, DUMMY_OBJ)
2944
+ }
2945
+ end
2946
+ end
2947
+