jf-ruby-opencv 0.0.18

Sign up to get free protection for your applications and to get access to all the features.
Files changed (241) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +28 -0
  3. data/.yardopts +3 -0
  4. data/DEVELOPERS_NOTE.md +137 -0
  5. data/Gemfile +9 -0
  6. data/History.txt +5 -0
  7. data/License.txt +30 -0
  8. data/Manifest.txt +239 -0
  9. data/README.md +94 -0
  10. data/Rakefile +99 -0
  11. data/config.yml +7 -0
  12. data/examples/alpha_blend.rb +21 -0
  13. data/examples/contours/bitmap-contours-with-labels.png +0 -0
  14. data/examples/contours/bitmap-contours.png +0 -0
  15. data/examples/contours/bounding-box-detect-canny.rb +62 -0
  16. data/examples/contours/contour_retrieval_modes.rb +139 -0
  17. data/examples/contours/rotated-boxes.jpg +0 -0
  18. data/examples/convexhull.rb +47 -0
  19. data/examples/face_detect.rb +20 -0
  20. data/examples/facerec/create_csv.rb +43 -0
  21. data/examples/facerec/facerec_eigenfaces.rb +132 -0
  22. data/examples/facerec/facerec_fisherfaces.rb +131 -0
  23. data/examples/facerec/facerec_lbph.rb +116 -0
  24. data/examples/facerec/readme.md +111 -0
  25. data/examples/find_obj.rb +169 -0
  26. data/examples/houghcircle.rb +22 -0
  27. data/examples/images/box.png +0 -0
  28. data/examples/images/box_in_scene.png +0 -0
  29. data/examples/images/inpaint.png +0 -0
  30. data/examples/images/lena-256x256.jpg +0 -0
  31. data/examples/images/lena-eyes.jpg +0 -0
  32. data/examples/images/lenna-rotated.jpg +0 -0
  33. data/examples/images/lenna.jpg +0 -0
  34. data/examples/images/stuff.jpg +0 -0
  35. data/examples/images/tiffany.jpg +0 -0
  36. data/examples/inpaint.rb +57 -0
  37. data/examples/match_kdtree.rb +88 -0
  38. data/examples/match_template.rb +26 -0
  39. data/examples/paint.rb +70 -0
  40. data/examples/snake.rb +43 -0
  41. data/ext/opencv/algorithm.cpp +291 -0
  42. data/ext/opencv/algorithm.h +38 -0
  43. data/ext/opencv/curve.cpp +127 -0
  44. data/ext/opencv/curve.h +34 -0
  45. data/ext/opencv/cvavgcomp.cpp +64 -0
  46. data/ext/opencv/cvavgcomp.h +39 -0
  47. data/ext/opencv/cvbox2d.cpp +195 -0
  48. data/ext/opencv/cvbox2d.h +61 -0
  49. data/ext/opencv/cvcapture.cpp +633 -0
  50. data/ext/opencv/cvcapture.h +82 -0
  51. data/ext/opencv/cvchain.cpp +233 -0
  52. data/ext/opencv/cvchain.h +46 -0
  53. data/ext/opencv/cvcircle32f.cpp +126 -0
  54. data/ext/opencv/cvcircle32f.h +52 -0
  55. data/ext/opencv/cvconnectedcomp.cpp +156 -0
  56. data/ext/opencv/cvconnectedcomp.h +49 -0
  57. data/ext/opencv/cvcontour.cpp +384 -0
  58. data/ext/opencv/cvcontour.h +51 -0
  59. data/ext/opencv/cvcontourtree.cpp +96 -0
  60. data/ext/opencv/cvcontourtree.h +41 -0
  61. data/ext/opencv/cvconvexitydefect.cpp +92 -0
  62. data/ext/opencv/cvconvexitydefect.h +42 -0
  63. data/ext/opencv/cverror.cpp +115 -0
  64. data/ext/opencv/cverror.h +28 -0
  65. data/ext/opencv/cvfeaturetree.cpp +123 -0
  66. data/ext/opencv/cvfeaturetree.h +55 -0
  67. data/ext/opencv/cvfont.cpp +228 -0
  68. data/ext/opencv/cvfont.h +64 -0
  69. data/ext/opencv/cvhaarclassifiercascade.cpp +148 -0
  70. data/ext/opencv/cvhaarclassifiercascade.h +39 -0
  71. data/ext/opencv/cvhistogram.cpp +717 -0
  72. data/ext/opencv/cvhistogram.h +73 -0
  73. data/ext/opencv/cvhumoments.cpp +178 -0
  74. data/ext/opencv/cvhumoments.h +51 -0
  75. data/ext/opencv/cvline.cpp +159 -0
  76. data/ext/opencv/cvline.h +54 -0
  77. data/ext/opencv/cvmat.cpp +6086 -0
  78. data/ext/opencv/cvmat.h +290 -0
  79. data/ext/opencv/cvmemstorage.cpp +73 -0
  80. data/ext/opencv/cvmemstorage.h +50 -0
  81. data/ext/opencv/cvmoments.cpp +293 -0
  82. data/ext/opencv/cvmoments.h +75 -0
  83. data/ext/opencv/cvpoint.cpp +234 -0
  84. data/ext/opencv/cvpoint.h +64 -0
  85. data/ext/opencv/cvpoint2d32f.cpp +216 -0
  86. data/ext/opencv/cvpoint2d32f.h +63 -0
  87. data/ext/opencv/cvpoint3d32f.cpp +252 -0
  88. data/ext/opencv/cvpoint3d32f.h +66 -0
  89. data/ext/opencv/cvrect.cpp +338 -0
  90. data/ext/opencv/cvrect.h +79 -0
  91. data/ext/opencv/cvscalar.cpp +241 -0
  92. data/ext/opencv/cvscalar.h +71 -0
  93. data/ext/opencv/cvseq.cpp +663 -0
  94. data/ext/opencv/cvseq.h +75 -0
  95. data/ext/opencv/cvsize.cpp +227 -0
  96. data/ext/opencv/cvsize.h +65 -0
  97. data/ext/opencv/cvsize2d32f.cpp +215 -0
  98. data/ext/opencv/cvsize2d32f.h +64 -0
  99. data/ext/opencv/cvslice.cpp +126 -0
  100. data/ext/opencv/cvslice.h +61 -0
  101. data/ext/opencv/cvsurfparams.cpp +208 -0
  102. data/ext/opencv/cvsurfparams.h +58 -0
  103. data/ext/opencv/cvsurfpoint.cpp +246 -0
  104. data/ext/opencv/cvsurfpoint.h +52 -0
  105. data/ext/opencv/cvtermcriteria.cpp +198 -0
  106. data/ext/opencv/cvtermcriteria.h +71 -0
  107. data/ext/opencv/cvtwopoints.cpp +122 -0
  108. data/ext/opencv/cvtwopoints.h +51 -0
  109. data/ext/opencv/cvutils.cpp +192 -0
  110. data/ext/opencv/cvutils.h +30 -0
  111. data/ext/opencv/cvvideowriter.cpp +142 -0
  112. data/ext/opencv/cvvideowriter.h +43 -0
  113. data/ext/opencv/eigenfaces.cpp +75 -0
  114. data/ext/opencv/eigenfaces.h +30 -0
  115. data/ext/opencv/extconf.rb +77 -0
  116. data/ext/opencv/facerecognizer.cpp +219 -0
  117. data/ext/opencv/facerecognizer.h +46 -0
  118. data/ext/opencv/fisherfaces.cpp +75 -0
  119. data/ext/opencv/fisherfaces.h +30 -0
  120. data/ext/opencv/gui.cpp +71 -0
  121. data/ext/opencv/gui.h +30 -0
  122. data/ext/opencv/iplconvkernel.cpp +198 -0
  123. data/ext/opencv/iplconvkernel.h +71 -0
  124. data/ext/opencv/iplimage.cpp +651 -0
  125. data/ext/opencv/iplimage.h +73 -0
  126. data/ext/opencv/lbph.cpp +78 -0
  127. data/ext/opencv/lbph.h +30 -0
  128. data/ext/opencv/mouseevent.cpp +186 -0
  129. data/ext/opencv/mouseevent.h +56 -0
  130. data/ext/opencv/opencv.cpp +819 -0
  131. data/ext/opencv/opencv.h +408 -0
  132. data/ext/opencv/pointset.cpp +280 -0
  133. data/ext/opencv/pointset.h +68 -0
  134. data/ext/opencv/trackbar.cpp +127 -0
  135. data/ext/opencv/trackbar.h +69 -0
  136. data/ext/opencv/window.cpp +377 -0
  137. data/ext/opencv/window.h +66 -0
  138. data/images/CvMat_sobel.png +0 -0
  139. data/images/CvMat_sub_rect.png +0 -0
  140. data/images/CvSeq_relationmap.png +0 -0
  141. data/lib/opencv.rb +12 -0
  142. data/lib/opencv/psyched_yaml.rb +22 -0
  143. data/lib/opencv/version.rb +3 -0
  144. data/ruby-opencv.gemspec +44 -0
  145. data/test/eigenfaces_save.xml +7524 -0
  146. data/test/fisherfaces_save.xml +7530 -0
  147. data/test/helper.rb +167 -0
  148. data/test/lbph_save.xml +4304 -0
  149. data/test/runner.rb +30 -0
  150. data/test/samples/airplane.jpg +0 -0
  151. data/test/samples/baboon.jpg +0 -0
  152. data/test/samples/baboon200.jpg +0 -0
  153. data/test/samples/baboon200_rotated.jpg +0 -0
  154. data/test/samples/blank0.jpg +0 -0
  155. data/test/samples/blank1.jpg +0 -0
  156. data/test/samples/blank2.jpg +0 -0
  157. data/test/samples/blank3.jpg +0 -0
  158. data/test/samples/blank4.jpg +0 -0
  159. data/test/samples/blank5.jpg +0 -0
  160. data/test/samples/blank6.jpg +0 -0
  161. data/test/samples/blank7.jpg +0 -0
  162. data/test/samples/blank8.jpg +0 -0
  163. data/test/samples/blank9.jpg +0 -0
  164. data/test/samples/cat.jpg +0 -0
  165. data/test/samples/chessboard.jpg +0 -0
  166. data/test/samples/contours.jpg +0 -0
  167. data/test/samples/fruits.jpg +0 -0
  168. data/test/samples/haarcascade_frontalface_alt.xml.gz +0 -0
  169. data/test/samples/inpaint-mask.bmp +0 -0
  170. data/test/samples/lena-256x256.jpg +0 -0
  171. data/test/samples/lena-32x32.jpg +0 -0
  172. data/test/samples/lena-eyes.jpg +0 -0
  173. data/test/samples/lena-inpaint.jpg +0 -0
  174. data/test/samples/lena.jpg +0 -0
  175. data/test/samples/lines.jpg +0 -0
  176. data/test/samples/messy0.jpg +0 -0
  177. data/test/samples/messy1.jpg +0 -0
  178. data/test/samples/movie_sample.avi +0 -0
  179. data/test/samples/one_way_train_0000.jpg +0 -0
  180. data/test/samples/one_way_train_0001.jpg +0 -0
  181. data/test/samples/partially_blank0.jpg +0 -0
  182. data/test/samples/partially_blank1.jpg +0 -0
  183. data/test/samples/smooth0.jpg +0 -0
  184. data/test/samples/smooth1.jpg +0 -0
  185. data/test/samples/smooth2.jpg +0 -0
  186. data/test/samples/smooth3.jpg +0 -0
  187. data/test/samples/smooth4.jpg +0 -0
  188. data/test/samples/smooth5.jpg +0 -0
  189. data/test/samples/smooth6.jpg +0 -0
  190. data/test/samples/str-cv-rotated.jpg +0 -0
  191. data/test/samples/str-cv.jpg +0 -0
  192. data/test/samples/str-ov.jpg +0 -0
  193. data/test/samples/stuff.jpg +0 -0
  194. data/test/test_curve.rb +43 -0
  195. data/test/test_cvavgcomp.rb +24 -0
  196. data/test/test_cvbox2d.rb +76 -0
  197. data/test/test_cvcapture.rb +191 -0
  198. data/test/test_cvchain.rb +108 -0
  199. data/test/test_cvcircle32f.rb +41 -0
  200. data/test/test_cvconnectedcomp.rb +61 -0
  201. data/test/test_cvcontour.rb +171 -0
  202. data/test/test_cvcontourtree.rb +43 -0
  203. data/test/test_cverror.rb +50 -0
  204. data/test/test_cvfeaturetree.rb +65 -0
  205. data/test/test_cvfont.rb +58 -0
  206. data/test/test_cvhaarclassifiercascade.rb +63 -0
  207. data/test/test_cvhistogram.rb +271 -0
  208. data/test/test_cvhumoments.rb +83 -0
  209. data/test/test_cvline.rb +50 -0
  210. data/test/test_cvmat.rb +3036 -0
  211. data/test/test_cvmat_drawing.rb +349 -0
  212. data/test/test_cvmat_dxt.rb +150 -0
  213. data/test/test_cvmat_imageprocessing.rb +2085 -0
  214. data/test/test_cvmoments.rb +180 -0
  215. data/test/test_cvpoint.rb +75 -0
  216. data/test/test_cvpoint2d32f.rb +75 -0
  217. data/test/test_cvpoint3d32f.rb +93 -0
  218. data/test/test_cvrect.rb +144 -0
  219. data/test/test_cvscalar.rb +113 -0
  220. data/test/test_cvseq.rb +311 -0
  221. data/test/test_cvsize.rb +75 -0
  222. data/test/test_cvsize2d32f.rb +75 -0
  223. data/test/test_cvslice.rb +31 -0
  224. data/test/test_cvsurfparams.rb +57 -0
  225. data/test/test_cvsurfpoint.rb +66 -0
  226. data/test/test_cvtermcriteria.rb +56 -0
  227. data/test/test_cvtwopoints.rb +40 -0
  228. data/test/test_cvvideowriter.rb +58 -0
  229. data/test/test_eigenfaces.rb +93 -0
  230. data/test/test_fisherfaces.rb +93 -0
  231. data/test/test_iplconvkernel.rb +54 -0
  232. data/test/test_iplimage.rb +232 -0
  233. data/test/test_lbph.rb +166 -0
  234. data/test/test_mouseevent.rb +17 -0
  235. data/test/test_opencv.rb +360 -0
  236. data/test/test_pointset.rb +128 -0
  237. data/test/test_preliminary.rb +130 -0
  238. data/test/test_trackbar.rb +47 -0
  239. data/test/test_window.rb +115 -0
  240. data/yard_extension.rb +5 -0
  241. metadata +352 -0
