bonanza-ruby-opencv 0.0.13.20140330211753

Sign up to get free protection for your applications and to get access to all the features.
Files changed (240) 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 +98 -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 +607 -0
  50. data/ext/opencv/cvcapture.h +72 -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 +332 -0
  58. data/ext/opencv/cvcontour.h +48 -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 +715 -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 +6829 -0
  78. data/ext/opencv/cvmat.h +323 -0
  79. data/ext/opencv/cvmemstorage.cpp +73 -0
  80. data/ext/opencv/cvmemstorage.h +53 -0
  81. data/ext/opencv/cvmoments.cpp +293 -0
  82. data/ext/opencv/cvmoments.h +75 -0
  83. data/ext/opencv/cvpoint.cpp +265 -0
  84. data/ext/opencv/cvpoint.h +67 -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 +441 -0
  90. data/ext/opencv/cvrect.h +88 -0
  91. data/ext/opencv/cvscalar.cpp +301 -0
  92. data/ext/opencv/cvscalar.h +76 -0
  93. data/ext/opencv/cvseq.cpp +605 -0
  94. data/ext/opencv/cvseq.h +74 -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 +279 -0
  104. data/ext/opencv/cvsurfpoint.h +54 -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 +221 -0
  110. data/ext/opencv/cvutils.h +31 -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 +82 -0
  116. data/ext/opencv/facerecognizer.cpp +181 -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 +666 -0
  125. data/ext/opencv/iplimage.h +75 -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 +833 -0
  131. data/ext/opencv/opencv.h +405 -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 +4 -0
  144. data/test/eigenfaces_save.xml +7524 -0
  145. data/test/fisherfaces_save.xml +7530 -0
  146. data/test/helper.rb +166 -0
  147. data/test/lbph_save.xml +4304 -0
  148. data/test/runner.rb +30 -0
  149. data/test/samples/airplane.jpg +0 -0
  150. data/test/samples/baboon.jpg +0 -0
  151. data/test/samples/baboon200.jpg +0 -0
  152. data/test/samples/baboon200_rotated.jpg +0 -0
  153. data/test/samples/blank0.jpg +0 -0
  154. data/test/samples/blank1.jpg +0 -0
  155. data/test/samples/blank2.jpg +0 -0
  156. data/test/samples/blank3.jpg +0 -0
  157. data/test/samples/blank4.jpg +0 -0
  158. data/test/samples/blank5.jpg +0 -0
  159. data/test/samples/blank6.jpg +0 -0
  160. data/test/samples/blank7.jpg +0 -0
  161. data/test/samples/blank8.jpg +0 -0
  162. data/test/samples/blank9.jpg +0 -0
  163. data/test/samples/cat.jpg +0 -0
  164. data/test/samples/chessboard.jpg +0 -0
  165. data/test/samples/contours.jpg +0 -0
  166. data/test/samples/fruits.jpg +0 -0
  167. data/test/samples/haarcascade_frontalface_alt.xml.gz +0 -0
  168. data/test/samples/inpaint-mask.bmp +0 -0
  169. data/test/samples/lena-256x256.jpg +0 -0
  170. data/test/samples/lena-32x32.jpg +0 -0
  171. data/test/samples/lena-eyes.jpg +0 -0
  172. data/test/samples/lena-inpaint.jpg +0 -0
  173. data/test/samples/lena.jpg +0 -0
  174. data/test/samples/lines.jpg +0 -0
  175. data/test/samples/messy0.jpg +0 -0
  176. data/test/samples/messy1.jpg +0 -0
  177. data/test/samples/movie_sample.avi +0 -0
  178. data/test/samples/one_way_train_0000.jpg +0 -0
  179. data/test/samples/one_way_train_0001.jpg +0 -0
  180. data/test/samples/partially_blank0.jpg +0 -0
  181. data/test/samples/partially_blank1.jpg +0 -0
  182. data/test/samples/smooth0.jpg +0 -0
  183. data/test/samples/smooth1.jpg +0 -0
  184. data/test/samples/smooth2.jpg +0 -0
  185. data/test/samples/smooth3.jpg +0 -0
  186. data/test/samples/smooth4.jpg +0 -0
  187. data/test/samples/smooth5.jpg +0 -0
  188. data/test/samples/smooth6.jpg +0 -0
  189. data/test/samples/str-cv-rotated.jpg +0 -0
  190. data/test/samples/str-cv.jpg +0 -0
  191. data/test/samples/str-ov.jpg +0 -0
  192. data/test/samples/stuff.jpg +0 -0
  193. data/test/test_curve.rb +43 -0
  194. data/test/test_cvavgcomp.rb +24 -0
  195. data/test/test_cvbox2d.rb +76 -0
  196. data/test/test_cvcapture.rb +183 -0
  197. data/test/test_cvchain.rb +108 -0
  198. data/test/test_cvcircle32f.rb +41 -0
  199. data/test/test_cvconnectedcomp.rb +61 -0
  200. data/test/test_cvcontour.rb +150 -0
  201. data/test/test_cvcontourtree.rb +43 -0
  202. data/test/test_cverror.rb +50 -0
  203. data/test/test_cvfeaturetree.rb +65 -0
  204. data/test/test_cvfont.rb +58 -0
  205. data/test/test_cvhaarclassifiercascade.rb +63 -0
  206. data/test/test_cvhistogram.rb +271 -0
  207. data/test/test_cvhumoments.rb +83 -0
  208. data/test/test_cvline.rb +50 -0
  209. data/test/test_cvmat.rb +3003 -0
  210. data/test/test_cvmat_drawing.rb +349 -0
  211. data/test/test_cvmat_dxt.rb +150 -0
  212. data/test/test_cvmat_imageprocessing.rb +2085 -0
  213. data/test/test_cvmoments.rb +180 -0
  214. data/test/test_cvpoint.rb +75 -0
  215. data/test/test_cvpoint2d32f.rb +75 -0
  216. data/test/test_cvpoint3d32f.rb +93 -0
  217. data/test/test_cvrect.rb +144 -0
  218. data/test/test_cvscalar.rb +113 -0
  219. data/test/test_cvseq.rb +295 -0
  220. data/test/test_cvsize.rb +75 -0
  221. data/test/test_cvsize2d32f.rb +75 -0
  222. data/test/test_cvslice.rb +31 -0
  223. data/test/test_cvsurfparams.rb +57 -0
  224. data/test/test_cvsurfpoint.rb +66 -0
  225. data/test/test_cvtermcriteria.rb +56 -0
  226. data/test/test_cvtwopoints.rb +40 -0
  227. data/test/test_cvvideowriter.rb +58 -0
  228. data/test/test_eigenfaces.rb +93 -0
  229. data/test/test_fisherfaces.rb +93 -0
  230. data/test/test_iplconvkernel.rb +54 -0
  231. data/test/test_iplimage.rb +232 -0
  232. data/test/test_lbph.rb +152 -0
  233. data/test/test_mouseevent.rb +17 -0
  234. data/test/test_opencv.rb +360 -0
  235. data/test/test_pointset.rb +128 -0
  236. data/test/test_preliminary.rb +130 -0
  237. data/test/test_trackbar.rb +47 -0
  238. data/test/test_window.rb +115 -0
  239. data/yard_extension.rb +5 -0
  240. metadata +399 -0
