afeld-opencv 0.0.8

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