afeld-opencv 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,150 @@
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::CvContour
10
+ class TestCvContour < OpenCVTestCase
11
+ def test_APPROX_OPTION
12
+ assert_equal(0, CvContour::APPROX_OPTION[:method])
13
+ assert_equal(1.0, CvContour::APPROX_OPTION[:accuracy])
14
+ assert_false(CvContour::APPROX_OPTION[:recursive])
15
+ end
16
+
17
+ def test_initialize
18
+ contour = CvContour.new
19
+ assert_not_nil(contour)
20
+ assert_equal(CvContour, contour.class)
21
+ assert(contour.is_a? CvSeq)
22
+ end
23
+
24
+ def test_rect
25
+ contour = CvContour.new
26
+ assert_not_nil(contour.rect)
27
+ assert_equal(CvRect, contour.rect.class)
28
+ end
29
+
30
+ def test_color
31
+ contour = CvContour.new
32
+ assert_equal(0, contour.color)
33
+ contour.color = 1
34
+ assert_equal(1, contour.color)
35
+ end
36
+
37
+ def test_reserved
38
+ reserved = CvContour.new.reserved
39
+ assert_equal(Array, reserved.class)
40
+ assert_equal(3, reserved.size)
41
+ end
42
+
43
+ def test_approx_poly
44
+ mat0 = create_cvmat(128, 128, :cv8u, 1) { |j, i|
45
+ (j - 64) ** 2 + (i - 64) ** 2 <= (32 ** 2) ? CvColor::White : CvColor::Black
46
+ }
47
+ contours = mat0.find_contours(:mode => CV_RETR_EXTERNAL)
48
+
49
+ poly = contours.approx_poly
50
+ assert_equal(CvContour, poly.class)
51
+ assert(poly.size > 0)
52
+ assert(poly.all? { |c| c.class == CvPoint })
53
+
54
+ poly = contours.approx_poly(:method => :dp)
55
+ assert_equal(CvContour, poly.class)
56
+ assert(poly.size > 0)
57
+ assert(poly.all? { |c| c.class == CvPoint })
58
+
59
+ poly = contours.approx_poly(:accuracy => 2.0)
60
+ assert_equal(CvContour, poly.class)
61
+ assert(poly.size > 0)
62
+ assert(poly.all? { |c| c.class == CvPoint })
63
+
64
+ [true, false, 1, 0].each { |recursive|
65
+ poly = contours.approx_poly(:recursive => recursive)
66
+ assert_equal(CvContour, poly.class)
67
+ assert(poly.size > 0)
68
+ assert(poly.all? { |c| c.class == CvPoint })
69
+ }
70
+
71
+ poly = contours.approx_poly(:method => :dp, :accuracy => 2.0, :recursive => false)
72
+ assert_equal(CvContour, poly.class)
73
+ assert(poly.size > 0)
74
+ assert(poly.all? { |c| c.class == CvPoint })
75
+
76
+ # Uncomment the following lines to show the result
77
+ # poly = contours.approx_poly(:accuracy => 3.0)
78
+ # dst = mat0.clone.zero
79
+ # begin
80
+ # dst.draw_contours!(poly, CvColor::White, CvColor::Black, 2,
81
+ # :thickness => 1, :line_type => :aa)
82
+ # end while (poly = poly.h_next)
83
+ # snap dst
84
+ end
85
+
86
+ def test_bounding_rect
87
+ mat0 = create_cvmat(128, 128, :cv8u, 1) { |j, i|
88
+ (j - 64) ** 2 + (i - 64) ** 2 <= (32 ** 2) ? CvColor::White : CvColor::Black
89
+ }
90
+ contours = mat0.find_contours
91
+ rect = contours.bounding_rect
92
+ assert_equal(CvRect, rect.class)
93
+ assert_equal(32, rect.x)
94
+ assert_equal(32, rect.y)
95
+ assert_equal(65, rect.width)
96
+ assert_equal(65, rect.height)
97
+ end
98
+
99
+ def test_create_tree
100
+ mat0 = create_cvmat(128, 128, :cv8u, 1) { |j, i|
101
+ (j - 64) ** 2 + (i - 64) ** 2 <= (32 ** 2) ? CvColor::White : CvColor::Black
102
+ }
103
+ contour = mat0.find_contours
104
+ tree = contour.create_tree
105
+ assert_equal(CvContourTree, tree.class)
106
+ assert_equal(34, tree.p1.x)
107
+ assert_equal(53, tree.p1.y)
108
+ assert_equal(0, tree.p2.x)
109
+ assert_equal(0, tree.p2.y)
110
+ end
111
+
112
+ def test_in
113
+ mat0 = create_cvmat(128, 128, :cv8u, 1) { |j, i|
114
+ (j - 64) ** 2 + (i - 64) ** 2 <= (32 ** 2) ? CvColor::White : CvColor::Black
115
+ }
116
+ contour = mat0.find_contours
117
+ assert(contour.in? CvPoint.new(64, 64))
118
+ assert_false(contour.in? CvPoint.new(0, 0))
119
+ assert_nil(contour.in? CvPoint.new(64, 32))
120
+ end
121
+
122
+ def test_measure_distance
123
+ mat0 = create_cvmat(128, 128, :cv8u, 1) { |j, i|
124
+ (j - 64) ** 2 + (i - 64) ** 2 <= (32 ** 2) ? CvColor::White : CvColor::Black
125
+ }
126
+ contour = mat0.find_contours
127
+ assert_in_delta(-0.7071, contour.measure_distance(CvPoint.new(63, 32)), 0.01)
128
+ assert_in_delta(31.01, contour.measure_distance(CvPoint.new(64, 64)), 0.01)
129
+ end
130
+
131
+ def test_point_polygon_test
132
+ mat0 = create_cvmat(128, 128, :cv8u, 1) { |j, i|
133
+ (j - 64) ** 2 + (i - 64) ** 2 <= (32 ** 2) ? CvColor::White : CvColor::Black
134
+ }
135
+ contour = mat0.find_contours
136
+
137
+ assert_equal(1, contour.point_polygon_test(CvPoint.new(64, 64), 0))
138
+ assert_equal(1, contour.point_polygon_test(CvPoint.new(64, 64), false))
139
+ assert_equal(-1, contour.point_polygon_test(CvPoint.new(0, 0), 0))
140
+ assert_equal(-1, contour.point_polygon_test(CvPoint.new(0, 0), false))
141
+ assert_equal(0, contour.point_polygon_test(CvPoint.new(64, 32), 0))
142
+ assert_equal(0, contour.point_polygon_test(CvPoint.new(64, 32), false))
143
+
144
+ assert_in_delta(-0.7071, contour.point_polygon_test(CvPoint.new(63, 32), 1), 0.01)
145
+ assert_in_delta(-0.7071, contour.point_polygon_test(CvPoint.new(63, 32), true), 0.01)
146
+ assert_in_delta(31.01, contour.point_polygon_test(CvPoint.new(64, 64), 1), 0.01)
147
+ assert_in_delta(31.01, contour.point_polygon_test(CvPoint.new(64, 64), true), 0.01)
148
+ end
149
+ end
150
+
@@ -0,0 +1,43 @@
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::CvContourTree
10
+ class TestCvContourTree < OpenCVTestCase
11
+ def setup
12
+ @tree = CvContourTree.new(CvPoint)
13
+ end
14
+
15
+ def test_initialize
16
+ tree = CvContourTree.new(CvPoint)
17
+ assert_equal(CvContourTree, tree.class)
18
+ assert(tree.is_a? CvSeq)
19
+ end
20
+
21
+ def test_p1
22
+ assert_equal(CvPoint, @tree.p1.class)
23
+ end
24
+
25
+ def test_p2
26
+ assert_equal(CvPoint, @tree.p2.class)
27
+ end
28
+
29
+ def test_contour
30
+ mat0 = create_cvmat(128, 128, :cv8u, 1) { |j, i|
31
+ (j - 64) ** 2 + (i - 64) ** 2 <= (32 ** 2) ? CvColor::White : CvColor::Black
32
+ }
33
+ contour = mat0.find_contours
34
+ tree = contour.create_tree
35
+ contour = tree.contour(CvTermCriteria.new(100, 0.01))
36
+ assert_equal(CvContour, contour.class)
37
+
38
+ assert_raise(CvStsBadArg) {
39
+ tree.contour(CvTermCriteria.new(0, 0))
40
+ }
41
+ end
42
+ end
43
+
@@ -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::CvError
10
+ class TestCvError < OpenCVTestCase
11
+ def test_cverror
12
+ assert_equal(StandardError, CvError.superclass)
13
+ begin
14
+ raise CvError
15
+ rescue
16
+ assert_equal(CvError, $!.class)
17
+ end
18
+ end
19
+
20
+ def test_subclass
21
+ errors = [CvStsBackTrace, CvStsError, CvStsInternal, CvStsNoMem, CvStsBadArg, CvStsBadFunc, CvStsNoConv, CvStsAutoTrace, CvHeaderIsNull, CvBadImageSize, CvBadOffset, CvBadDataPtr, CvBadStep, CvBadModelOrChSeq, CvBadNumChannels, CvBadNumChannel1U, CvBadDepth, CvBadAlphaChannel, CvBadOrder, CvBadOrigin, CvBadAlign, CvBadCallBack, CvBadTileSize, CvBadCOI, CvBadROISize, CvMaskIsTiled, CvStsNullPtr, CvStsVecLengthErr, CvStsFilterStructContentErr, CvStsKernelStructContentErr, CvStsFilterOffsetErr, CvStsBadSize, CvStsDivByZero, CvStsInplaceNotSupported, CvStsObjectNotFound, CvStsUnmatchedFormats, CvStsBadFlag, CvStsBadPoint, CvStsBadMask, CvStsUnmatchedSizes, CvStsUnsupportedFormat, CvStsOutOfRange, CvStsParseError, CvStsNotImplemented, CvStsBadMemBlock, CvStsAssert, CvGpuNotSupported, CvGpuApiCallError]
22
+
23
+ errors.each { |err|
24
+ assert_equal(CvError, err.superclass)
25
+
26
+ begin
27
+ raise err
28
+ rescue err
29
+ assert_equal(err, $!.class)
30
+ rescue
31
+ flunk("Failed to catch #{err}")
32
+ end
33
+
34
+ begin
35
+ raise err
36
+ rescue CvError
37
+ assert_equal(err, $!.class)
38
+ rescue
39
+ flunk("Failed to catch #{err}")
40
+ end
41
+
42
+ begin
43
+ raise err
44
+ rescue
45
+ assert_equal(err, $!.class)
46
+ end
47
+ }
48
+ end
49
+ end
50
+
@@ -0,0 +1,65 @@
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::CvFeatureTree
10
+ class TestCvFeatureTree < OpenCVTestCase
11
+ def test_initialize
12
+ desc = CvMat.new(1, 1, :cv32f, 1)
13
+ ft = CvFeatureTree.new(desc)
14
+ assert_equal(CvFeatureTree, ft.class)
15
+
16
+ assert_raise(TypeError) {
17
+ CvFeatureTree.new(DUMMY_OBJ)
18
+ }
19
+ end
20
+
21
+ def test_find_feature
22
+ dim = 2
23
+ points = []
24
+ points << [99, 51]
25
+ points << [52, 57]
26
+ points << [57, 42]
27
+ points << [13, 39]
28
+ points << [15, 68]
29
+ points << [75, 11]
30
+ points << [69, 62]
31
+ points << [52, 46]
32
+ points << [0, 64]
33
+ points << [67, 16]
34
+
35
+ desc1 = CvMat.new(points.size, dim, :cv32f, 1)
36
+ desc1.set_data(points)
37
+
38
+ pt = [[50, 50], [11, 40]]
39
+ desc2 = CvMat.new(pt.size, dim, :cv32f, 1)
40
+ desc2.set_data(pt)
41
+
42
+ ft = CvFeatureTree.new(desc1)
43
+ results, dist = ft.find_features(desc2, 1, 10)
44
+
45
+ assert_equal(CvMat, results.class)
46
+ assert_equal(CvMat, dist.class)
47
+
48
+ assert_equal(7, results[0][0].to_i)
49
+ assert_in_delta(4.472, dist[0][0], 0.001)
50
+
51
+ assert_equal(3, results[1][0].to_i)
52
+ assert_in_delta(2.236, dist[1][0], 0.001)
53
+
54
+ assert_raise(TypeError) {
55
+ ft.find_features(DUMMY_OBJ, 1, 10)
56
+ }
57
+ assert_raise(TypeError) {
58
+ ft.find_features(desc2, DUMMY_OBJ, 10)
59
+ }
60
+ assert_raise(TypeError) {
61
+ ft.find_features(desc2, 1, DUMMY_OBJ)
62
+ }
63
+ end
64
+ end
65
+
@@ -0,0 +1,58 @@
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::CvFont
10
+ class TestCvFont < OpenCVTestCase
11
+ def test_FACE
12
+ assert_equal(0, CvFont::FACE[:simplex])
13
+ assert_equal(1, CvFont::FACE[:plain])
14
+ assert_equal(2, CvFont::FACE[:duplex])
15
+ assert_equal(4, CvFont::FACE[:triplex])
16
+ assert_equal(5, CvFont::FACE[:complex_small])
17
+ assert_equal(6, CvFont::FACE[:script_simplex])
18
+ assert_equal(7, CvFont::FACE[:script_complex])
19
+ end
20
+
21
+ def test_FONT_OPTION
22
+ assert_equal(1.0, CvFont::FONT_OPTION[:hscale])
23
+ assert_equal(1.0, CvFont::FONT_OPTION[:vscale])
24
+ assert_equal(0, CvFont::FONT_OPTION[:shear])
25
+ assert_equal(1, CvFont::FONT_OPTION[:thickness])
26
+ assert_equal(8, CvFont::FONT_OPTION[:line_type])
27
+ end
28
+
29
+ def test_initialize
30
+ font = CvFont.new(:simplex)
31
+ assert_equal(0, font.face)
32
+ assert_equal(1.0, font.hscale)
33
+ assert_equal(1.0, font.vscale)
34
+ assert_equal(0, font.shear)
35
+ assert_equal(1, font.thickness)
36
+ assert_equal(8, font.line_type)
37
+ assert_false(font.italic)
38
+
39
+ font = CvFont.new(:plain, :hscale => 2.5, :vscale => 3.5,
40
+ :shear => 0.5, :thickness => 3, :line_type => 2, :italic => false)
41
+ assert_equal(1, font.face)
42
+ assert_equal(2.5, font.hscale)
43
+ assert_equal(3.5, font.vscale)
44
+ assert_equal(0.5, font.shear)
45
+ assert_equal(3, font.thickness)
46
+ assert_equal(2, font.line_type)
47
+ assert_false(font.italic)
48
+
49
+ font = CvFont.new(:simplex, :italic => true)
50
+ assert_equal(16, font.face)
51
+ assert(font.italic)
52
+
53
+ assert_raise(ArgumentError) {
54
+ CvFont.new(:foo)
55
+ }
56
+ end
57
+ end
58
+
@@ -0,0 +1,63 @@
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::CvHaarClassifierCascade
10
+ class TestCvHaarClassifierCascade < OpenCVTestCase
11
+ def setup
12
+ @cascade = CvHaarClassifierCascade.load(HAARCASCADE_FRONTALFACE_ALT)
13
+ end
14
+
15
+ def test_load
16
+ assert_equal(CvHaarClassifierCascade, @cascade.class)
17
+ assert_raise(ArgumentError) {
18
+ CvHaarClassifierCascade.load('not/exist.xml')
19
+ }
20
+ end
21
+
22
+ def test_detect_objects
23
+ img = CvMat.load(FILENAME_LENA256x256)
24
+
25
+ detected = @cascade.detect_objects(img)
26
+ assert_equal(CvSeq, detected.class)
27
+ assert_equal(1, detected.size)
28
+ assert_equal(CvAvgComp, detected[0].class)
29
+ assert_equal(106, detected[0].x)
30
+ assert_equal(100, detected[0].y)
31
+ assert_equal(89, detected[0].width)
32
+ assert_equal(89, detected[0].height)
33
+ assert_equal(48, detected[0].neighbors)
34
+
35
+ detected = @cascade.detect_objects(img) { |face|
36
+ assert_equal(106, face.x)
37
+ assert_equal(100, face.y)
38
+ assert_equal(89, face.width)
39
+ assert_equal(89, face.height)
40
+ assert_equal(48, face.neighbors)
41
+ }
42
+ assert_equal(CvSeq, detected.class)
43
+ assert_equal(1, detected.size)
44
+ assert_equal(CvAvgComp, detected[0].class)
45
+
46
+ detected = @cascade.detect_objects(img, :scale_factor => 2.0, :flags => CV_HAAR_DO_CANNY_PRUNING,
47
+ :min_neighbors => 5, :min_size => CvSize.new(10, 10),
48
+ :max_size => CvSize.new(100, 100))
49
+ assert_equal(CvSeq, detected.class)
50
+ assert_equal(1, detected.size)
51
+ assert_equal(CvAvgComp, detected[0].class)
52
+ assert_equal(109, detected[0].x)
53
+ assert_equal(102, detected[0].y)
54
+ assert_equal(80, detected[0].width)
55
+ assert_equal(80, detected[0].height)
56
+ assert_equal(7, detected[0].neighbors)
57
+
58
+ assert_raise(TypeError) {
59
+ @cascade.detect_objects('foo')
60
+ }
61
+ end
62
+ end
63
+
@@ -0,0 +1,271 @@
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::CvHistogram
10
+ class TestCvHistogram < OpenCVTestCase
11
+ def setup
12
+ @img = IplImage.load(FILENAME_LENA32x32, 0)
13
+
14
+ dim = 1
15
+ sizes = [8]
16
+ ranges = [[0, 255]]
17
+ @hist1 = CvHistogram.new(dim, sizes, CV_HIST_ARRAY, ranges, true).calc_hist!([@img])
18
+
19
+ dim = 2
20
+ sizes = [8, 16]
21
+ ranges = [[0, 255], [0, 255]]
22
+ @hist2 = CvHistogram.new(dim, sizes, CV_HIST_SPARSE, ranges, true).calc_hist!([@img, @img])
23
+ end
24
+
25
+ def teardown
26
+ @hist1 = nil
27
+ @hist2 = nil
28
+ GC.start
29
+ end
30
+
31
+ def test_initialize
32
+ dim = 1
33
+ sizes = [256]
34
+ ranges = [[0, 256]]
35
+ hist1 = CvHistogram.new(dim, sizes, CV_HIST_ARRAY, ranges, true)
36
+ hist2 = CvHistogram.new(dim, sizes, CV_HIST_ARRAY)
37
+ hist3 = CvHistogram.new(dim, sizes, CV_HIST_ARRAY, nil)
38
+ hist4 = CvHistogram.new(dim, sizes, CV_HIST_ARRAY, nil, false)
39
+ end
40
+
41
+ def test_is_uniform
42
+ assert(@hist1.is_uniform?)
43
+ assert(@hist2.is_uniform?)
44
+ end
45
+
46
+ def test_is_sparse
47
+ assert_false(@hist1.is_sparse?)
48
+ assert(@hist2.is_sparse?)
49
+ end
50
+
51
+ def test_has_range
52
+ assert(@hist1.has_range?)
53
+ assert(@hist2.has_range?)
54
+ end
55
+
56
+ def test_calc_hist
57
+ img = IplImage.new(1, 1, :cv8u, 1)
58
+ assert_equal(CvHistogram, @hist1.calc_hist([img]).class)
59
+ assert_equal(CvHistogram, @hist1.calc_hist([img, img]).class)
60
+ assert_equal(CvHistogram, @hist1.calc_hist([img], true).class)
61
+ assert_equal(CvHistogram, @hist1.calc_hist([img, img], false).class)
62
+ mask = CvMat.new(1, 1, :cv8u, 1)
63
+ assert_equal(CvHistogram, @hist1.calc_hist([img], true, mask).class)
64
+
65
+ assert_raise(TypeError) {
66
+ @hist1.calc_hist(img)
67
+ }
68
+ assert_raise(TypeError) {
69
+ @hist1.calc_hist([DUMMY_OBJ])
70
+ }
71
+ assert_raise(TypeError) {
72
+ @hist1.calc_hist(nil)
73
+ }
74
+ assert_raise(TypeError) {
75
+ @hist1.calc_hist([img], true, DUMMY_OBJ)
76
+ }
77
+ assert_raise(CvStsBadArg) {
78
+ @hist1.calc_hist([])
79
+ }
80
+ end
81
+
82
+ def test_aref
83
+ expected = [0.0, 102.0, 189.0, 244.0, 285.0, 140.0, 64.0, 0.0]
84
+ expected.each_with_index { |x, i|
85
+ assert_in_delta(x, @hist1[i], 0.001)
86
+ assert_in_delta(x, @hist1.query_hist_value(i), 0.001)
87
+ }
88
+ end
89
+
90
+ def test_min_max_value
91
+ min, max, min_loc, max_loc = @hist1.min_max_value
92
+ assert_in_delta(0.0, min, 0.001)
93
+ assert_in_delta(285.0, max, 0.001)
94
+ assert_equal(Array, min_loc.class)
95
+ assert_equal(Array, max_loc.class)
96
+ assert_equal(1, min_loc.size)
97
+ assert_equal(1, max_loc.size)
98
+ assert_equal(0, min_loc[0])
99
+ assert_equal(4, max_loc[0])
100
+
101
+ min, max, min_loc, max_loc = @hist2.min_max_value
102
+ assert_in_delta(14.0, min, 0.001)
103
+ assert_in_delta(158.0, max, 0.001)
104
+ assert_equal(Array, min_loc.class)
105
+ assert_equal(Array, max_loc.class)
106
+ assert_equal(2, min_loc.size)
107
+ assert_equal(2, max_loc.size)
108
+ assert_equal(1, min_loc[0])
109
+ assert_equal(2, min_loc[1])
110
+ assert_equal(4, max_loc[0])
111
+ assert_equal(9, max_loc[1])
112
+ end
113
+
114
+ def test_dims
115
+ dims, sizes = @hist1.dims
116
+ assert_equal(1, dims)
117
+ assert_equal(Array, sizes.class)
118
+ assert_equal(1, sizes.size)
119
+ assert_equal(8, sizes[0])
120
+
121
+ dims, sizes = @hist2.dims
122
+ assert_equal(2, dims)
123
+ assert_equal(Array, sizes.class)
124
+ assert_equal(2, sizes.size)
125
+ assert_equal(8, sizes[0])
126
+ assert_equal(16, sizes[1])
127
+ end
128
+
129
+ def test_copy_hist
130
+ expected = [0.0, 102.0, 189.0, 244.0, 285.0, 140.0, 64.0, 0.0]
131
+ hist = @hist1.copy_hist
132
+ expected.each_with_index { |x, i|
133
+ assert_in_delta(x, hist[i], 0.001)
134
+ assert_in_delta(x, hist.query_hist_value(i), 0.001)
135
+ }
136
+ end
137
+
138
+ def test_clear_hist
139
+ @hist1.clear_hist!
140
+ dims, sizes = @hist1.dims
141
+ dims.times { |i|
142
+ assert_in_delta(0.0, @hist1[i], 0.001)
143
+ }
144
+ end
145
+
146
+ def test_normalize_hist
147
+ @hist1.normalize_hist!(100)
148
+ expected = [0.0, 9.96, 18.46, 23.83, 27.83, 13.67, 6.25, 0.0]
149
+ expected.each_with_index { |x, i|
150
+ assert_in_delta(x, @hist1[i], 0.01)
151
+ }
152
+ end
153
+
154
+ def test_thresh_hist
155
+ @hist1.thresh_hist!(150)
156
+ expected = [0.0, 0.0, 189.0, 244.0, 285.0, 0.0, 0.0, 0.0]
157
+ expected.each_with_index { |x, i|
158
+ assert_in_delta(x, @hist1[i], 0.001)
159
+ }
160
+ end
161
+
162
+ def test_set_hist_bin_ranges
163
+ dim = 1
164
+ sizes = [8]
165
+
166
+ hist = CvHistogram.new(dim, sizes, CV_HIST_ARRAY)
167
+ assert_false(hist.has_range?)
168
+ assert(hist.is_uniform?)
169
+
170
+ ranges = [[0, 255]]
171
+ hist.set_hist_bin_ranges!(ranges, true)
172
+ assert(hist.has_range?)
173
+ assert(hist.is_uniform?)
174
+
175
+ assert_raise(TypeError) {
176
+ hist.set_hist_bin_ranges!(DUMMY_OBJ)
177
+ }
178
+ assert_raise(TypeError) {
179
+ hist.set_hist_bin_ranges!([DUMMY_OBJ])
180
+ }
181
+ end
182
+
183
+ def test_calc_back_project
184
+ back_project = @hist1.calc_back_project([@img])
185
+ assert_equal(@img.class, back_project.class)
186
+ assert_equal('2a0097af1ab4f9343e4feaae3a780c93', hash_img(back_project))
187
+
188
+ assert_raise(TypeError) {
189
+ @hist1.calc_back_project(DUMMY_OBJ)
190
+ }
191
+ assert_raise(TypeError) {
192
+ @hist1.calc_back_project([DUMMY_OBJ])
193
+ }
194
+ end
195
+
196
+ def test_calc_back_project_patch
197
+ img = IplImage.load(FILENAME_LENA256x256, 0)
198
+ template = IplImage.load(FILENAME_LENA_EYES, 0)
199
+
200
+ dim = 1
201
+ sizes = [8]
202
+ ranges = [[0, 255]]
203
+ hist = CvHistogram.new(dim, sizes, CV_HIST_ARRAY, ranges).calc_hist!([template])
204
+ back_project = hist.calc_back_project_patch([img], template.size, CV_COMP_CORREL, 1.0)
205
+ assert_equal('e6497e45c6f2f715328bbc2fefe31581', hash_img(back_project))
206
+
207
+ assert_raise(TypeError) {
208
+ hist.calc_back_project_patch(DUMMY_OBJ, template.size, CV_COMP_CORREL, 1.0)
209
+ }
210
+ assert_raise(TypeError) {
211
+ hist.calc_back_project_patch([DUMMY_OBJ], template.size, CV_COMP_CORREL, 1.0)
212
+ }
213
+
214
+ # Uncomment the following line to show the result
215
+ # min_val, max_val, min_loc, max_loc = back_project.min_max_loc
216
+ # result = img.rectangle(max_loc, CvPoint.new(max_loc.x + template.width, max_loc.y + template.height),
217
+ # :thickness => 2)
218
+ # snap img, template, back_project, result
219
+ end
220
+
221
+ def test_compare_hist
222
+ img = IplImage.load(FILENAME_CAT, 0)
223
+ dim, sizes = @hist1.dims
224
+ ranges = [[0, 255]]
225
+ hist = CvHistogram.new(dim, sizes, CV_HIST_ARRAY, ranges).calc_hist!([img])
226
+
227
+ assert_in_delta(0.7446, CvHistogram.compare_hist(@hist1, hist, CV_COMP_CORREL), 0.001)
228
+ assert_in_delta(184459.0, CvHistogram.compare_hist(@hist1, hist, CV_COMP_CHISQR), 1.0)
229
+ assert_in_delta(1024.0, CvHistogram.compare_hist(@hist1, hist, CV_COMP_INTERSECT), 1.0)
230
+ assert_in_delta(0.2955, CvHistogram.compare_hist(@hist1, hist, CV_COMP_BHATTACHARYYA), 0.001)
231
+
232
+ assert_raise(TypeError) {
233
+ CvHistogram.compare_hist(DUMMY_OBJ, hist, CV_COMP_CORREL)
234
+ }
235
+ assert_raise(TypeError) {
236
+ CvHistogram.compare_hist(@hist1, DUMMY_OBJ, CV_COMP_CORREL)
237
+ }
238
+ assert_raise(TypeError) {
239
+ CvHistogram.compare_hist(@hist1, hist, DUMMY_OBJ)
240
+ }
241
+ end
242
+
243
+ def test_calc_prob_density
244
+ img = IplImage.load(FILENAME_CAT, 0)
245
+ dim, sizes = @hist1.dims
246
+ ranges = [[0, 255]]
247
+ hist = CvHistogram.new(dim, sizes, CV_HIST_ARRAY, ranges).calc_hist!([img])
248
+ dst = CvHistogram.calc_prob_density(hist, @hist1)
249
+
250
+ assert_equal(CvHistogram, dst.class)
251
+ dim, sizes = dst.dims
252
+ expected_dim, expected_sizes = @hist1.dims
253
+ assert_equal(expected_dim, dim)
254
+ expected_sizes.each_with_index { |x, i|
255
+ assert_equal(x, sizes[i])
256
+ }
257
+
258
+ expected = [0.0, 1.437, 1.135, 1.092, 2.323, 3.712, 3.103, 0.0]
259
+ expected.each_with_index { |x, i|
260
+ assert_in_delta(x, dst[i], 0.001)
261
+ }
262
+
263
+ assert_raise(TypeError) {
264
+ CvHistogram.calc_prob_density(DUMMY_OBJ, @hist1)
265
+ }
266
+ assert_raise(TypeError) {
267
+ CvHistogram.calc_prob_density(hist, DUMMY_OBJ)
268
+ }
269
+ end
270
+ end
271
+