ruby-opencv 0.0.8-x86-mingw32 → 0.0.9.pre2-x86-mingw32
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/DEVELOPERS_NOTE.md +29 -12
- data/Gemfile +1 -2
- data/License.txt +30 -30
- data/Manifest.txt +5 -4
- data/README.md +1 -1
- data/Rakefile +62 -4
- data/config.yml +7 -0
- data/examples/alpha_blend.rb +21 -21
- data/examples/find_obj.rb +169 -169
- data/examples/match_kdtree.rb +88 -88
- data/ext/opencv/cvcapture.cpp +19 -12
- data/ext/opencv/cvutils.cpp +192 -194
- data/ext/opencv/cvutils.h +30 -29
- data/{extconf.rb → ext/opencv/extconf.rb} +12 -4
- data/lib/opencv.rb +12 -3
- data/lib/opencv/psyched_yaml.rb +22 -22
- data/lib/opencv/version.rb +1 -1
- data/ruby-opencv.gemspec +17 -16
- data/test/helper.rb +1 -1
- data/test/runner.rb +30 -30
- data/test/test_curve.rb +1 -1
- data/test/test_cvavgcomp.rb +24 -24
- data/test/test_cvbox2d.rb +76 -76
- data/test/test_cvcapture.rb +183 -183
- data/test/test_cvchain.rb +108 -108
- data/test/test_cvcircle32f.rb +41 -41
- data/test/test_cvconnectedcomp.rb +61 -61
- data/test/test_cvcontour.rb +150 -150
- data/test/test_cvcontourtree.rb +43 -43
- data/test/test_cverror.rb +1 -1
- data/test/test_cvfeaturetree.rb +65 -65
- data/test/test_cvfont.rb +58 -58
- data/test/test_cvhaarclassifiercascade.rb +63 -63
- data/test/test_cvhistogram.rb +1 -1
- data/test/test_cvhumoments.rb +83 -83
- data/test/test_cvline.rb +50 -50
- data/test/test_cvmat.rb +1 -1
- data/test/test_cvmat_drawing.rb +1 -1
- data/test/test_cvmat_dxt.rb +1 -1
- data/test/test_cvmat_imageprocessing.rb +1 -1
- data/test/test_cvmat_matching.rb +1 -1
- data/test/test_cvmoments.rb +180 -180
- data/test/test_cvpoint.rb +75 -75
- data/test/test_cvpoint2d32f.rb +75 -75
- data/test/test_cvpoint3d32f.rb +93 -93
- data/test/test_cvrect.rb +144 -144
- data/test/test_cvscalar.rb +113 -113
- data/test/test_cvseq.rb +295 -295
- data/test/test_cvsize.rb +75 -75
- data/test/test_cvsize2d32f.rb +75 -75
- data/test/test_cvslice.rb +31 -31
- data/test/test_cvsurfparams.rb +57 -57
- data/test/test_cvsurfpoint.rb +66 -66
- data/test/test_cvtermcriteria.rb +56 -56
- data/test/test_cvtwopoints.rb +40 -40
- data/test/test_cvvideowriter.rb +58 -58
- data/test/test_iplconvkernel.rb +54 -54
- data/test/test_iplimage.rb +1 -1
- data/test/test_mouseevent.rb +17 -17
- data/test/test_opencv.rb +1 -1
- data/test/test_pointset.rb +1 -1
- data/test/test_preliminary.rb +130 -130
- data/test/test_trackbar.rb +47 -47
- data/test/test_window.rb +115 -115
- metadata +26 -54
- data/ext/opencv/lib/opencv.rb +0 -3
- data/ext/opencv/lib/opencv/psyched_yaml.rb +0 -22
- data/ext/opencv/lib/opencv/version.rb +0 -3
data/examples/match_kdtree.rb
CHANGED
@@ -1,88 +1,88 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# -*- mode: ruby; coding: utf-8
|
3
|
-
|
4
|
-
# A sample of matching SURF feature points using kd-tree
|
5
|
-
# See http://tech.groups.yahoo.com/group/OpenCV/message/62318
|
6
|
-
|
7
|
-
require 'opencv'
|
8
|
-
include OpenCV
|
9
|
-
|
10
|
-
USE_EXTENDED_DESCRIPTOR = true
|
11
|
-
THRESHOLD = 1500
|
12
|
-
DESCRIPTOR_SIZE = USE_EXTENDED_DESCRIPTOR ? 128 : 64
|
13
|
-
|
14
|
-
img1 = CvMat.load('lenna.jpg', CV_LOAD_IMAGE_GRAYSCALE)
|
15
|
-
img2 = CvMat.load('lenna-rotated.jpg', CV_LOAD_IMAGE_GRAYSCALE)
|
16
|
-
|
17
|
-
puts 'Extracting features from img1 using SURF...'
|
18
|
-
param = CvSURFParams.new(THRESHOLD, USE_EXTENDED_DESCRIPTOR)
|
19
|
-
kp1, desc1 = img1.extract_surf(param)
|
20
|
-
puts "found #{kp1.size} keypoints from img1"
|
21
|
-
|
22
|
-
puts 'Extracting features from img2 using SURF...'
|
23
|
-
kp2, desc2 = img2.extract_surf(param)
|
24
|
-
puts "found #{kp2.size} keypoints from img2"
|
25
|
-
|
26
|
-
puts 'Matching keypoints...'
|
27
|
-
desc1mat = CvMat.new(kp1.size, DESCRIPTOR_SIZE, :cv32f, 1)
|
28
|
-
desc2mat = CvMat.new(kp2.size, DESCRIPTOR_SIZE, :cv32f, 1)
|
29
|
-
desc1.each_with_index { |desc, i|
|
30
|
-
desc.each_with_index { |d, j|
|
31
|
-
desc1mat[i, j] = CvScalar.new(d)
|
32
|
-
}
|
33
|
-
}
|
34
|
-
desc2.each_with_index { |desc, i|
|
35
|
-
desc.each_with_index { |d, j|
|
36
|
-
desc2mat[i, j] = CvScalar.new(d)
|
37
|
-
}
|
38
|
-
}
|
39
|
-
|
40
|
-
feature_tree = CvFeatureTree.new(desc1mat)
|
41
|
-
results, distances = feature_tree.find_features(desc2mat, 1, 250)
|
42
|
-
|
43
|
-
reverse_lookup = []
|
44
|
-
reverse_lookup_dist = []
|
45
|
-
kp1.size.times { |i|
|
46
|
-
reverse_lookup << -1
|
47
|
-
reverse_lookup_dist << Float::MAX
|
48
|
-
}
|
49
|
-
|
50
|
-
match_count = 0
|
51
|
-
kp2.size.times { |j|
|
52
|
-
i = results[j][0].to_i
|
53
|
-
d = distances[j][0]
|
54
|
-
if (d < reverse_lookup_dist[i])
|
55
|
-
match_count += 1 if reverse_lookup_dist[i] == Float::MAX
|
56
|
-
reverse_lookup[i] = j
|
57
|
-
reverse_lookup_dist[i] = d
|
58
|
-
end
|
59
|
-
}
|
60
|
-
puts "found #{match_count} putative correspondences"
|
61
|
-
|
62
|
-
points1 = []
|
63
|
-
points2 = []
|
64
|
-
kp2.size.times { |j|
|
65
|
-
i = results[j][0].to_i
|
66
|
-
if (j == reverse_lookup[i])
|
67
|
-
points1 << kp1[i].pt
|
68
|
-
points2 << kp2[j].pt
|
69
|
-
end
|
70
|
-
}
|
71
|
-
|
72
|
-
width = img1.cols + img2.cols
|
73
|
-
height = (img1.rows > img2.rows) ? img1.rows : img2.rows
|
74
|
-
correspond = IplImage.new(width, height, :cv8u, 1);
|
75
|
-
correspond.set_roi(CvRect.new(0, 0, img1.cols, img1.rows))
|
76
|
-
img1.copy(correspond)
|
77
|
-
correspond.set_roi(CvRect.new(img1.cols, 0, img1.cols + img2.cols, img2.rows))
|
78
|
-
img2.copy(correspond)
|
79
|
-
correspond.reset_roi
|
80
|
-
|
81
|
-
points1.zip(points2) { |pt1, pt2|
|
82
|
-
pt2.x += img1.cols
|
83
|
-
correspond.line!(pt1, pt2, :color => CvColor::White)
|
84
|
-
}
|
85
|
-
|
86
|
-
GUI::Window.new('Object Correspond').show correspond
|
87
|
-
GUI::wait_key
|
88
|
-
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# -*- mode: ruby; coding: utf-8 -*-
|
3
|
+
|
4
|
+
# A sample of matching SURF feature points using kd-tree
|
5
|
+
# See http://tech.groups.yahoo.com/group/OpenCV/message/62318
|
6
|
+
|
7
|
+
require 'opencv'
|
8
|
+
include OpenCV
|
9
|
+
|
10
|
+
USE_EXTENDED_DESCRIPTOR = true
|
11
|
+
THRESHOLD = 1500
|
12
|
+
DESCRIPTOR_SIZE = USE_EXTENDED_DESCRIPTOR ? 128 : 64
|
13
|
+
|
14
|
+
img1 = CvMat.load('lenna.jpg', CV_LOAD_IMAGE_GRAYSCALE)
|
15
|
+
img2 = CvMat.load('lenna-rotated.jpg', CV_LOAD_IMAGE_GRAYSCALE)
|
16
|
+
|
17
|
+
puts 'Extracting features from img1 using SURF...'
|
18
|
+
param = CvSURFParams.new(THRESHOLD, USE_EXTENDED_DESCRIPTOR)
|
19
|
+
kp1, desc1 = img1.extract_surf(param)
|
20
|
+
puts "found #{kp1.size} keypoints from img1"
|
21
|
+
|
22
|
+
puts 'Extracting features from img2 using SURF...'
|
23
|
+
kp2, desc2 = img2.extract_surf(param)
|
24
|
+
puts "found #{kp2.size} keypoints from img2"
|
25
|
+
|
26
|
+
puts 'Matching keypoints...'
|
27
|
+
desc1mat = CvMat.new(kp1.size, DESCRIPTOR_SIZE, :cv32f, 1)
|
28
|
+
desc2mat = CvMat.new(kp2.size, DESCRIPTOR_SIZE, :cv32f, 1)
|
29
|
+
desc1.each_with_index { |desc, i|
|
30
|
+
desc.each_with_index { |d, j|
|
31
|
+
desc1mat[i, j] = CvScalar.new(d)
|
32
|
+
}
|
33
|
+
}
|
34
|
+
desc2.each_with_index { |desc, i|
|
35
|
+
desc.each_with_index { |d, j|
|
36
|
+
desc2mat[i, j] = CvScalar.new(d)
|
37
|
+
}
|
38
|
+
}
|
39
|
+
|
40
|
+
feature_tree = CvFeatureTree.new(desc1mat)
|
41
|
+
results, distances = feature_tree.find_features(desc2mat, 1, 250)
|
42
|
+
|
43
|
+
reverse_lookup = []
|
44
|
+
reverse_lookup_dist = []
|
45
|
+
kp1.size.times { |i|
|
46
|
+
reverse_lookup << -1
|
47
|
+
reverse_lookup_dist << Float::MAX
|
48
|
+
}
|
49
|
+
|
50
|
+
match_count = 0
|
51
|
+
kp2.size.times { |j|
|
52
|
+
i = results[j][0].to_i
|
53
|
+
d = distances[j][0]
|
54
|
+
if (d < reverse_lookup_dist[i])
|
55
|
+
match_count += 1 if reverse_lookup_dist[i] == Float::MAX
|
56
|
+
reverse_lookup[i] = j
|
57
|
+
reverse_lookup_dist[i] = d
|
58
|
+
end
|
59
|
+
}
|
60
|
+
puts "found #{match_count} putative correspondences"
|
61
|
+
|
62
|
+
points1 = []
|
63
|
+
points2 = []
|
64
|
+
kp2.size.times { |j|
|
65
|
+
i = results[j][0].to_i
|
66
|
+
if (j == reverse_lookup[i])
|
67
|
+
points1 << kp1[i].pt
|
68
|
+
points2 << kp2[j].pt
|
69
|
+
end
|
70
|
+
}
|
71
|
+
|
72
|
+
width = img1.cols + img2.cols
|
73
|
+
height = (img1.rows > img2.rows) ? img1.rows : img2.rows
|
74
|
+
correspond = IplImage.new(width, height, :cv8u, 1);
|
75
|
+
correspond.set_roi(CvRect.new(0, 0, img1.cols, img1.rows))
|
76
|
+
img1.copy(correspond)
|
77
|
+
correspond.set_roi(CvRect.new(img1.cols, 0, img1.cols + img2.cols, img2.rows))
|
78
|
+
img2.copy(correspond)
|
79
|
+
correspond.reset_roi
|
80
|
+
|
81
|
+
points1.zip(points2) { |pt1, pt2|
|
82
|
+
pt2.x += img1.cols
|
83
|
+
correspond.line!(pt1, pt2, :color => CvColor::White)
|
84
|
+
}
|
85
|
+
|
86
|
+
GUI::Window.new('Object Correspond').show correspond
|
87
|
+
GUI::wait_key
|
88
|
+
|
data/ext/opencv/cvcapture.cpp
CHANGED
@@ -187,21 +187,25 @@ VALUE
|
|
187
187
|
rb_retrieve(VALUE self)
|
188
188
|
{
|
189
189
|
VALUE image = Qnil;
|
190
|
+
IplImage *frame = NULL;
|
190
191
|
try {
|
191
|
-
|
192
|
-
if (!frame)
|
192
|
+
if (!(frame = cvRetrieveFrame(CVCAPTURE(self)))) {
|
193
193
|
return Qnil;
|
194
|
-
|
195
|
-
|
196
|
-
|
194
|
+
}
|
195
|
+
image = cIplImage::new_object(frame->width, frame->height,
|
196
|
+
CV_MAKETYPE(IPL2CV_DEPTH(frame->depth), frame->nChannels));
|
197
|
+
if (frame->origin == IPL_ORIGIN_TL) {
|
197
198
|
cvCopy(frame, CVARR(image));
|
198
|
-
|
199
|
+
}
|
200
|
+
else {
|
199
201
|
cvFlip(frame, CVARR(image));
|
202
|
+
}
|
200
203
|
}
|
201
204
|
catch (cv::Exception& e) {
|
202
205
|
raise_cverror(e);
|
203
206
|
}
|
204
207
|
return image;
|
208
|
+
|
205
209
|
}
|
206
210
|
|
207
211
|
/*
|
@@ -214,16 +218,19 @@ VALUE
|
|
214
218
|
rb_query(VALUE self)
|
215
219
|
{
|
216
220
|
VALUE image = Qnil;
|
221
|
+
IplImage *frame = NULL;
|
217
222
|
try {
|
218
|
-
|
219
|
-
if (!frame)
|
223
|
+
if (!(frame = cvQueryFrame(CVCAPTURE(self)))) {
|
220
224
|
return Qnil;
|
221
|
-
|
222
|
-
|
223
|
-
|
225
|
+
}
|
226
|
+
image = cIplImage::new_object(frame->width, frame->height,
|
227
|
+
CV_MAKETYPE(IPL2CV_DEPTH(frame->depth), frame->nChannels));
|
228
|
+
if (frame->origin == IPL_ORIGIN_TL) {
|
224
229
|
cvCopy(frame, CVARR(image));
|
225
|
-
|
230
|
+
}
|
231
|
+
else {
|
226
232
|
cvFlip(frame, CVARR(image));
|
233
|
+
}
|
227
234
|
}
|
228
235
|
catch (cv::Exception& e) {
|
229
236
|
raise_cverror(e);
|
data/ext/opencv/cvutils.cpp
CHANGED
@@ -1,194 +1,192 @@
|
|
1
|
-
/************************************************************
|
2
|
-
|
3
|
-
cvutils.cpp -
|
4
|
-
|
5
|
-
$Author: ser1zw $
|
6
|
-
|
7
|
-
Copyright (C) 2011 ser1zw
|
8
|
-
|
9
|
-
************************************************************/
|
10
|
-
#include "cvutils.h"
|
11
|
-
|
12
|
-
void
|
13
|
-
raise_typeerror(VALUE object, VALUE expected_class)
|
14
|
-
{
|
15
|
-
raise_typeerror(object, rb_class2name(expected_class));
|
16
|
-
}
|
17
|
-
|
18
|
-
void
|
19
|
-
raise_typeerror(VALUE object, const char* expected_class_name)
|
20
|
-
{
|
21
|
-
rb_raise(rb_eTypeError, "wrong argument type %s (expected %s)",
|
22
|
-
rb_obj_classname(object), expected_class_name);
|
23
|
-
}
|
24
|
-
|
25
|
-
void
|
26
|
-
raise_compatible_typeerror(VALUE object, VALUE expected_class)
|
27
|
-
{
|
28
|
-
raise_compatible_typeerror(object, rb_class2name(expected_class));
|
29
|
-
}
|
30
|
-
|
31
|
-
void
|
32
|
-
raise_compatible_typeerror(VALUE object, const char* expected_class_name)
|
33
|
-
{
|
34
|
-
rb_raise(rb_eTypeError, "wrong argument type %s (expected %s or compatible object)",
|
35
|
-
rb_obj_classname(object), expected_class_name);
|
36
|
-
}
|
37
|
-
|
38
|
-
/*
|
39
|
-
* Allocates a memory buffer
|
40
|
-
*
|
41
|
-
*/
|
42
|
-
void*
|
43
|
-
|
44
|
-
{
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
}
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
CvMat*
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
}
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
}
|
93
|
-
return
|
94
|
-
}
|
95
|
-
|
96
|
-
/*
|
97
|
-
* Create IplImage header and allocate underlying data
|
98
|
-
* When memory allocation is failed, run GC and retry it
|
99
|
-
*/
|
100
|
-
IplImage*
|
101
|
-
rb_cvCreateImage(CvSize size, int depth, int channels)
|
102
|
-
{
|
103
|
-
IplImage* ptr = NULL;
|
104
|
-
try {
|
105
|
-
ptr =
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
{
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
rb_raise(
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
{
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
rb_raise(
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
return table;
|
191
|
-
|
192
|
-
|
193
|
-
}
|
194
|
-
|
1
|
+
/************************************************************
|
2
|
+
|
3
|
+
cvutils.cpp -
|
4
|
+
|
5
|
+
$Author: ser1zw $
|
6
|
+
|
7
|
+
Copyright (C) 2011 ser1zw
|
8
|
+
|
9
|
+
************************************************************/
|
10
|
+
#include "cvutils.h"
|
11
|
+
|
12
|
+
void
|
13
|
+
raise_typeerror(VALUE object, VALUE expected_class)
|
14
|
+
{
|
15
|
+
raise_typeerror(object, rb_class2name(expected_class));
|
16
|
+
}
|
17
|
+
|
18
|
+
void
|
19
|
+
raise_typeerror(VALUE object, const char* expected_class_name)
|
20
|
+
{
|
21
|
+
rb_raise(rb_eTypeError, "wrong argument type %s (expected %s)",
|
22
|
+
rb_obj_classname(object), expected_class_name);
|
23
|
+
}
|
24
|
+
|
25
|
+
void
|
26
|
+
raise_compatible_typeerror(VALUE object, VALUE expected_class)
|
27
|
+
{
|
28
|
+
raise_compatible_typeerror(object, rb_class2name(expected_class));
|
29
|
+
}
|
30
|
+
|
31
|
+
void
|
32
|
+
raise_compatible_typeerror(VALUE object, const char* expected_class_name)
|
33
|
+
{
|
34
|
+
rb_raise(rb_eTypeError, "wrong argument type %s (expected %s or compatible object)",
|
35
|
+
rb_obj_classname(object), expected_class_name);
|
36
|
+
}
|
37
|
+
|
38
|
+
/*
|
39
|
+
* Allocates a memory buffer
|
40
|
+
* see cv::fastMalloc()
|
41
|
+
*/
|
42
|
+
void*
|
43
|
+
rbFastMalloc(size_t size)
|
44
|
+
{
|
45
|
+
uchar* udata = (uchar*)xmalloc(size + sizeof(void*) + CV_MALLOC_ALIGN);
|
46
|
+
if(!udata) {
|
47
|
+
rb_raise(rb_eNoMemError, "Failed to allocate memory");
|
48
|
+
}
|
49
|
+
uchar** adata = cv::alignPtr((uchar**)udata + 1, CV_MALLOC_ALIGN);
|
50
|
+
adata[-1] = udata;
|
51
|
+
return adata;
|
52
|
+
}
|
53
|
+
|
54
|
+
/*
|
55
|
+
* Allocates a memory buffer
|
56
|
+
* When memory allocation is failed, run GC and retry it
|
57
|
+
*/
|
58
|
+
void*
|
59
|
+
rb_cvAlloc(size_t size)
|
60
|
+
{
|
61
|
+
return rbFastMalloc(size);
|
62
|
+
}
|
63
|
+
|
64
|
+
/*
|
65
|
+
* Creates CvMat and underlying data
|
66
|
+
* When memory allocation is failed, run GC and retry it
|
67
|
+
*/
|
68
|
+
CvMat*
|
69
|
+
rb_cvCreateMat(int rows, int cols, int type)
|
70
|
+
{
|
71
|
+
CvMat* mat = NULL;
|
72
|
+
try {
|
73
|
+
mat = cvCreateMatHeader(rows, cols, type);
|
74
|
+
if (mat) {
|
75
|
+
// see OpenCV's cvCreateData()
|
76
|
+
size_t step = mat->step;
|
77
|
+
size_t total_size = step * mat->rows + sizeof(int) + CV_MALLOC_ALIGN;
|
78
|
+
|
79
|
+
mat->refcount = (int*)rbFastMalloc(total_size);
|
80
|
+
mat->data.ptr = (uchar*)cvAlignPtr(mat->refcount + 1, CV_MALLOC_ALIGN);
|
81
|
+
*mat->refcount = 1;
|
82
|
+
}
|
83
|
+
else {
|
84
|
+
rb_raise(rb_eRuntimeError, "Failed to create mat header");
|
85
|
+
}
|
86
|
+
}
|
87
|
+
catch(cv::Exception& e) {
|
88
|
+
if (mat) {
|
89
|
+
cvReleaseMat(&mat);
|
90
|
+
}
|
91
|
+
rb_raise(rb_eRuntimeError, "%s", e.what());
|
92
|
+
}
|
93
|
+
return mat;
|
94
|
+
}
|
95
|
+
|
96
|
+
/*
|
97
|
+
* Create IplImage header and allocate underlying data
|
98
|
+
* When memory allocation is failed, run GC and retry it
|
99
|
+
*/
|
100
|
+
IplImage*
|
101
|
+
rb_cvCreateImage(CvSize size, int depth, int channels)
|
102
|
+
{
|
103
|
+
IplImage* ptr = NULL;
|
104
|
+
try {
|
105
|
+
ptr = cvCreateImageHeader(size, depth, channels);
|
106
|
+
if (ptr) {
|
107
|
+
// see OpenCV's cvCreateData()
|
108
|
+
ptr->imageData = ptr->imageDataOrigin = (char*)rbFastMalloc((size_t)ptr->imageSize);
|
109
|
+
}
|
110
|
+
else {
|
111
|
+
rb_raise(rb_eRuntimeError, "Failed to create image header");
|
112
|
+
}
|
113
|
+
}
|
114
|
+
catch(cv::Exception& e) {
|
115
|
+
if (ptr) {
|
116
|
+
cvReleaseImage(&ptr);
|
117
|
+
}
|
118
|
+
rb_raise(rb_eRuntimeError, "%s", e.what());
|
119
|
+
}
|
120
|
+
return ptr;
|
121
|
+
}
|
122
|
+
|
123
|
+
/*
|
124
|
+
* Creates a structuring element
|
125
|
+
* When memory allocation is failed, run GC and retry it
|
126
|
+
*/
|
127
|
+
IplConvKernel*
|
128
|
+
rb_cvCreateStructuringElementEx(int cols, int rows,
|
129
|
+
int anchorX, int anchorY,
|
130
|
+
int shape, int *values)
|
131
|
+
{
|
132
|
+
IplConvKernel* ptr = NULL;
|
133
|
+
try {
|
134
|
+
ptr = cvCreateStructuringElementEx(cols, rows, anchorX, anchorY, shape, values);
|
135
|
+
}
|
136
|
+
catch(cv::Exception& e) {
|
137
|
+
if (e.code != CV_StsNoMem)
|
138
|
+
rb_raise(rb_eRuntimeError, "%s", e.what());
|
139
|
+
|
140
|
+
rb_gc_start();
|
141
|
+
try {
|
142
|
+
ptr = cvCreateStructuringElementEx(cols, rows, anchorX, anchorY, shape, values);
|
143
|
+
}
|
144
|
+
catch (cv::Exception& e) {
|
145
|
+
if (e.code == CV_StsNoMem)
|
146
|
+
rb_raise(rb_eNoMemError, "%s", e.what());
|
147
|
+
else
|
148
|
+
rb_raise(rb_eRuntimeError, "%s", e.what());
|
149
|
+
}
|
150
|
+
}
|
151
|
+
return ptr;
|
152
|
+
}
|
153
|
+
|
154
|
+
/*
|
155
|
+
* Creates memory storage
|
156
|
+
* When memory allocation is failed, run GC and retry it
|
157
|
+
*/
|
158
|
+
CvMemStorage*
|
159
|
+
rb_cvCreateMemStorage(int block_size)
|
160
|
+
{
|
161
|
+
CvMemStorage* ptr = NULL;
|
162
|
+
try {
|
163
|
+
ptr = cvCreateMemStorage(block_size);
|
164
|
+
}
|
165
|
+
catch(cv::Exception& e) {
|
166
|
+
if (e.code != CV_StsNoMem)
|
167
|
+
rb_raise(rb_eRuntimeError, "%s", e.what());
|
168
|
+
|
169
|
+
rb_gc_start();
|
170
|
+
try {
|
171
|
+
ptr = cvCreateMemStorage(block_size);
|
172
|
+
}
|
173
|
+
catch (cv::Exception& e) {
|
174
|
+
if (e.code == CV_StsNoMem)
|
175
|
+
rb_raise(rb_eNoMemError, "%s", e.what());
|
176
|
+
else
|
177
|
+
rb_raise(rb_eRuntimeError, "%s", e.what());
|
178
|
+
}
|
179
|
+
}
|
180
|
+
return ptr;
|
181
|
+
}
|
182
|
+
|
183
|
+
VALUE
|
184
|
+
rb_get_option_table(VALUE klass, const char* table_name, VALUE option)
|
185
|
+
{
|
186
|
+
VALUE table = rb_const_get(klass, rb_intern(table_name));
|
187
|
+
if (NIL_P(option))
|
188
|
+
return table;
|
189
|
+
else
|
190
|
+
return rb_funcall(table, rb_intern("merge"), 1, option);
|
191
|
+
}
|
192
|
+
|