@@ -0,0 +1,71 @@
1
+ /************************************************************
2
+
3
+ iplconvkernel.h -
4
+
5
+ $Author: lsxi $
6
+
7
+ Copyright (C) 2005-2006 Masakazu Yonekura
8
+
9
+ ************************************************************/
10
+ #ifndef RUBY_OPENCV_IPLCONVKERNEL_H
11
+ #define RUBY_OPENCV_IPLCONVKERNEL_H
12
+
13
+ #include "opencv.h"
14
+
15
+ #define __NAMESPACE_BEGIN_IPLCONVKERNEL namespace cIplConvKernel {
16
+ #define __NAMESPACE_END_IPLCONVKERNEL }
17
+
18
+ __NAMESPACE_BEGIN_OPENCV
19
+ __NAMESPACE_BEGIN_IPLCONVKERNEL
20
+
21
+ VALUE rb_class();
22
+ VALUE rb_allocate(VALUE klass);
23
+
24
+ void init_ruby_class();
25
+ VALUE rb_initialize(int argc, VALUE *argv, VALUE self);
26
+ VALUE rb_size(VALUE self);
27
+ VALUE rb_cols(VALUE self);
28
+ VALUE rb_rows(VALUE self);
29
+ VALUE rb_anchor(VALUE self);
30
+ VALUE rb_anchor_x(VALUE self);
31
+ VALUE rb_anchor_y(VALUE self);
32
+
33
+ __NAMESPACE_END_IPLCONVKERNEL
34
+
35
+ inline IplConvKernel*
36
+ IPLCONVKERNEL(VALUE object)
37
+ {
38
+ IplConvKernel *ptr;
39
+ Data_Get_Struct(object, IplConvKernel, ptr);
40
+ return ptr;
41
+ }
42
+
43
+ inline IplConvKernel*
44
+ IPLCONVKERNEL_WITH_CHECK(VALUE object)
45
+ {
46
+ if (!rb_obj_is_kind_of(object, cIplConvKernel::rb_class()))
47
+ raise_typeerror(object, cIplConvKernel::rb_class());
48
+ return IPLCONVKERNEL(object);
49
+ }
50
+
51
+ /*
52
+ inline IplConvKernel*
53
+ IPLCONVKERNEL(VALUE object)
54
+ {
55
+ IplConvKernel *ptr;
56
+ if (NIL_P(object))
57
+ return NULL;
58
+ else if (rb_obj_is_kind_of(object, cIplConvKernel::rb_class())) {
59
+ Data_Get_Struct(object, IplConvKernel, ptr);
60
+ return ptr;
61
+ }
62
+ else {
63
+ rb_warn("invalid kernel. use default kernel (3x3 rectangle).");
64
+ return NULL;
65
+ }
66
+ }
67
+ */
68
+
69
+ __NAMESPACE_END_OPENCV
70
+
71
+ #endif // RUBY_OPENCV_IPLCONVKERNEL_H
@@ -0,0 +1,666 @@
1
+ /************************************************************
2
+
3
+ iplimage.cpp -
4
+
5
+ $Author: lsxi $
6
+
7
+ Copyright (C) 2005-2006 Masakazu Yonekura
8
+
9
+ ************************************************************/
10
+ #include "iplimage.h"
11
+ /*
12
+ * Document-class: OpenCV::IplImage
13
+ *
14
+ * IPL(Intel Image Processing Library) Image class.
15
+ *
16
+ * IplImage is subclass of CvMat. IplImage support ROI(region of interest) and COI(color of interest).
17
+ * Most of CvMat method support ROI, and some of CvMat method support COI.
18
+ *
19
+ * =What is ROI?
20
+ * region of interest.
21
+ *
22
+ * =What is COI?
23
+ * color of interest.
24
+ */
25
+ __NAMESPACE_BEGIN_OPENCV
26
+ __NAMESPACE_BEGIN_IPLIMAGE
27
+
28
+ VALUE rb_klass;
29
+
30
+ VALUE
31
+ rb_class()
32
+ {
33
+ return rb_klass;
34
+ }
35
+
36
+ VALUE
37
+ rb_allocate(VALUE klass)
38
+ {
39
+ return OPENCV_OBJECT(rb_klass, 0);
40
+ }
41
+
42
+ /*
43
+ * call-seq:
44
+ * new(width, height[, depth = CV_8U][, channel = 3])
45
+ *
46
+ * Create width * height image. Each element-value set 0.
47
+ *
48
+ * Each element possigle range is set by <i>depth</i>. Default is unsigned 8bit.
49
+ *
50
+ * Number of channel is set by <i>channel</i>. <i>channel</i> should be 1..4.
51
+ *
52
+ * note: width = col, height = row, on CvMat. It is noted not to make a mistake
53
+ * because the order of argument is differenct to CvMat.
54
+ */
55
+ VALUE
56
+ rb_initialize(int argc, VALUE *argv, VALUE self)
57
+ {
58
+ VALUE width, height, depth, channel;
59
+ rb_scan_args(argc, argv, "22", &width, &height, &depth, &channel);
60
+ int _depth = CVMETHOD("DEPTH", depth, CV_8U);
61
+ int _channel = argc < 4 ? 3 : NUM2INT(channel);
62
+ DATA_PTR(self) = rb_cvCreateImage(cvSize(NUM2INT(width), NUM2INT(height)), cvIplDepth(_depth), _channel);
63
+ return self;
64
+ }
65
+
66
+ /*
67
+ * call-seq:
68
+ * IplImage::load(filename[,iscolor = CV_LOAD_IMAGE_COLOR])
69
+ *
70
+ * Load an image from file.
71
+ * iscolor = CV_LOAD_IMAGE_COLOR, the loaded image is forced to be a 3-channel color image
72
+ * iscolor = CV_LOAD_IMAGE_GRAYSCALE, the loaded image is forced to be grayscale
73
+ * iscolor = CV_LOAD_IMAGE_UNCHANGED, the loaded image will be loaded as is.
74
+ * Currently the following file format are supported.
75
+ * * Windows bitmaps - BMP,DIB
76
+ * * JPEG files - JPEG,JPG,JPE
77
+ * * Portable Network Graphics - PNG
78
+ * * Portable image format - PBM,PGM,PPM
79
+ * * Sun rasters - SR,RAS
80
+ * * TIFF files - TIFF,TIF
81
+ */
82
+ VALUE
83
+ rb_load_image(int argc, VALUE *argv, VALUE self)
84
+ {
85
+ VALUE filename, iscolor;
86
+ rb_scan_args(argc, argv, "11", &filename, &iscolor);
87
+ Check_Type(filename, T_STRING);
88
+
89
+ int _iscolor;
90
+ if (TYPE(iscolor) == T_NIL) {
91
+ _iscolor = CV_LOAD_IMAGE_COLOR;
92
+ }
93
+ else {
94
+ Check_Type(iscolor, T_FIXNUM);
95
+ _iscolor = FIX2INT(iscolor);
96
+ }
97
+
98
+ IplImage *image;
99
+ if ((image = cvLoadImage(StringValueCStr(filename), _iscolor)) == NULL) {
100
+ rb_raise(rb_eStandardError, "file does not exist or invalid format image.");
101
+ }
102
+ return OPENCV_OBJECT(rb_klass, image);
103
+ }
104
+
105
+ /*
106
+ * call-seq:
107
+ * decode_image(buf[, iscolor=CV_LOAD_IMAGE_COLOR]) -> IplImage
108
+ *
109
+ * Reads an image from a buffer in memory.
110
+ *
111
+ * Parameters:
112
+ * buf <CvMat, Array, String> - Input array
113
+ * iscolor <Integer> - Flags specifying the color type of a decoded image (the same flags as CvMat#load)
114
+ */
115
+ VALUE
116
+ rb_decode_image(int argc, VALUE *argv, VALUE self)
117
+ {
118
+ int iscolor, need_release;
119
+ CvMat* buff = cCvMat::prepare_decoding(argc, argv, &iscolor, &need_release);
120
+ IplImage* img_ptr = NULL;
121
+ try {
122
+ img_ptr = cvDecodeImage(buff, iscolor);
123
+ if (need_release) {
124
+ cvReleaseMat(&buff);
125
+ }
126
+ }
127
+ catch (cv::Exception& e) {
128
+ raise_cverror(e);
129
+ }
130
+
131
+ return OPENCV_OBJECT(rb_klass, img_ptr);
132
+ }
133
+
134
+ /*
135
+ * Get ROI as CvRect.
136
+ */
137
+ VALUE
138
+ rb_get_roi(VALUE self)
139
+ {
140
+ CvRect rect;
141
+ try {
142
+ rect = cvGetImageROI(IPLIMAGE(self));
143
+ }
144
+ catch (cv::Exception& e) {
145
+ raise_cverror(e);
146
+ }
147
+ return cCvRect::new_object(rect);
148
+ }
149
+
150
+ /*
151
+ * call-seq:
152
+ * set_roi(rect)
153
+ * set_roi(rect){|image| ...}
154
+ *
155
+ * Set ROI. <i>rect</i> should be CvRect or compatible object.
156
+ * Return self.
157
+ */
158
+ VALUE
159
+ rb_set_roi(VALUE self, VALUE roi)
160
+ {
161
+ VALUE block = rb_block_given_p() ? rb_block_proc() : 0;
162
+ try {
163
+ if (block) {
164
+ CvRect prev_roi = cvGetImageROI(IPLIMAGE(self));
165
+ cvSetImageROI(IPLIMAGE(self), VALUE_TO_CVRECT(roi));
166
+ rb_yield_values(1, self);
167
+ cvSetImageROI(IPLIMAGE(self), prev_roi);
168
+ }
169
+ else {
170
+ cvSetImageROI(IPLIMAGE(self), VALUE_TO_CVRECT(roi));
171
+ }
172
+ }
173
+ catch (cv::Exception& e) {
174
+ raise_cverror(e);
175
+ }
176
+ return self;
177
+ }
178
+
179
+
180
+ /*
181
+ * Reset ROI setting. Same as IplImage#roi = nil. Return self.
182
+ */
183
+ VALUE
184
+ rb_reset_roi(VALUE self)
185
+ {
186
+ try {
187
+ cvResetImageROI(IPLIMAGE(self));
188
+ }
189
+ catch (cv::Exception& e) {
190
+ raise_cverror(e);
191
+ }
192
+ return self;
193
+ }
194
+
195
+ /*
196
+ * Return COI as Fixnum.
197
+ */
198
+ VALUE
199
+ rb_get_coi(VALUE self)
200
+ {
201
+ int coi = 0;
202
+ try {
203
+ coi = cvGetImageCOI(IPLIMAGE(self));
204
+ }
205
+ catch (cv::Exception& e) {
206
+ raise_cverror(e);
207
+ }
208
+ return INT2FIX(coi);
209
+ }
210
+
211
+ /*
212
+ * call-seq:
213
+ * set_coi(coi)
214
+ * set_coi(coi){|image| ...}
215
+ *
216
+ * Set COI. <i>coi</i> should be Fixnum.
217
+ * Return self.
218
+ */
219
+ VALUE
220
+ rb_set_coi(VALUE self, VALUE coi)
221
+ {
222
+ VALUE block = rb_block_given_p() ? rb_block_proc() : 0;
223
+ try {
224
+ if (block) {
225
+ int prev_coi = cvGetImageCOI(IPLIMAGE(self));
226
+ cvSetImageCOI(IPLIMAGE(self), NUM2INT(coi));
227
+ rb_yield_values(1, self);
228
+ cvSetImageCOI(IPLIMAGE(self), prev_coi);
229
+ }
230
+ else {
231
+ cvSetImageCOI(IPLIMAGE(self), NUM2INT(coi));
232
+ }
233
+ }
234
+ catch (cv::Exception& e) {
235
+ raise_cverror(e);
236
+ }
237
+ return self;
238
+ }
239
+
240
+ /*
241
+ * Reset COI setting. Same as IplImage#coi = 0. Return self.
242
+ */
243
+ VALUE
244
+ rb_reset_coi(VALUE self)
245
+ {
246
+ try {
247
+ cvSetImageCOI(IPLIMAGE(self), 0);
248
+ }
249
+ catch (cv::Exception& e) {
250
+ raise_cverror(e);
251
+ }
252
+ return self;
253
+ }
254
+
255
+ /*
256
+ * Return a CvMat pointing to the same data as this iplimage
257
+ */
258
+ VALUE
259
+ rb_get_mat(VALUE self)
260
+ {
261
+ CvMat* mat_ptr = NULL;
262
+ try {
263
+ mat_ptr = CVMAT(self);
264
+ }
265
+ catch (cv::Exception& e) {
266
+ raise_cverror(e);
267
+ }
268
+ return DEPEND_OBJECT(cCvMat::rb_class(), mat_ptr, self);
269
+ }
270
+
271
+ /*
272
+ * call-seq:
273
+ * IplImage.smoothness(lowFreqRatio, blankDensity, messyDensity, highFreqRatio) -> [ symbol, float, float ]
274
+ *
275
+ * Determines if the image's smoothness is either, :smooth, :messy, or :blank.
276
+ *
277
+ * Original Author: yuhanz@gmail.com
278
+ */
279
+ VALUE
280
+ rb_smoothness(int argc, VALUE *argv, VALUE self)
281
+ {
282
+ VALUE lowFreqRatio, blankDensity, messyDensity, highFreqRatio;
283
+ rb_scan_args(argc, argv, "04", &lowFreqRatio, &blankDensity, &messyDensity, &highFreqRatio);
284
+
285
+ double f_lowFreqRatio, f_blankDensity, f_messyDensity, f_highFreqRatio;
286
+ double outLowDensity, outHighDensity;
287
+ if (TYPE(lowFreqRatio) == T_NIL) {
288
+ f_lowFreqRatio = 10 / 128.0f;
289
+ }
290
+ else {
291
+ Check_Type(lowFreqRatio, T_FLOAT);
292
+ f_lowFreqRatio = NUM2DBL(lowFreqRatio);
293
+ }
294
+ if (TYPE(blankDensity) == T_NIL) {
295
+ f_blankDensity = 1.2f;
296
+ }
297
+ else {
298
+ Check_Type(blankDensity, T_FLOAT);
299
+ f_blankDensity = NUM2DBL(blankDensity);
300
+ }
301
+ if (TYPE(messyDensity) == T_NIL) {
302
+ f_messyDensity = 0.151f;
303
+ }
304
+ else {
305
+ Check_Type(messyDensity, T_FLOAT);
306
+ f_messyDensity = NUM2DBL(messyDensity);
307
+ }
308
+ if (TYPE(highFreqRatio) == T_NIL) {
309
+ f_highFreqRatio = 5 / 128.0f;
310
+ }
311
+ else {
312
+ Check_Type(highFreqRatio, T_FLOAT);
313
+ f_highFreqRatio = NUM2DBL(highFreqRatio);
314
+ }
315
+
316
+ IplImage *pFourierImage;
317
+ IplImage *p64DepthImage;
318
+
319
+ // the image is required to be in depth of 64
320
+ if (IPLIMAGE(self)->depth == 64) {
321
+ p64DepthImage = NULL;
322
+ pFourierImage = create_fourier_image(IPLIMAGE(self));
323
+ }
324
+ else {
325
+ p64DepthImage = rb_cvCreateImage(cvGetSize(IPLIMAGE(self)), IPL_DEPTH_64F, 1);
326
+ cvConvertScale(CVARR(self), p64DepthImage, 1.0, 0.0);
327
+ pFourierImage = create_fourier_image(p64DepthImage);
328
+ }
329
+
330
+ Smoothness result = compute_smoothness(pFourierImage, f_lowFreqRatio, f_blankDensity, f_messyDensity,
331
+ f_highFreqRatio, outLowDensity, outHighDensity);
332
+
333
+ cvReleaseImage(&pFourierImage);
334
+ if (p64DepthImage != NULL)
335
+ cvReleaseImage(&p64DepthImage);
336
+
337
+ switch(result) {
338
+ case SMOOTH:
339
+ return rb_ary_new3(3, ID2SYM(rb_intern("smooth")), rb_float_new(outLowDensity), rb_float_new(outHighDensity));
340
+ case MESSY:
341
+ return rb_ary_new3(3, ID2SYM(rb_intern("messy")), rb_float_new(outLowDensity), rb_float_new(outHighDensity));
342
+ case BLANK:
343
+ return rb_ary_new3(3, ID2SYM(rb_intern("blank")), rb_float_new(outLowDensity), rb_float_new(outHighDensity));
344
+ default:
345
+ return rb_ary_new3(3, NULL, rb_float_new(outLowDensity), rb_float_new(outHighDensity));
346
+ }
347
+ }
348
+
349
+ /**
350
+ * Note: if lowDensity < blankDensityThreshold -> blank;
351
+ * else if highDensity > messyDensityThreshold -> messy;
352
+ * else -> good;
353
+ */
354
+ Smoothness
355
+ compute_smoothness(const IplImage *pFourierImage, const double lowFreqRatio, const double blankDensity,
356
+ const double messyDensity, const double highFreqRatio, double &outLowDensity,
357
+ double &outHighDensity)
358
+ {
359
+ int low, high;
360
+ IplImage *filteredFourierImage;
361
+ int totalIntensity;
362
+ double sum, den, totalArea;
363
+ CvScalar scalar;
364
+
365
+ if (!(pFourierImage->nChannels == 1 && pFourierImage->depth == 64) ) {
366
+ cvError(CV_StsUnmatchedSizes, "compute_smoothness", "input image must contain only 1 channel and a depth of 64",
367
+ __FILE__, __LINE__ );
368
+ }
369
+
370
+ high_pass_range(pFourierImage, lowFreqRatio, low, high );
371
+ totalArea = M_PI * (high * high - low * low);
372
+
373
+ filteredFourierImage = create_frequency_filtered_image(pFourierImage, low, high);
374
+ scalar = cvSum(filteredFourierImage);
375
+ totalIntensity = (int)scalar.val[0];
376
+ cvReleaseImage(&filteredFourierImage);
377
+ outLowDensity = den = totalIntensity / totalArea;
378
+
379
+ if (den <= blankDensity) {
380
+ return BLANK;
381
+ }
382
+
383
+ low = (int)(high * (1.0 - highFreqRatio));
384
+
385
+ filteredFourierImage = create_frequency_filtered_image(pFourierImage, low, high);
386
+ scalar = cvSum(filteredFourierImage);
387
+ totalIntensity = (int)scalar.val[0];
388
+ cvReleaseImage(&filteredFourierImage);
389
+ outHighDensity = den = totalIntensity / totalArea;
390
+
391
+ if (den >= messyDensity) {
392
+ return MESSY;
393
+ }
394
+
395
+ return SMOOTH;
396
+ }
397
+
398
+ // Rearrange the quadrants of Fourier image so that the origin is at
399
+ // the image center
400
+ // src & dst arrays of equal size & type
401
+ void
402
+ cvShiftDFT(CvArr *src_arr, CvArr *dst_arr )
403
+ {
404
+ CvMat *tmp = NULL;
405
+ CvMat q1stub, q2stub;
406
+ CvMat q3stub, q4stub;
407
+ CvMat d1stub, d2stub;
408
+ CvMat d3stub, d4stub;
409
+ CvMat *q1, *q2, *q3, *q4;
410
+ CvMat *d1, *d2, *d3, *d4;
411
+
412
+ CvSize size = cvGetSize(src_arr);
413
+ CvSize dst_size = cvGetSize(dst_arr);
414
+ int cx, cy;
415
+
416
+ if (dst_size.width != size.width ||
417
+ dst_size.height != size.height) {
418
+ cvError( CV_StsUnmatchedSizes, "cvShiftDFT", "Source and Destination arrays must have equal sizes",
419
+ __FILE__, __LINE__ );
420
+ }
421
+
422
+ if (src_arr == dst_arr) {
423
+ tmp = rb_cvCreateMat(size.height / 2, size.width / 2, cvGetElemType(src_arr));
424
+ }
425
+
426
+ cx = size.width / 2;
427
+ cy = size.height / 2; // image center
428
+
429
+ q1 = cvGetSubRect(src_arr, &q1stub, cvRect(0,0,cx, cy));
430
+ q2 = cvGetSubRect(src_arr, &q2stub, cvRect(cx,0,cx,cy));
431
+ q3 = cvGetSubRect(src_arr, &q3stub, cvRect(cx,cy,cx,cy));
432
+ q4 = cvGetSubRect(src_arr, &q4stub, cvRect(0,cy,cx,cy));
433
+ d1 = cvGetSubRect(src_arr, &d1stub, cvRect(0,0,cx,cy));
434
+ d2 = cvGetSubRect(src_arr, &d2stub, cvRect(cx,0,cx,cy));
435
+ d3 = cvGetSubRect(src_arr, &d3stub, cvRect(cx,cy,cx,cy));
436
+ d4 = cvGetSubRect(src_arr, &d4stub, cvRect(0,cy,cx,cy));
437
+
438
+ if (src_arr != dst_arr) {
439
+ if (!CV_ARE_TYPES_EQ(q1, d1)) {
440
+ cvError(CV_StsUnmatchedFormats, "cvShiftDFT", "Source and Destination arrays must have the same format",
441
+ __FILE__, __LINE__ );
442
+ }
443
+ cvCopy(q3, d1, 0);
444
+ cvCopy(q4, d2, 0);
445
+ cvCopy(q1, d3, 0);
446
+ cvCopy(q2, d4, 0);
447
+ }
448
+ else {
449
+ cvCopy(q3, tmp, 0);
450
+ cvCopy(q1, q3, 0);
451
+ cvCopy(tmp, q1, 0);
452
+ cvCopy(q4, tmp, 0);
453
+ cvCopy(q2, q4, 0);
454
+ cvCopy(tmp, q2, 0);
455
+ }
456
+
457
+ if (tmp != NULL) {
458
+ cvReleaseMat(&tmp);
459
+ }
460
+ }
461
+
462
+ IplImage*
463
+ create_fourier_image(const IplImage *im)
464
+ {
465
+ IplImage *realInput;
466
+ IplImage *imaginaryInput;
467
+ IplImage *complexInput;
468
+ int dft_M, dft_N;
469
+ CvMat *dft_A, tmp;
470
+ IplImage *image_Re;
471
+ IplImage *image_Im;
472
+
473
+ realInput = rb_cvCreateImage(cvGetSize(im), IPL_DEPTH_64F, 1);
474
+ imaginaryInput = rb_cvCreateImage(cvGetSize(im), IPL_DEPTH_64F, 1);
475
+ complexInput = rb_cvCreateImage(cvGetSize(im), IPL_DEPTH_64F, 2);
476
+
477
+ cvScale(im, realInput, 1.0, 0.0);
478
+ cvZero(imaginaryInput);
479
+ cvMerge(realInput, imaginaryInput, NULL, NULL, complexInput);
480
+
481
+ dft_M = cvGetOptimalDFTSize(im->height - 1);
482
+ dft_N = cvGetOptimalDFTSize(im->width - 1);
483
+
484
+ dft_A = rb_cvCreateMat(dft_M, dft_N, CV_64FC2);
485
+ image_Re = rb_cvCreateImage(cvSize(dft_N, dft_M), IPL_DEPTH_64F, 1);
486
+ image_Im = rb_cvCreateImage(cvSize(dft_N, dft_M), IPL_DEPTH_64F, 1);
487
+
488
+ // copy A to dft_A and pad dft_A with zeros
489
+ cvGetSubRect(dft_A, &tmp, cvRect(0,0, im->width, im->height));
490
+ cvCopy(complexInput, &tmp, NULL);
491
+ if (dft_A->cols > im->width) {
492
+ cvGetSubRect(dft_A, &tmp, cvRect(im->width,0, dft_A->cols - im->width, im->height));
493
+ cvZero(&tmp);
494
+ }
495
+
496
+ // no need to pad bottom part of dft_A with zeros because of
497
+ // use nonzero_rows parameter in cvDFT() call below
498
+
499
+ cvDFT(dft_A, dft_A, CV_DXT_FORWARD, complexInput->height);
500
+
501
+ // Split Fourier in real and imaginary parts
502
+ cvSplit(dft_A, image_Re, image_Im, 0, 0);
503
+
504
+ // Compute the magnitude of the spectrum Mag = sqrt(Re^2 + Im^2)
505
+ cvPow(image_Re, image_Re, 2.0);
506
+ cvPow(image_Im, image_Im, 2.0);
507
+ cvAdd(image_Re, image_Im, image_Re, NULL);
508
+ cvPow(image_Re, image_Re, 0.5);
509
+
510
+ // Compute log(1 + Mag)
511
+ cvAddS(image_Re, cvScalarAll(1.0), image_Re, NULL); // 1 + Mag
512
+ cvLog(image_Re, image_Re); // log(1 + Mag)
513
+
514
+ // Rearrange the quadrants of Fourier image so that the origin is at
515
+ // the image center
516
+ cvShiftDFT(image_Re, image_Re);
517
+
518
+ cvReleaseImage(&realInput);
519
+ cvReleaseImage(&imaginaryInput);
520
+ cvReleaseImage(&complexInput);
521
+ cvReleaseImage(&image_Im);
522
+
523
+ cvReleaseMat(&dft_A);
524
+
525
+ return image_Re;
526
+ }
527
+
528
+ IplImage*
529
+ create_frequency_filtered_image(const IplImage *pImage, int low, int high)
530
+ {
531
+
532
+ CvPoint2D32f center;
533
+ center.x = pImage->width / 2;
534
+ center.y = pImage->height / 2;
535
+ CvBox2D box;
536
+ box.center = center;
537
+
538
+ box.size.width = high;
539
+ box.size.height = high;
540
+
541
+ IplImage *pFilterMask = rb_cvCreateImage(cvGetSize(pImage), IPL_DEPTH_64F, 1);
542
+ IplImage *pFiltered = rb_cvCreateImage(cvGetSize(pImage), IPL_DEPTH_64F, 1);
543
+
544
+ cvZero(pFilterMask);
545
+ cvZero(pFiltered);
546
+
547
+ if (high > 0)
548
+ cvEllipseBox(pFilterMask, box, cvScalar(255, 255, 255, 255), CV_FILLED, 8, 0);
549
+
550
+ box.size.width = low;
551
+ box.size.height = low;
552
+ if (low > 0)
553
+ cvEllipseBox(pFilterMask, box, cvScalar(0, 0, 0, 0), CV_FILLED, 8, 0);
554
+
555
+ cvAnd(pImage, pFilterMask, pFiltered, NULL);
556
+
557
+ cvReleaseImage(&pFilterMask);
558
+
559
+ return pFiltered;
560
+ }
561
+
562
+ void
563
+ high_pass_range(const IplImage *pImage, float lostPercentage, int &outLow, int &outHigh)
564
+ {
565
+ if (lostPercentage > 1.0f) {
566
+ lostPercentage = 1;
567
+ }
568
+ else if (lostPercentage < 0.0f) {
569
+ lostPercentage = 0;
570
+ }
571
+
572
+ outHigh = (int)MIN(pImage->width, pImage->height);
573
+ outLow = (int)(lostPercentage * outHigh);
574
+ }
575
+
576
+ /*
577
+ * call-seq:
578
+ * pyr_segmentation(level, threshold1, threshold2) -> [iplimage, cvseq(include cvconnectedcomp)]
579
+ *
580
+ * Does image segmentation by pyramids.
581
+ * The pyramid builds up to the level <i>level<i>.
582
+ * The links between any pixel a on <i>level<i>i and
583
+ * its candidate father pixel b on the adjacent level are established if
584
+ * p(c(a),c(b)) < threshold1. After the connected components are defined, they are joined into several clusters. Any two segments A and B belong to the same cluster, if
585
+ * p(c(A),c(B)) < threshold2. The input image has only one channel, then
586
+ * p(c^2,c^2)=|c^2-c^2|. If the input image has three channels (red, green and blue), then
587
+ * p(c^2,c^2)=0,3*(c^2 r-c^2 r)+0.59*(c^2 g-c^2 g)+0,11*(c^2 b-c^2 b) . There may be more than one connected component per a cluster.
588
+ *
589
+ * Return segmented image and sequence of connected components.
590
+ * <b>support single-channel or 3-channel 8bit unsigned image only</b>
591
+ */
592
+ VALUE
593
+ rb_pyr_segmentation(VALUE self, VALUE level, VALUE threshold1, VALUE threshold2)
594
+ {
595
+ IplImage* self_ptr = IPLIMAGE(self);
596
+ CvSeq *comp = NULL;
597
+ VALUE storage = cCvMemStorage::new_object();
598
+ VALUE dest = Qnil;
599
+ try {
600
+ dest = cIplImage::new_object(cvGetSize(self_ptr), cvGetElemType(self_ptr));
601
+ cvPyrSegmentation(self_ptr, IPLIMAGE(dest), CVMEMSTORAGE(storage), &comp,
602
+ NUM2INT(level), NUM2DBL(threshold1), NUM2DBL(threshold2));
603
+ }
604
+ catch (cv::Exception& e) {
605
+ raise_cverror(e);
606
+ }
607
+ if (!comp) {
608
+ comp = cvCreateSeq(CV_SEQ_CONNECTED_COMP, sizeof(CvSeq), sizeof(CvConnectedComp), CVMEMSTORAGE(storage));
609
+ }
610
+ return rb_ary_new3(2, dest, cCvSeq::new_sequence(cCvSeq::rb_class(), comp, cCvConnectedComp::rb_class(), storage));
611
+ }
612
+
613
+ VALUE
614
+ new_object(int width, int height, int type)
615
+ {
616
+ return OPENCV_OBJECT(rb_klass, rb_cvCreateImage(cvSize(width, height), cvIplDepth(type), CV_MAT_CN(type)));
617
+ }
618
+
619
+ VALUE
620
+ new_object(CvSize size, int type)
621
+ {
622
+ return OPENCV_OBJECT(rb_klass, rb_cvCreateImage(size, cvIplDepth(type), CV_MAT_CN(type)));
623
+ }
624
+
625
+ void
626
+ init_ruby_class()
627
+ {
628
+ #if 0
629
+ // For documentation using YARD
630
+ VALUE opencv = rb_define_module("OpenCV");
631
+ VALUE cvmat = rb_define_class_under(opencv, "CvMat", rb_cObject);
632
+ #endif
633
+
634
+ if (rb_klass)
635
+ return;
636
+ /*
637
+ * opencv = rb_define_module("OpenCV");
638
+ * cvmat = rb_define_class_under(opencv, "CvMat", rb_cObject);
639
+ *
640
+ * note: this comment is used by rdoc.
641
+ */
642
+ VALUE opencv = rb_module_opencv();
643
+ VALUE cvmat = cCvMat::rb_class();
644
+ rb_klass = rb_define_class_under(opencv, "IplImage", cvmat);
645
+ rb_define_alloc_func(rb_klass, rb_allocate);
646
+ rb_define_singleton_method(rb_klass, "load", RUBY_METHOD_FUNC(rb_load_image), -1);
647
+ rb_define_method(rb_klass, "initialize", RUBY_METHOD_FUNC(rb_initialize), -1);
648
+ rb_define_method(rb_klass, "get_roi", RUBY_METHOD_FUNC(rb_get_roi), 0);
649
+ rb_define_alias(rb_klass, "roi", "get_roi");
650
+ rb_define_method(rb_klass, "set_roi", RUBY_METHOD_FUNC(rb_set_roi), 1);
651
+ rb_define_alias(rb_klass, "roi=", "set_roi");
652
+ rb_define_method(rb_klass, "reset_roi", RUBY_METHOD_FUNC(rb_reset_roi), 0);
653
+ rb_define_method(rb_klass, "get_coi", RUBY_METHOD_FUNC(rb_get_coi), 0);
654
+ rb_define_alias(rb_klass, "coi", "get_coi");
655
+ rb_define_method(rb_klass, "set_coi", RUBY_METHOD_FUNC(rb_set_coi), 1);
656
+ rb_define_alias(rb_klass, "coi=", "set_coi");
657
+ rb_define_method(rb_klass, "reset_coi", RUBY_METHOD_FUNC(rb_reset_coi), 0);
658
+ rb_define_method(rb_klass, "pyr_segmentation", RUBY_METHOD_FUNC(rb_pyr_segmentation), 3);
659
+ rb_define_method(rb_klass, "smoothness", RUBY_METHOD_FUNC(rb_smoothness), -1);
660
+
661
+ rb_define_singleton_method(rb_klass, "decode_image", RUBY_METHOD_FUNC(rb_decode_image), -1);
662
+ rb_define_alias(rb_singleton_class(rb_klass), "decode", "decode_image");
663
+ }
664
+
665
+ __NAMESPACE_END_IPLIMAGE
666
+ __NAMESPACE_END_OPENCV