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,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,651 @@
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 Integer.
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 Integer.
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
+ * call-seq:
257
+ * IplImage.smoothness(lowFreqRatio, blankDensity, messyDensity, highFreqRatio) -> [ symbol, float, float ]
258
+ *
259
+ * Determines if the image's smoothness is either, :smooth, :messy, or :blank.
260
+ *
261
+ * Original Author: yuhanz@gmail.com
262
+ */
263
+ VALUE
264
+ rb_smoothness(int argc, VALUE *argv, VALUE self)
265
+ {
266
+ VALUE lowFreqRatio, blankDensity, messyDensity, highFreqRatio;
267
+ rb_scan_args(argc, argv, "04", &lowFreqRatio, &blankDensity, &messyDensity, &highFreqRatio);
268
+
269
+ double f_lowFreqRatio, f_blankDensity, f_messyDensity, f_highFreqRatio;
270
+ double outLowDensity, outHighDensity;
271
+ if (TYPE(lowFreqRatio) == T_NIL) {
272
+ f_lowFreqRatio = 10 / 128.0f;
273
+ }
274
+ else {
275
+ Check_Type(lowFreqRatio, T_FLOAT);
276
+ f_lowFreqRatio = NUM2DBL(lowFreqRatio);
277
+ }
278
+ if (TYPE(blankDensity) == T_NIL) {
279
+ f_blankDensity = 1.2f;
280
+ }
281
+ else {
282
+ Check_Type(blankDensity, T_FLOAT);
283
+ f_blankDensity = NUM2DBL(blankDensity);
284
+ }
285
+ if (TYPE(messyDensity) == T_NIL) {
286
+ f_messyDensity = 0.151f;
287
+ }
288
+ else {
289
+ Check_Type(messyDensity, T_FLOAT);
290
+ f_messyDensity = NUM2DBL(messyDensity);
291
+ }
292
+ if (TYPE(highFreqRatio) == T_NIL) {
293
+ f_highFreqRatio = 5 / 128.0f;
294
+ }
295
+ else {
296
+ Check_Type(highFreqRatio, T_FLOAT);
297
+ f_highFreqRatio = NUM2DBL(highFreqRatio);
298
+ }
299
+
300
+ IplImage *pFourierImage;
301
+ IplImage *p64DepthImage;
302
+
303
+ // the image is required to be in depth of 64
304
+ if (IPLIMAGE(self)->depth == 64) {
305
+ p64DepthImage = NULL;
306
+ pFourierImage = create_fourier_image(IPLIMAGE(self));
307
+ }
308
+ else {
309
+ p64DepthImage = rb_cvCreateImage(cvGetSize(IPLIMAGE(self)), IPL_DEPTH_64F, 1);
310
+ cvConvertScale(CVARR(self), p64DepthImage, 1.0, 0.0);
311
+ pFourierImage = create_fourier_image(p64DepthImage);
312
+ }
313
+
314
+ Smoothness result = compute_smoothness(pFourierImage, f_lowFreqRatio, f_blankDensity, f_messyDensity,
315
+ f_highFreqRatio, outLowDensity, outHighDensity);
316
+
317
+ cvReleaseImage(&pFourierImage);
318
+ if (p64DepthImage != NULL)
319
+ cvReleaseImage(&p64DepthImage);
320
+
321
+ switch(result) {
322
+ case SMOOTH:
323
+ return rb_ary_new3(3, ID2SYM(rb_intern("smooth")), rb_float_new(outLowDensity), rb_float_new(outHighDensity));
324
+ case MESSY:
325
+ return rb_ary_new3(3, ID2SYM(rb_intern("messy")), rb_float_new(outLowDensity), rb_float_new(outHighDensity));
326
+ case BLANK:
327
+ return rb_ary_new3(3, ID2SYM(rb_intern("blank")), rb_float_new(outLowDensity), rb_float_new(outHighDensity));
328
+ default:
329
+ return rb_ary_new3(3, NULL, rb_float_new(outLowDensity), rb_float_new(outHighDensity));
330
+ }
331
+ }
332
+
333
+ /**
334
+ * Note: if lowDensity < blankDensityThreshold -> blank;
335
+ * else if highDensity > messyDensityThreshold -> messy;
336
+ * else -> good;
337
+ */
338
+ Smoothness
339
+ compute_smoothness(const IplImage *pFourierImage, const double lowFreqRatio, const double blankDensity,
340
+ const double messyDensity, const double highFreqRatio, double &outLowDensity,
341
+ double &outHighDensity)
342
+ {
343
+ int low, high;
344
+ IplImage *filteredFourierImage;
345
+ int totalIntensity;
346
+ double den, totalArea;
347
+ CvScalar scalar;
348
+
349
+ if (!(pFourierImage->nChannels == 1 && pFourierImage->depth == 64) ) {
350
+ cvError(CV_StsUnmatchedSizes, "compute_smoothness", "input image must contain only 1 channel and a depth of 64",
351
+ __FILE__, __LINE__ );
352
+ }
353
+
354
+ high_pass_range(pFourierImage, lowFreqRatio, low, high );
355
+ totalArea = M_PI * (high * high - low * low);
356
+
357
+ filteredFourierImage = create_frequency_filtered_image(pFourierImage, low, high);
358
+ scalar = cvSum(filteredFourierImage);
359
+ totalIntensity = (int)scalar.val[0];
360
+ cvReleaseImage(&filteredFourierImage);
361
+ outLowDensity = den = totalIntensity / totalArea;
362
+
363
+ if (den <= blankDensity) {
364
+ return BLANK;
365
+ }
366
+
367
+ low = (int)(high * (1.0 - highFreqRatio));
368
+
369
+ filteredFourierImage = create_frequency_filtered_image(pFourierImage, low, high);
370
+ scalar = cvSum(filteredFourierImage);
371
+ totalIntensity = (int)scalar.val[0];
372
+ cvReleaseImage(&filteredFourierImage);
373
+ outHighDensity = den = totalIntensity / totalArea;
374
+
375
+ if (den >= messyDensity) {
376
+ return MESSY;
377
+ }
378
+
379
+ return SMOOTH;
380
+ }
381
+
382
+ // Rearrange the quadrants of Fourier image so that the origin is at
383
+ // the image center
384
+ // src & dst arrays of equal size & type
385
+ void
386
+ cvShiftDFT(CvArr *src_arr, CvArr *dst_arr )
387
+ {
388
+ CvMat *tmp = NULL;
389
+ CvMat q1stub, q2stub;
390
+ CvMat q3stub, q4stub;
391
+ CvMat d1stub, d2stub;
392
+ CvMat d3stub, d4stub;
393
+ CvMat *q1, *q2, *q3, *q4;
394
+ CvMat *d1, *d2, *d3, *d4;
395
+
396
+ CvSize size = cvGetSize(src_arr);
397
+ CvSize dst_size = cvGetSize(dst_arr);
398
+ int cx, cy;
399
+
400
+ if (dst_size.width != size.width ||
401
+ dst_size.height != size.height) {
402
+ cvError( CV_StsUnmatchedSizes, "cvShiftDFT", "Source and Destination arrays must have equal sizes",
403
+ __FILE__, __LINE__ );
404
+ }
405
+
406
+ if (src_arr == dst_arr) {
407
+ tmp = rb_cvCreateMat(size.height / 2, size.width / 2, cvGetElemType(src_arr));
408
+ }
409
+
410
+ cx = size.width / 2;
411
+ cy = size.height / 2; // image center
412
+
413
+ q1 = cvGetSubRect(src_arr, &q1stub, cvRect(0,0,cx, cy));
414
+ q2 = cvGetSubRect(src_arr, &q2stub, cvRect(cx,0,cx,cy));
415
+ q3 = cvGetSubRect(src_arr, &q3stub, cvRect(cx,cy,cx,cy));
416
+ q4 = cvGetSubRect(src_arr, &q4stub, cvRect(0,cy,cx,cy));
417
+ d1 = cvGetSubRect(src_arr, &d1stub, cvRect(0,0,cx,cy));
418
+ d2 = cvGetSubRect(src_arr, &d2stub, cvRect(cx,0,cx,cy));
419
+ d3 = cvGetSubRect(src_arr, &d3stub, cvRect(cx,cy,cx,cy));
420
+ d4 = cvGetSubRect(src_arr, &d4stub, cvRect(0,cy,cx,cy));
421
+
422
+ if (src_arr != dst_arr) {
423
+ if (!CV_ARE_TYPES_EQ(q1, d1)) {
424
+ cvError(CV_StsUnmatchedFormats, "cvShiftDFT", "Source and Destination arrays must have the same format",
425
+ __FILE__, __LINE__ );
426
+ }
427
+ cvCopy(q3, d1, 0);
428
+ cvCopy(q4, d2, 0);
429
+ cvCopy(q1, d3, 0);
430
+ cvCopy(q2, d4, 0);
431
+ }
432
+ else {
433
+ cvCopy(q3, tmp, 0);
434
+ cvCopy(q1, q3, 0);
435
+ cvCopy(tmp, q1, 0);
436
+ cvCopy(q4, tmp, 0);
437
+ cvCopy(q2, q4, 0);
438
+ cvCopy(tmp, q2, 0);
439
+ }
440
+
441
+ if (tmp != NULL) {
442
+ cvReleaseMat(&tmp);
443
+ }
444
+ }
445
+
446
+ IplImage*
447
+ create_fourier_image(const IplImage *im)
448
+ {
449
+ IplImage *realInput;
450
+ IplImage *imaginaryInput;
451
+ IplImage *complexInput;
452
+ int dft_M, dft_N;
453
+ CvMat *dft_A, tmp;
454
+ IplImage *image_Re;
455
+ IplImage *image_Im;
456
+
457
+ realInput = rb_cvCreateImage(cvGetSize(im), IPL_DEPTH_64F, 1);
458
+ imaginaryInput = rb_cvCreateImage(cvGetSize(im), IPL_DEPTH_64F, 1);
459
+ complexInput = rb_cvCreateImage(cvGetSize(im), IPL_DEPTH_64F, 2);
460
+
461
+ cvScale(im, realInput, 1.0, 0.0);
462
+ cvZero(imaginaryInput);
463
+ cvMerge(realInput, imaginaryInput, NULL, NULL, complexInput);
464
+
465
+ dft_M = cvGetOptimalDFTSize(im->height - 1);
466
+ dft_N = cvGetOptimalDFTSize(im->width - 1);
467
+
468
+ dft_A = rb_cvCreateMat(dft_M, dft_N, CV_64FC2);
469
+ image_Re = rb_cvCreateImage(cvSize(dft_N, dft_M), IPL_DEPTH_64F, 1);
470
+ image_Im = rb_cvCreateImage(cvSize(dft_N, dft_M), IPL_DEPTH_64F, 1);
471
+
472
+ // copy A to dft_A and pad dft_A with zeros
473
+ cvGetSubRect(dft_A, &tmp, cvRect(0,0, im->width, im->height));
474
+ cvCopy(complexInput, &tmp, NULL);
475
+ if (dft_A->cols > im->width) {
476
+ cvGetSubRect(dft_A, &tmp, cvRect(im->width,0, dft_A->cols - im->width, im->height));
477
+ cvZero(&tmp);
478
+ }
479
+
480
+ // no need to pad bottom part of dft_A with zeros because of
481
+ // use nonzero_rows parameter in cvDFT() call below
482
+
483
+ cvDFT(dft_A, dft_A, CV_DXT_FORWARD, complexInput->height);
484
+
485
+ // Split Fourier in real and imaginary parts
486
+ cvSplit(dft_A, image_Re, image_Im, 0, 0);
487
+
488
+ // Compute the magnitude of the spectrum Mag = sqrt(Re^2 + Im^2)
489
+ cvPow(image_Re, image_Re, 2.0);
490
+ cvPow(image_Im, image_Im, 2.0);
491
+ cvAdd(image_Re, image_Im, image_Re, NULL);
492
+ cvPow(image_Re, image_Re, 0.5);
493
+
494
+ // Compute log(1 + Mag)
495
+ cvAddS(image_Re, cvScalarAll(1.0), image_Re, NULL); // 1 + Mag
496
+ cvLog(image_Re, image_Re); // log(1 + Mag)
497
+
498
+ // Rearrange the quadrants of Fourier image so that the origin is at
499
+ // the image center
500
+ cvShiftDFT(image_Re, image_Re);
501
+
502
+ cvReleaseImage(&realInput);
503
+ cvReleaseImage(&imaginaryInput);
504
+ cvReleaseImage(&complexInput);
505
+ cvReleaseImage(&image_Im);
506
+
507
+ cvReleaseMat(&dft_A);
508
+
509
+ return image_Re;
510
+ }
511
+
512
+ IplImage*
513
+ create_frequency_filtered_image(const IplImage *pImage, int low, int high)
514
+ {
515
+
516
+ CvPoint2D32f center;
517
+ center.x = (float)(pImage->width / 2);
518
+ center.y = (float)(pImage->height / 2);
519
+ CvBox2D box;
520
+ box.center = center;
521
+
522
+ box.size.width = (float)high;
523
+ box.size.height = (float)high;
524
+
525
+ IplImage *pFilterMask = rb_cvCreateImage(cvGetSize(pImage), IPL_DEPTH_64F, 1);
526
+ IplImage *pFiltered = rb_cvCreateImage(cvGetSize(pImage), IPL_DEPTH_64F, 1);
527
+
528
+ cvZero(pFilterMask);
529
+ cvZero(pFiltered);
530
+
531
+ if (high > 0)
532
+ cvEllipseBox(pFilterMask, box, cvScalar(255, 255, 255, 255), CV_FILLED, 8, 0);
533
+
534
+ box.size.width = (float)low;
535
+ box.size.height = (float)low;
536
+ if (low > 0)
537
+ cvEllipseBox(pFilterMask, box, cvScalar(0, 0, 0, 0), CV_FILLED, 8, 0);
538
+
539
+ cvAnd(pImage, pFilterMask, pFiltered, NULL);
540
+
541
+ cvReleaseImage(&pFilterMask);
542
+
543
+ return pFiltered;
544
+ }
545
+
546
+ void
547
+ high_pass_range(const IplImage *pImage, float lostPercentage, int &outLow, int &outHigh)
548
+ {
549
+ if (lostPercentage > 1.0f) {
550
+ lostPercentage = 1;
551
+ }
552
+ else if (lostPercentage < 0.0f) {
553
+ lostPercentage = 0;
554
+ }
555
+
556
+ outHigh = (int)MIN(pImage->width, pImage->height);
557
+ outLow = (int)(lostPercentage * outHigh);
558
+ }
559
+
560
+ /*
561
+ * call-seq:
562
+ * pyr_segmentation(level, threshold1, threshold2) -> [iplimage, cvseq(include cvconnectedcomp)]
563
+ *
564
+ * Does image segmentation by pyramids.
565
+ * The pyramid builds up to the level <i>level<i>.
566
+ * The links between any pixel a on <i>level<i>i and
567
+ * its candidate father pixel b on the adjacent level are established if
568
+ * 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
569
+ * p(c(A),c(B)) < threshold2. The input image has only one channel, then
570
+ * p(c^2,c^2)=|c^2-c^2|. If the input image has three channels (red, green and blue), then
571
+ * 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.
572
+ *
573
+ * Return segmented image and sequence of connected components.
574
+ * <b>support single-channel or 3-channel 8bit unsigned image only</b>
575
+ */
576
+ VALUE
577
+ rb_pyr_segmentation(VALUE self, VALUE level, VALUE threshold1, VALUE threshold2)
578
+ {
579
+ IplImage* self_ptr = IPLIMAGE(self);
580
+ CvSeq *comp = NULL;
581
+ VALUE storage = cCvMemStorage::new_object();
582
+ VALUE dest = Qnil;
583
+ try {
584
+ dest = cIplImage::new_object(cvGetSize(self_ptr), cvGetElemType(self_ptr));
585
+ cvPyrSegmentation(self_ptr, IPLIMAGE(dest), CVMEMSTORAGE(storage), &comp,
586
+ NUM2INT(level), NUM2DBL(threshold1), NUM2DBL(threshold2));
587
+ }
588
+ catch (cv::Exception& e) {
589
+ raise_cverror(e);
590
+ }
591
+ if (!comp) {
592
+ comp = cvCreateSeq(CV_SEQ_CONNECTED_COMP, sizeof(CvSeq), sizeof(CvConnectedComp), CVMEMSTORAGE(storage));
593
+ }
594
+ return rb_ary_new3(2, dest, cCvSeq::new_sequence(cCvSeq::rb_class(), comp, cCvConnectedComp::rb_class(), storage));
595
+ }
596
+
597
+ VALUE
598
+ new_object(int width, int height, int type)
599
+ {
600
+ return OPENCV_OBJECT(rb_klass, rb_cvCreateImage(cvSize(width, height), cvIplDepth(type), CV_MAT_CN(type)));
601
+ }
602
+
603
+ VALUE
604
+ new_object(CvSize size, int type)
605
+ {
606
+ return OPENCV_OBJECT(rb_klass, rb_cvCreateImage(size, cvIplDepth(type), CV_MAT_CN(type)));
607
+ }
608
+
609
+ void
610
+ init_ruby_class()
611
+ {
612
+ #if 0
613
+ // For documentation using YARD
614
+ VALUE opencv = rb_define_module("OpenCV");
615
+ VALUE cvmat = rb_define_class_under(opencv, "CvMat", rb_cObject);
616
+ #endif
617
+
618
+ if (rb_klass)
619
+ return;
620
+ /*
621
+ * opencv = rb_define_module("OpenCV");
622
+ * cvmat = rb_define_class_under(opencv, "CvMat", rb_cObject);
623
+ *
624
+ * note: this comment is used by rdoc.
625
+ */
626
+ VALUE opencv = rb_module_opencv();
627
+ VALUE cvmat = cCvMat::rb_class();
628
+ rb_klass = rb_define_class_under(opencv, "IplImage", cvmat);
629
+ rb_define_alloc_func(rb_klass, rb_allocate);
630
+ rb_define_singleton_method(rb_klass, "load", RUBY_METHOD_FUNC(rb_load_image), -1);
631
+ rb_define_method(rb_klass, "initialize", RUBY_METHOD_FUNC(rb_initialize), -1);
632
+ rb_define_method(rb_klass, "get_roi", RUBY_METHOD_FUNC(rb_get_roi), 0);
633
+ rb_define_alias(rb_klass, "roi", "get_roi");
634
+ rb_define_method(rb_klass, "set_roi", RUBY_METHOD_FUNC(rb_set_roi), 1);
635
+ rb_define_alias(rb_klass, "roi=", "set_roi");
636
+ rb_define_method(rb_klass, "reset_roi", RUBY_METHOD_FUNC(rb_reset_roi), 0);
637
+ rb_define_method(rb_klass, "get_coi", RUBY_METHOD_FUNC(rb_get_coi), 0);
638
+ rb_define_alias(rb_klass, "coi", "get_coi");
639
+ rb_define_method(rb_klass, "set_coi", RUBY_METHOD_FUNC(rb_set_coi), 1);
640
+ rb_define_alias(rb_klass, "coi=", "set_coi");
641
+ rb_define_method(rb_klass, "reset_coi", RUBY_METHOD_FUNC(rb_reset_coi), 0);
642
+ rb_define_method(rb_klass, "pyr_segmentation", RUBY_METHOD_FUNC(rb_pyr_segmentation), 3);
643
+ rb_define_method(rb_klass, "smoothness", RUBY_METHOD_FUNC(rb_smoothness), -1);
644
+
645
+ rb_define_singleton_method(rb_klass, "decode_image", RUBY_METHOD_FUNC(rb_decode_image), -1);
646
+ rb_define_alias(rb_singleton_class(rb_klass), "decode", "decode_image");
647
+ }
648
+
649
+ __NAMESPACE_END_IPLIMAGE
650
+ __NAMESPACE_END_OPENCV
651
+