@@ -0,0 +1,148 @@
1
+ /************************************************************
2
+
3
+ cvhaarclassifercascade.cpp -
4
+
5
+ $Author: lsxi $
6
+
7
+ Copyright (C) 2005-2007 Masakazu Yonekura
8
+
9
+ ************************************************************/
10
+ #include "cvhaarclassifiercascade.h"
11
+ /*
12
+ * Document-class: OpenCV::CvHaarClassifierCascade
13
+ *
14
+ * Haar Feature-based Cascade Classifier for Object Detection
15
+ */
16
+ __NAMESPACE_BEGIN_OPENCV
17
+ __NAMESPACE_BEGIN_CVHAARCLASSIFERCASCADE
18
+
19
+ VALUE rb_klass;
20
+
21
+ VALUE
22
+ rb_class()
23
+ {
24
+ return rb_klass;
25
+ }
26
+
27
+ VALUE
28
+ rb_allocate(VALUE klass)
29
+ {
30
+ return OPENCV_OBJECT(klass, 0);
31
+ }
32
+
33
+ void
34
+ cvhaarclassifiercascade_free(void* ptr)
35
+ {
36
+ if (ptr) {
37
+ CvHaarClassifierCascade* cascade = (CvHaarClassifierCascade*)ptr;
38
+ cvReleaseHaarClassifierCascade(&cascade);
39
+ }
40
+ }
41
+
42
+ /*
43
+ * Load trained cascade of haar classifers from file.
44
+ *
45
+ * @overload load(filename)
46
+ * @param filename [String] Haar classifer file name
47
+ * @return [CvHaarClassifierCascade] Object detector
48
+ * @scope class
49
+ * @opencv_func cvLoad
50
+ */
51
+ VALUE
52
+ rb_load(VALUE klass, VALUE path)
53
+ {
54
+ CvHaarClassifierCascade *cascade = NULL;
55
+ try {
56
+ cascade = (CvHaarClassifierCascade*)cvLoad(StringValueCStr(path), 0, 0, 0);
57
+ }
58
+ catch (cv::Exception& e) {
59
+ raise_cverror(e);
60
+ }
61
+ if (!CV_IS_HAAR_CLASSIFIER(cascade))
62
+ rb_raise(rb_eArgError, "invalid format haar classifier cascade file.");
63
+ return Data_Wrap_Struct(klass, 0, cvhaarclassifiercascade_free, cascade);
64
+ }
65
+
66
+ /*
67
+ * Detects objects of different sizes in the input image.
68
+ *
69
+ * @overload detect_objects(image, options = nil)
70
+ * @param image [CvMat,IplImage] Matrix of the type CV_8U containing an image where objects are detected.
71
+ * @param options [Hash] Options
72
+ * @option options [Number] :scale_factor
73
+ * Parameter specifying how much the image size is reduced at each image scale.
74
+ * @option options [Number] :storage
75
+ * Memory storage to store the resultant sequence of the object candidate rectangles
76
+ * @option options [Number] :min_neighbors
77
+ * Parameter specifying how many neighbors each candidate rectangle should have to retain it.
78
+ * @option options [CvSize] :min_size
79
+ * Minimum possible object size. Objects smaller than that are ignored.
80
+ * @option options [CvSize] :max_size
81
+ * Maximum possible object size. Objects larger than that are ignored.
82
+ * @return [CvSeq<CvAvgComp>] Detected objects as a list of rectangles
83
+ * @opencv_func cvHaarDetectObjects
84
+ */
85
+ VALUE
86
+ rb_detect_objects(int argc, VALUE *argv, VALUE self)
87
+ {
88
+ VALUE image, options;
89
+ rb_scan_args(argc, argv, "11", &image, &options);
90
+
91
+ double scale_factor;
92
+ int flags, min_neighbors;
93
+ CvSize min_size, max_size;
94
+ VALUE storage_val;
95
+ if (NIL_P(options)) {
96
+ scale_factor = 1.1;
97
+ flags = 0;
98
+ min_neighbors = 3;
99
+ min_size = max_size = cvSize(0, 0);
100
+ storage_val = cCvMemStorage::new_object();
101
+ }
102
+ else {
103
+ scale_factor = IF_DBL(LOOKUP_HASH(options, "scale_factor"), 1.1);
104
+ flags = IF_INT(LOOKUP_HASH(options, "flags"), 0);
105
+ min_neighbors = IF_INT(LOOKUP_HASH(options, "min_neighbors"), 3);
106
+ VALUE min_size_val = LOOKUP_HASH(options, "min_size");
107
+ min_size = NIL_P(min_size_val) ? cvSize(0, 0) : VALUE_TO_CVSIZE(min_size_val);
108
+ VALUE max_size_val = LOOKUP_HASH(options, "max_size");
109
+ max_size = NIL_P(max_size_val) ? cvSize(0, 0) : VALUE_TO_CVSIZE(max_size_val);
110
+ storage_val = CHECK_CVMEMSTORAGE(LOOKUP_HASH(options, "storage"));
111
+ }
112
+
113
+ VALUE result = Qnil;
114
+ try {
115
+ CvSeq *seq = cvHaarDetectObjects(CVARR_WITH_CHECK(image), CVHAARCLASSIFIERCASCADE(self), CVMEMSTORAGE(storage_val),
116
+ scale_factor, min_neighbors, flags, min_size, max_size);
117
+ result = cCvSeq::new_sequence(cCvSeq::rb_class(), seq, cCvAvgComp::rb_class(), storage_val);
118
+ if (rb_block_given_p()) {
119
+ for(int i = 0; i < seq->total; ++i)
120
+ rb_yield(REFER_OBJECT(cCvAvgComp::rb_class(), cvGetSeqElem(seq, i), storage_val));
121
+ }
122
+ }
123
+ catch (cv::Exception& e) {
124
+ raise_cverror(e);
125
+ }
126
+ return result;
127
+ }
128
+
129
+ void
130
+ init_ruby_class()
131
+ {
132
+ #if 0
133
+ // For documentation using YARD
134
+ VALUE opencv = rb_define_module("OpenCV");
135
+ #endif
136
+
137
+ if (rb_klass)
138
+ return;
139
+
140
+ VALUE opencv = rb_module_opencv();
141
+ rb_klass = rb_define_class_under(opencv, "CvHaarClassifierCascade", rb_cObject);
142
+ rb_define_alloc_func(rb_klass, rb_allocate);
143
+ rb_define_singleton_method(rb_klass, "load", RUBY_METHOD_FUNC(rb_load), 1);
144
+ rb_define_method(rb_klass, "detect_objects", RUBY_METHOD_FUNC(rb_detect_objects), -1);
145
+ }
146
+
147
+ __NAMESPACE_END_CVHAARCLASSIFERCASCADE
148
+ __NAMESPACE_END_OPENCV
@@ -0,0 +1,39 @@
1
+ /************************************************************
2
+
3
+ cvhaarclassifiercascade.h -
4
+
5
+ $Author: lsxi $
6
+
7
+ Copyright (C) 2005-2006 Masakazu Yonekura
8
+
9
+ ************************************************************/
10
+ #ifndef RUBY_OPENCV_CVHAARCLASSIFIERCASCADE_H
11
+ #define RUBY_OPENCV_CVHAARCLASSIFIERCASCADE_H
12
+
13
+ #define __NAMESPACE_BEGIN_CVHAARCLASSIFERCASCADE namespace cCvHaarClassifierCascade {
14
+ #define __NAMESPACE_END_CVHAARCLASSIFERCASCADE }
15
+
16
+ #include "opencv.h"
17
+
18
+ __NAMESPACE_BEGIN_OPENCV
19
+ __NAMESPACE_BEGIN_CVHAARCLASSIFERCASCADE
20
+
21
+ VALUE rb_class();
22
+
23
+ void init_ruby_class();
24
+
25
+ VALUE rb_allocate(VALUE klass);
26
+
27
+ VALUE rb_load(VALUE klass, VALUE path);
28
+ VALUE rb_detect_objects(int argc, VALUE *argv, VALUE self);
29
+
30
+ __NAMESPACE_END_CVHAARCLASSIFERCASCADE
31
+ inline CvHaarClassifierCascade*
32
+ CVHAARCLASSIFIERCASCADE(VALUE object) {
33
+ CvHaarClassifierCascade *ptr;
34
+ Data_Get_Struct(object, CvHaarClassifierCascade, ptr);
35
+ return ptr;
36
+ }
37
+ __NAMESPACE_END_OPENCV
38
+
39
+ #endif // RUBY_OPENCV_CVHAARCLASSIFIERCASCADE_H
@@ -0,0 +1,717 @@
1
+ /************************************************************
2
+
3
+ cvhistogram.cpp -
4
+
5
+ $Author: lsxi $
6
+
7
+ Copyright (C) 2005-2008 Masakazu Yonekura
8
+
9
+ ************************************************************/
10
+ #include "cvhistogram.h"
11
+ /*
12
+ * Document-class: OpenCV::CvHistogram
13
+ *
14
+ * Multi-dimensional histogram.
15
+ */
16
+ __NAMESPACE_BEGIN_OPENCV
17
+ __NAMESPACE_BEGIN_CVHISTOGRAM
18
+
19
+ VALUE rb_klass;
20
+
21
+ VALUE
22
+ rb_class()
23
+ {
24
+ return rb_klass;
25
+ }
26
+
27
+ void
28
+ release_hist(void* ptr)
29
+ {
30
+ if (ptr) {
31
+ try {
32
+ cvReleaseHist((CvHistogram**)&ptr);
33
+ }
34
+ catch (cv::Exception& e) {
35
+ raise_cverror(e);
36
+ }
37
+ }
38
+ }
39
+
40
+ VALUE
41
+ rb_allocate(VALUE klass)
42
+ {
43
+ CvHistogram* ptr = NULL;
44
+ return Data_Wrap_Struct(klass, 0, release_hist, ptr);
45
+ }
46
+
47
+ float*
48
+ ary2fltptr(VALUE ary, float* buff)
49
+ {
50
+ Check_Type(ary, T_ARRAY);
51
+ int size = RARRAY_LEN(ary);
52
+ VALUE* ary_ptr = RARRAY_PTR(ary);
53
+ for (int i = 0; i < size; ++i) {
54
+ buff[i] = NUM2DBL(ary_ptr[i]);
55
+ }
56
+ return buff;
57
+ }
58
+
59
+ int*
60
+ ary2intptr(VALUE ary, int* buff)
61
+ {
62
+ Check_Type(ary, T_ARRAY);
63
+ int size = RARRAY_LEN(ary);
64
+ VALUE* ary_ptr = RARRAY_PTR(ary);
65
+ for (int i = 0; i < size; ++i) {
66
+ buff[i] = NUM2INT(ary_ptr[i]);
67
+ }
68
+ return buff;
69
+ }
70
+
71
+ /*
72
+ * Creates a histogram
73
+ * @overload new(dims, sizes, type, ranges=nil, uniform=true)
74
+ * @param dims [Integer] Number of histogram dimensions
75
+ * @param sizes [Array<Integer>] Array of the histogram dimension sizes
76
+ * @param type [Integer]
77
+ * Histogram representation format. CV_HIST_ARRAY means that the histogram data is represented
78
+ * as a multi-dimensional dense array CvMatND. CV_HIST_SPARSE means that histogram data is
79
+ * represented as a multi-dimensional sparse array CvSparseMat.
80
+ * @param ranges [Array<Integer>]
81
+ * Array of ranges for the histogram bins. Its meaning depends on the uniform parameter value.
82
+ * The ranges are used when the histogram is calculated or backprojected to determine which
83
+ * histogram bin corresponds to which value/tuple of values from the input image(s).
84
+ * @param uniform [Boolean] Uniformity flag.
85
+ * @return [CvHistogram] Histogram
86
+ * @opencv_func cvCreateHist
87
+ */
88
+ VALUE
89
+ rb_initialize(int argc, VALUE *argv, VALUE self)
90
+ {
91
+ VALUE _dims, _sizes, _type, _ranges, _uniform;
92
+ int uniform;
93
+ int* sizes;
94
+ float** ranges = NULL;
95
+
96
+ rb_scan_args(argc, argv, "32", &_dims, &_sizes, &_type, &_ranges, &_uniform);
97
+ int sizes_len = RARRAY_LEN(_sizes);
98
+ sizes = RB_ALLOC_N(int, sizes_len);
99
+
100
+ if (NIL_P(_ranges)) {
101
+ sizes = ary2intptr(_sizes, sizes);
102
+ ranges = NULL;
103
+ }
104
+ else {
105
+ ranges = RB_ALLOC_N(float*, sizes_len);
106
+ VALUE* range_ptr = RARRAY_PTR(_ranges);
107
+ int i;
108
+ for (i = 0; i < sizes_len; i++) {
109
+ sizes[i] = NUM2INT(RARRAY_PTR(_sizes)[i]);
110
+ ranges[i] = ary2fltptr(range_ptr[i], RB_ALLOC_N(float, 2));
111
+ }
112
+ }
113
+ uniform = TRUE_OR_FALSE(_uniform, 1);
114
+
115
+ try {
116
+ DATA_PTR(self) = cvCreateHist(NUM2INT(_dims), sizes, NUM2INT(_type), ranges, uniform);
117
+ }
118
+ catch (cv::Exception& e) {
119
+ raise_cverror(e);
120
+ }
121
+
122
+ return self;
123
+ }
124
+
125
+ /*
126
+ * Returns <tt>self</tt> is uniform histogram or not
127
+ * @overload is_uniform?
128
+ * @return [Boolean] Uniform or not
129
+ * @opencv_func CV_IS_UNIFORM_HIST
130
+ */
131
+ VALUE
132
+ rb_is_uniform(VALUE self)
133
+ {
134
+ return CV_IS_UNIFORM_HIST(CVHISTOGRAM(self)) ? Qtrue : Qfalse;
135
+ }
136
+
137
+ /*
138
+ * Returns <tt>self</tt> is sparse histogram or not
139
+ * @overload is_sparse?
140
+ * @return [Boolean] Sparse or not
141
+ * @opencv_func CV_IS_SPARSE_HIST
142
+ */
143
+ VALUE
144
+ rb_is_sparse(VALUE self)
145
+ {
146
+ return CV_IS_SPARSE_HIST(CVHISTOGRAM(self)) ? Qtrue : Qfalse;
147
+ }
148
+
149
+ /*
150
+ * Returns <tt>self</tt> has range or not
151
+ * @overload has_range?
152
+ * @return [Boolean] Has range or not
153
+ * @opencv_func CV_HIST_HAS_RANGES
154
+ */
155
+ VALUE
156
+ rb_has_range(VALUE self)
157
+ {
158
+ return CV_HIST_HAS_RANGES(CVHISTOGRAM(self)) ? Qtrue : Qfalse;
159
+ }
160
+
161
+ /*
162
+ * Calculates a histogram of a set of arrays.
163
+ * @overload calc_hist(images, accumulate=nil, mask=nil)
164
+ * @param images [Array<IplImage>]
165
+ * Source arrays. They all should have the same depth, CV_8U or CV_32F, and the same size.
166
+ * Each of them can have an arbitrary number of channels.
167
+ * @param accumulate [Boolean]
168
+ * Accumulation flag. If it is set, the histogram is not cleared in the beginning when it is allocated.
169
+ * This feature enables you to compute a single histogram from several sets of arrays,
170
+ * or to update the histogram in time.
171
+ * @param mask [CvMat]
172
+ * Optional mask. If the matrix is not empty, it must be an 8-bit array of the same size as images[i].
173
+ * The non-zero mask elements mark the array elements counted in the histogram.
174
+ * @return [CvHistogram] Histogram of a set of arrays
175
+ * @opencv_func cvCalcHist
176
+ */
177
+ VALUE
178
+ rb_calc_hist(int argc, VALUE* argv, VALUE self)
179
+ {
180
+ return rb_calc_hist_bang(argc, argv, rb_copy_hist(self));
181
+ }
182
+
183
+ /*
184
+ * Calculates a histogram of a set of arrays.
185
+ * @overload calc_hist!(images, accumulate=nil, mask=nil)
186
+ * @see #calc_hist
187
+ * @opencv_func cvCalcHist
188
+ */
189
+ VALUE
190
+ rb_calc_hist_bang(int argc, VALUE* argv, VALUE self)
191
+ {
192
+ VALUE images, accumulate, mask;
193
+ rb_scan_args(argc, argv, "12", &images, &accumulate, &mask);
194
+ Check_Type(images, T_ARRAY);
195
+ int num_images = RARRAY_LEN(images);
196
+ if (num_images == 0) {
197
+ rb_raise(rb_eArgError, "One or more arrays are required.");
198
+ }
199
+ IplImage** img = RB_ALLOC_N(IplImage*, num_images);
200
+ VALUE* images_ptr = RARRAY_PTR(images);
201
+ for (int i = 0; i < num_images; i++) {
202
+ img[i] = IPLIMAGE_WITH_CHECK(images_ptr[i]);
203
+ }
204
+ CvMat* m = NIL_P(mask) ? NULL : CVMAT_WITH_CHECK(mask);
205
+ try {
206
+ cvCalcHist(img, CVHISTOGRAM(self), TRUE_OR_FALSE(accumulate, 0), m);
207
+ }
208
+ catch (cv::Exception& e) {
209
+ raise_cverror(e);
210
+ }
211
+ return self;
212
+ }
213
+
214
+ /*
215
+ * Queries the value of the histogram bin.
216
+ * @overload [](idx0)
217
+ * @overload [](idx0, idx1)
218
+ * @overload [](idx0, idx1, idx2)
219
+ * @overload [](idx0, idx1, idx2, idx3, ...)
220
+ * @param idx* [Integer] *-th index
221
+ * @return [Number] The value of the specified bin of the 1D, 2D, 3D, or N-D histogram.
222
+ * @opencv_func cvQueryHistValue_1D
223
+ * @opencv_func cvQueryHistValue_2D
224
+ * @opencv_func cvQueryHistValue_3D
225
+ * @opencv_func cvQueryHistValue_nD
226
+ */
227
+ VALUE
228
+ rb_aref(VALUE self, VALUE args)
229
+ {
230
+ int num_idx = RARRAY_LEN(args);
231
+ int* idx = RB_ALLOC_N(int, num_idx);
232
+ VALUE* args_ptr = RARRAY_PTR(args);
233
+ for (int i = 0; i < num_idx; i++) {
234
+ idx[i] = NUM2INT(args_ptr[i]);
235
+ }
236
+
237
+ float value = 0.0;
238
+ CvHistogram* self_ptr = CVHISTOGRAM(self);
239
+ try {
240
+ switch (num_idx) {
241
+ case 1:
242
+ value = cvQueryHistValue_1D(self_ptr, idx[0]);
243
+ break;
244
+ case 2:
245
+ value = cvQueryHistValue_2D(self_ptr, idx[0], idx[1]);
246
+ break;
247
+ case 3:
248
+ value = cvQueryHistValue_3D(self_ptr, idx[0], idx[1], idx[2]);
249
+ break;
250
+ default:
251
+ value = cvQueryHistValue_nD(self_ptr, idx);
252
+ break;
253
+ }
254
+ }
255
+ catch (cv::Exception& e) {
256
+ raise_cverror(e);
257
+ }
258
+
259
+ return rb_float_new((double)value);
260
+ }
261
+
262
+ /*
263
+ * Finds the minimum and maximum histogram bins.
264
+ * @overload min_max_value
265
+ * @return [Array]
266
+ * [min_value, max_value, min_idx, max_idx]: Array of the minimum / maximum value of the histogram
267
+ * and their coordinates.
268
+ * - min_value: The minimum value of the histogram.
269
+ * - max_value: The maximum value of the histogram.
270
+ * - min_idx: The array of coordinates for the minimum.
271
+ * - max_idx: The array of coordinates for the maximum.
272
+ * @opencv_func cvGetMinMaxHistValue
273
+ */
274
+ VALUE
275
+ rb_min_max_value(VALUE self)
276
+ {
277
+ CvHistogram* self_ptr = CVHISTOGRAM(self);
278
+ int dims = 0;
279
+ float min_value = 0.0, max_value = 0.0;
280
+ int *min_idx = NULL;
281
+ int *max_idx = NULL;
282
+ try {
283
+ dims = cvGetDims(self_ptr->bins, NULL);
284
+ min_idx = RB_ALLOC_N(int, dims);
285
+ max_idx = RB_ALLOC_N(int, dims);
286
+ cvGetMinMaxHistValue(CVHISTOGRAM(self), &min_value, &max_value, min_idx, max_idx);
287
+ }
288
+ catch (cv::Exception& e) {
289
+ raise_cverror(e);
290
+ }
291
+
292
+ VALUE _min_idx = rb_ary_new2(dims);
293
+ VALUE _max_idx = rb_ary_new2(dims);
294
+ for (int i = 0; i < dims; i++) {
295
+ rb_ary_store(_min_idx, i, INT2NUM(min_idx[i]));
296
+ rb_ary_store(_max_idx, i, INT2NUM(max_idx[i]));
297
+ }
298
+
299
+ return rb_ary_new3(4, rb_float_new((double)min_value), rb_float_new((double)max_value),
300
+ _min_idx, _max_idx);
301
+ }
302
+
303
+ /*
304
+ * Returns number of array dimensions
305
+ * @overload [](idx0, idx1, ...)
306
+ * @param idx* [Integer] *-th index
307
+ * @return [Array<Integer, Array<Integer>>]
308
+ * [dims, sizes]: Number of array dimensions and its sizes.
309
+ * - dims (Integer): Number of array dimensions
310
+ * - sizes (Array<Integer>): Vector of the array dimension sizes.
311
+ * For 2D arrays the number of rows (height) goes first,
312
+ * number of columns (width) next.
313
+ * @opencv_func cvGetDims
314
+ */
315
+ VALUE
316
+ rb_dims(VALUE self)
317
+ {
318
+ VALUE _sizes = Qnil;
319
+ int size[CV_MAX_DIM];
320
+ int dims = 0;
321
+ try {
322
+ dims = cvGetDims(CVHISTOGRAM(self)->bins, size);
323
+ _sizes = rb_ary_new2(dims);
324
+ for (int i = 0; i < dims; ++i) {
325
+ rb_ary_store(_sizes, i, INT2NUM(size[i]));
326
+ }
327
+ }
328
+ catch (cv::Exception& e) {
329
+ raise_cverror(e);
330
+ }
331
+ return rb_assoc_new(INT2NUM(dims), _sizes);
332
+ }
333
+
334
+ /*
335
+ * Clones histogram
336
+ * @overload copy_hist
337
+ * @return [CvHistogram] Copy of the histogram
338
+ * @opencv_func cvCopyHist
339
+ */
340
+ VALUE
341
+ rb_copy_hist(VALUE self)
342
+ {
343
+ CvHistogram* hist = NULL;
344
+ try {
345
+ cvCopyHist(CVHISTOGRAM(self), &hist);
346
+ }
347
+ catch (cv::Exception& e) {
348
+ raise_cverror(e);
349
+ }
350
+ return Data_Wrap_Struct(rb_klass, 0, release_hist, hist);
351
+ }
352
+
353
+ /*
354
+ * Sets all histogram bins to 0 in case of dense histogram
355
+ * and removes all histogram bins in case of sparse array.
356
+ * @overload clear_hist
357
+ * @return [CvHistogram] Cleared histogram
358
+ * @opencv_func cvClearHist
359
+ */
360
+ VALUE
361
+ rb_clear_hist(VALUE self)
362
+ {
363
+ return rb_clear_hist_bang(rb_copy_hist(self));
364
+ }
365
+
366
+ /*
367
+ * Sets all histogram bins to 0 in case of dense histogram
368
+ * and removes all histogram bins in case of sparse array.
369
+ * This method changes <tt>self</tt>.
370
+ * @overload clear_hist!
371
+ * @see #clear_hist
372
+ * @return [CvHistogram] Cleared histogram
373
+ * @opencv_func cvClearHist
374
+ */
375
+ VALUE
376
+ rb_clear_hist_bang(VALUE self)
377
+ {
378
+ try {
379
+ cvClearHist(CVHISTOGRAM(self));
380
+ }
381
+ catch (cv::Exception& e) {
382
+ raise_cverror(e);
383
+ }
384
+ return self;
385
+ }
386
+
387
+ /*
388
+ * Returns normalized the histogram bins by scaling them,
389
+ * such that the sum of the bins becomes equal to <tt>factor</tt>.
390
+ * @overload normalize(factor)
391
+ * @param factor [Number] Normalization factor. The sum of the bins becomes equal to this value.
392
+ * @return [CvHistogram] Normalized histogram
393
+ * @opencv_func cvNormalizeHist
394
+ */
395
+ VALUE
396
+ rb_normalize_hist(VALUE self, VALUE factor)
397
+ {
398
+ return rb_normalize_hist_bang(rb_copy_hist(self), factor);
399
+ }
400
+
401
+ /*
402
+ * Returns normalized the histogram bins by scaling them,
403
+ * such that the sum of the bins becomes equal to <tt>factor</tt>.
404
+ * This method changes <tt>self</tt>.
405
+ * @overload normalize!(factor)
406
+ * @param factor [Number] Normalization factor. The sum of the bins becomes equal to this value.
407
+ * @return [CvHistogram] Normalized histogram
408
+ * @see #normalize
409
+ * @opencv_func cvNormalizeHist
410
+ */
411
+ VALUE
412
+ rb_normalize_hist_bang(VALUE self, VALUE factor)
413
+ {
414
+ try {
415
+ cvNormalizeHist(CVHISTOGRAM(self), NUM2DBL(factor));
416
+ }
417
+ catch (cv::Exception& e) {
418
+ raise_cverror(e);
419
+ }
420
+ return self;
421
+ }
422
+
423
+ /*
424
+ * Returns cleared histogram bins that are below the specified threshold.
425
+ * @overload thresh_hist(threshold)
426
+ * @param threshold [Number] Threshold value
427
+ * @return [CvHistogram] Cleared histogram
428
+ * @opencv_func cvThreshHist
429
+ */
430
+ VALUE
431
+ rb_thresh_hist(VALUE self, VALUE threshold)
432
+ {
433
+ return rb_thresh_hist_bang(rb_copy_hist(self), threshold);
434
+ }
435
+
436
+ /*
437
+ * Cleares histogram bins that are below the specified threshold.
438
+ * This method changes <tt>self</tt>.
439
+ * @overload thresh_hist!(threshold)
440
+ * @param threshold [Number] Threshold value
441
+ * @return [CvHistogram] Cleared histogram
442
+ * @see #thresh_hist
443
+ * @opencv_func cvThreshHist
444
+ */
445
+ VALUE
446
+ rb_thresh_hist_bang(VALUE self, VALUE threshold)
447
+ {
448
+ try {
449
+ cvThreshHist(CVHISTOGRAM(self), NUM2DBL(threshold));
450
+ }
451
+ catch (cv::Exception& e) {
452
+ raise_cverror(e);
453
+ }
454
+ return self;
455
+ }
456
+
457
+ /*
458
+ * Sets the bounds of the histogram bins.
459
+ * @overload set_hist_bin_ranges(ranges, uniform=true)
460
+ * @param ranges [Array<Number>]
461
+ * Array of ranges for the histogram bins. Its meaning depends on the uniform parameter value.
462
+ * The ranges are used when the histogram is calculated or backprojected to determine
463
+ * which histogram bin corresponds to which value/tuple of values from the input image(s).
464
+ * @param uniform [Boolean]
465
+ * Uniformity flag.
466
+ * @return [CvHistogram]
467
+ * Histogram
468
+ * @opencv_func cvSetHistBinRanges
469
+ */
470
+ VALUE
471
+ rb_set_hist_bin_ranges(int argc, VALUE* argv, VALUE self)
472
+ {
473
+ return rb_set_hist_bin_ranges_bang(argc, argv, rb_copy_hist(self));
474
+ }
475
+
476
+ /*
477
+ * Sets the bounds of the histogram bins. This method changes <tt>self</tt>.
478
+ * @overload set_hist_bin_ranges!(ranges, uniform=true)
479
+ * @param ranges [Array<Number>]
480
+ * Array of ranges for the histogram bins. Its meaning depends on the uniform parameter value.
481
+ * The ranges are used when the histogram is calculated or backprojected to determine
482
+ * which histogram bin corresponds to which value/tuple of values from the input image(s).
483
+ * @param uniform [Boolean]
484
+ * Uniformity flag.
485
+ * @return [CvHistogram]
486
+ * Histogram
487
+ * @see #set_hist_bin_ranges
488
+ * @opencv_func cvSetHistBinRanges
489
+ */
490
+ VALUE
491
+ rb_set_hist_bin_ranges_bang(int argc, VALUE* argv, VALUE self)
492
+ {
493
+ VALUE _ranges, _uniform;
494
+ rb_scan_args(argc, argv, "11", &_ranges, &_uniform);
495
+ Check_Type(_ranges, T_ARRAY);
496
+
497
+ int ranges_size = RARRAY_LEN(_ranges);
498
+ float** ranges = RB_ALLOC_N(float*, ranges_size);
499
+ VALUE* range_ptr = RARRAY_PTR(_ranges);
500
+ for (int i = 0; i < ranges_size; ++i) {
501
+ ranges[i] = ary2fltptr(range_ptr[i], RB_ALLOC_N(float, 2));
502
+ }
503
+ int uniform = TRUE_OR_FALSE(_uniform, 1);
504
+
505
+ try {
506
+ cvSetHistBinRanges(CVHISTOGRAM(self), ranges, uniform);
507
+ }
508
+ catch (cv::Exception& e) {
509
+ raise_cverror(e);
510
+ }
511
+
512
+ return self;
513
+ }
514
+
515
+ /*
516
+ * Calculates the back projection of a histogram.
517
+ * @overload calc_back_project(images)
518
+ * @param images [Array<IplImage>]
519
+ * Source arrays. They all should have the same depth, CV_8U or CV_32F, and the same size.
520
+ * Each of them can have an arbitrary number of channels.
521
+ * @return [CvMat,IplImage]
522
+ * Destination back projection array that is a single-channel array of the same size and depth
523
+ * as the first element of <tt>images</tt>
524
+ * @opencv_func cvCalcBackProject
525
+ */
526
+ VALUE
527
+ rb_calc_back_project(VALUE self, VALUE image)
528
+ {
529
+ Check_Type(image, T_ARRAY);
530
+ int num_images = RARRAY_LEN(image);
531
+ if (num_images == 0) {
532
+ return Qnil;
533
+ }
534
+
535
+ IplImage** img = RB_ALLOC_N(IplImage*, num_images);
536
+ VALUE* image_ptr = RARRAY_PTR(image);
537
+ for (int i = 0; i < num_images; ++i) {
538
+ img[i] = IPLIMAGE_WITH_CHECK(image_ptr[i]);
539
+ }
540
+
541
+ CvSize size;
542
+ size.width = img[0]->width;
543
+ size.height = img[0]->height;
544
+ VALUE back_project = cCvMat::new_mat_kind_object(size, image_ptr[0]);
545
+ try {
546
+ cvCalcBackProject(img, CVARR(back_project), CVHISTOGRAM(self));
547
+ }
548
+ catch (cv::Exception& e) {
549
+ raise_cverror(e);
550
+ }
551
+
552
+ return back_project;
553
+ }
554
+
555
+ /*
556
+ * Locates a template within an image by using a histogram comparison.
557
+ * @overload calc_back_project_patch(images, patch_size, method, factor)
558
+ * @param images [Array<IplImage>] Source arrays.
559
+ * @param pach_size [CvSize] Size of the patch slid though the source image.
560
+ * @param method [Integer]
561
+ * Comparison method that could be one of the following:
562
+ * - <tt>CV_COMP_CORREL</tt>: Correlation
563
+ * - <tt>CV_COMP_CHISQR</tt>: Chi-Square
564
+ * - <tt>CV_COMP_INTERSECT</tt>: Intersection
565
+ * - <tt>CV_COMP_BHATTACHARYYA</tt>: Bhattacharyya distance
566
+ * - <tt>CV_COMP_HELLINGER</tt>: Synonym for <tt>CV_COMP_BHATTACHARYYA</tt>
567
+ * @param factor [Number]
568
+ * Normalization factor for histograms that affects the normalization scale
569
+ * of the destination image. Pass 1 if not sure.
570
+ * @return [CvMat,IplImage] Destination image.
571
+ * @opencv_func cvCalcBackProject
572
+ */
573
+ VALUE
574
+ rb_calc_back_project_patch(VALUE self, VALUE image, VALUE patch_size, VALUE method, VALUE factor)
575
+ {
576
+ Check_Type(image, T_ARRAY);
577
+ int num_images = RARRAY_LEN(image);
578
+ if (num_images == 0) {
579
+ return Qnil;
580
+ }
581
+
582
+ IplImage** img = RB_ALLOC_N(IplImage*, num_images);
583
+ VALUE* image_ptr = RARRAY_PTR(image);
584
+ for (int i = 0; i < num_images; ++i) {
585
+ img[i] = IPLIMAGE_WITH_CHECK(image_ptr[i]);
586
+ }
587
+
588
+ CvSize patchsize = VALUE_TO_CVSIZE(patch_size);
589
+ CvSize dst_size;
590
+ dst_size.width = img[0]->width - patchsize.width + 1;
591
+ dst_size.height = img[0]->height - patchsize.height + 1;
592
+
593
+ VALUE dst = cCvMat::new_mat_kind_object(dst_size, image_ptr[0], CV_32F, 1);
594
+ try {
595
+ cvCalcBackProjectPatch(img, CVARR(dst), patchsize, CVHISTOGRAM(self),
596
+ NUM2INT(method), NUM2DBL(factor));
597
+ }
598
+ catch (cv::Exception& e) {
599
+ raise_cverror(e);
600
+ }
601
+
602
+ return dst;
603
+ }
604
+
605
+ /*
606
+ * Compares two histograms.
607
+ * @overload compare_hist(hist1, hist2, method)
608
+ * @param hist1 [CvHistogram] First compared histogram.
609
+ * @param hist2 [CvHistogram] Second compared histogram of the same size as <tt>hist1</tt>.
610
+ * @param method [Integer]
611
+ * Comparison method that could be one of the following:
612
+ * - <tt>CV_COMP_CORREL</tt>: Correlation
613
+ * - <tt>CV_COMP_CHISQR</tt>: Chi-Square
614
+ * - <tt>CV_COMP_INTERSECT</tt>: Intersection
615
+ * - <tt>CV_COMP_BHATTACHARYYA</tt>: Bhattacharyya distance
616
+ * - <tt>CV_COMP_HELLINGER</tt>: Synonym for <tt>CV_COMP_BHATTACHARYYA</tt>
617
+ * @return [Number] Distance of the two histograms.
618
+ * @scope class
619
+ * @opencv_func cvCompareHist
620
+ */
621
+ VALUE
622
+ rb_compare_hist(VALUE self, VALUE hist1, VALUE hist2, VALUE method)
623
+ {
624
+ double result = 0;
625
+ try {
626
+ result = cvCompareHist(CVHISTOGRAM_WITH_CHECK(hist1), CVHISTOGRAM_WITH_CHECK(hist2),
627
+ NUM2INT(method));
628
+ }
629
+ catch (cv::Exception& e) {
630
+ raise_cverror(e);
631
+ }
632
+
633
+ return rb_float_new(result);
634
+ }
635
+
636
+ /*
637
+ * Divides one histogram by another.
638
+ * @overload calc_prob_density(hist1, hist2, scale=255)
639
+ * @param hist1 [CvHistogram] First histogram (the divisor).
640
+ * @param hist2 [CvHistogram] Second histogram.
641
+ * @param scale [Number] Scale factor for the destination histogram.
642
+ * @return [CvHistogram] Destination histogram.
643
+ * @opencv_func cvCalcProbDensity
644
+ */
645
+ VALUE
646
+ rb_calc_prob_density(int argc, VALUE* argv, VALUE self)
647
+ {
648
+ VALUE hist1, hist2, scale;
649
+ rb_scan_args(argc, argv, "21", &hist1, &hist2, &scale);
650
+ double s = NIL_P(scale) ? 255 : NUM2DBL(scale);
651
+
652
+ CvHistogram* hist1_ptr = CVHISTOGRAM_WITH_CHECK(hist1);
653
+ VALUE dst_hist = rb_allocate(rb_klass);
654
+ try {
655
+ cvCopyHist(hist1_ptr, (CvHistogram**)&(DATA_PTR(dst_hist)));
656
+ cvCalcProbDensity(hist1_ptr, CVHISTOGRAM_WITH_CHECK(hist2), CVHISTOGRAM(dst_hist), s);
657
+ }
658
+ catch (cv::Exception& e) {
659
+ raise_cverror(e);
660
+ }
661
+
662
+ return dst_hist;
663
+ }
664
+
665
+ void
666
+ init_ruby_class()
667
+ {
668
+ #if 0
669
+ // For documentation using YARD
670
+ VALUE opencv = rb_define_module("OpenCV");
671
+ #endif
672
+
673
+ if (rb_klass)
674
+ return;
675
+
676
+ VALUE opencv = rb_module_opencv();
677
+ rb_klass = rb_define_class_under(opencv, "CvHistogram", rb_cObject);
678
+ rb_define_alloc_func(rb_klass, rb_allocate);
679
+ rb_define_method(rb_klass, "initialize", RUBY_METHOD_FUNC(rb_initialize), -1);
680
+ rb_define_method(rb_klass, "is_uniform?", RUBY_METHOD_FUNC(rb_is_uniform), 0);
681
+ rb_define_method(rb_klass, "is_sparse?", RUBY_METHOD_FUNC(rb_is_sparse), 0);
682
+ rb_define_method(rb_klass, "has_range?", RUBY_METHOD_FUNC(rb_has_range), 0);
683
+ rb_define_method(rb_klass, "dims", RUBY_METHOD_FUNC(rb_dims), 0);
684
+ rb_define_method(rb_klass, "calc_hist", RUBY_METHOD_FUNC(rb_calc_hist), -1);
685
+ rb_define_method(rb_klass, "calc_hist!", RUBY_METHOD_FUNC(rb_calc_hist_bang), -1);
686
+ rb_define_method(rb_klass, "[]", RUBY_METHOD_FUNC(rb_aref), -2);
687
+ rb_define_alias(rb_klass, "query_hist_value", "[]");
688
+ rb_define_method(rb_klass, "min_max_value", RUBY_METHOD_FUNC(rb_min_max_value), 0);
689
+ rb_define_method(rb_klass, "copy_hist", RUBY_METHOD_FUNC(rb_copy_hist), 0);
690
+
691
+ rb_define_method(rb_klass, "clear_hist", RUBY_METHOD_FUNC(rb_clear_hist), 0);
692
+ rb_define_alias(rb_klass, "clear", "clear_hist");
693
+ rb_define_method(rb_klass, "clear_hist!", RUBY_METHOD_FUNC(rb_clear_hist_bang), 0);
694
+ rb_define_alias(rb_klass, "clear!", "clear_hist!");
695
+
696
+ rb_define_method(rb_klass, "normalize_hist", RUBY_METHOD_FUNC(rb_normalize_hist), 1);
697
+ rb_define_alias(rb_klass, "normalize", "normalize_hist");
698
+ rb_define_method(rb_klass, "normalize_hist!", RUBY_METHOD_FUNC(rb_normalize_hist_bang), 1);
699
+ rb_define_alias(rb_klass, "normalize!", "normalize_hist!");
700
+
701
+ rb_define_method(rb_klass, "thresh_hist", RUBY_METHOD_FUNC(rb_thresh_hist), 1);
702
+ rb_define_alias(rb_klass, "thresh", "thresh_hist");
703
+ rb_define_method(rb_klass, "thresh_hist!", RUBY_METHOD_FUNC(rb_thresh_hist_bang), 1);
704
+ rb_define_alias(rb_klass, "thresh!", "thresh_hist!");
705
+
706
+ rb_define_method(rb_klass, "set_hist_bin_ranges", RUBY_METHOD_FUNC(rb_set_hist_bin_ranges), -1);
707
+ rb_define_method(rb_klass, "set_hist_bin_ranges!", RUBY_METHOD_FUNC(rb_set_hist_bin_ranges_bang), -1);
708
+
709
+ rb_define_method(rb_klass, "calc_back_project", RUBY_METHOD_FUNC(rb_calc_back_project), 1);
710
+ rb_define_method(rb_klass, "calc_back_project_patch", RUBY_METHOD_FUNC(rb_calc_back_project_patch), 4);
711
+
712
+ rb_define_singleton_method(rb_klass, "calc_prob_density", RUBY_METHOD_FUNC(rb_calc_prob_density), -1);
713
+ rb_define_singleton_method(rb_klass, "compare_hist", RUBY_METHOD_FUNC(rb_compare_hist), 3);
714
+ }
715
+
716
+ __NAMESPACE_END_CVHISTOGRAM
717
+ __NAMESPACE_END_OPENCV