1
0
Fork 0
mirror of https://github.com/ruby-opencv/ruby-opencv synced 2023-03-27 23:22:12 -04:00

fix IplImage#split to return Array<IplImage> (Issue #7)

https://github.com/ruby-opencv/ruby-opencv/issues/7
This commit is contained in:
ser1zw 2012-03-02 02:15:06 +09:00
parent 71b2743c76
commit e760e91d71
3 changed files with 53 additions and 11 deletions

View file

@ -1588,22 +1588,25 @@ rb_flip_bang(int argc, VALUE *argv, VALUE self)
VALUE
rb_split(VALUE self)
{
CvMat* self_ptr = CVMAT(self);
int type = self_ptr->type, depth = CV_MAT_DEPTH(type), channel = CV_MAT_CN(type);
CvMat *dest[] = { NULL, NULL, NULL, NULL };
CvArr* self_ptr = CVARR(self);
int type = cvGetElemType(self_ptr);
int depth = CV_MAT_DEPTH(type), channel = CV_MAT_CN(type);
VALUE dest = rb_ary_new2(channel);
try {
CvArr *dest_ptr[] = { NULL, NULL, NULL, NULL };
CvSize size = cvGetSize(self_ptr);
for (int i = 0; i < channel; ++i)
dest[i] = rb_cvCreateMat(size.height, size.width, CV_MAKETYPE(depth, 1));
cvSplit(self_ptr, dest[0], dest[1], dest[2], dest[3]);
for (int i = 0; i < channel; ++i) {
VALUE tmp = new_mat_kind_object(size, self, depth, 1);
rb_ary_store(dest, i, tmp);
dest_ptr[i] = CVARR(tmp);
}
cvSplit(self_ptr, dest_ptr[0], dest_ptr[1], dest_ptr[2], dest_ptr[3]);
}
catch (cv::Exception& e) {
raise_cverror(e);
}
VALUE ary = rb_ary_new2(channel);
for (int i = 0; i < channel; ++i)
rb_ary_store(ary, i, OPENCV_OBJECT(rb_klass, dest[i]));
return ary;
return dest;
}
/*

View file

@ -99,6 +99,19 @@ class OpenCVTestCase < Test::Unit::TestCase
m
end
def create_iplimage(width, height, depth = :cv8u, channel = 4, &block)
m = IplImage.new(width, height, depth, channel)
block = lambda { |j, i, c| CvScalar.new(*([c + 1] * channel)) } unless block_given?
count = 0
height.times { |j|
width.times { |i|
m[j, i] = block.call(j, i, count)
count += 1
}
}
m
end
def assert_each_cvscalar(actual, delta = 0, &block)
raise unless block_given?
count = 0

View file

@ -826,9 +826,14 @@ class TestCvMat < OpenCVTestCase
m0 = create_cvmat(2, 3, :cv8u, 3) { |j, i, c|
CvScalar.new(c * 10, c * 20, c * 30)
}
m0.split.each_with_index { |m, idx|
splitted = m0.split
assert_equal(m0.channel, splitted.size)
splitted.each_with_index { |m, idx|
assert_equal(CvMat, m.class)
assert_equal(m0.height, m.height)
assert_equal(m0.width, m.width)
assert_equal(1, m.channel)
c = 0
m0.height.times { |j|
@ -839,6 +844,27 @@ class TestCvMat < OpenCVTestCase
}
}
}
# IplImage#split should return Array<IplImage>
image = create_iplimage(2, 3, :cv8u, 3) { |j, i, c|
CvScalar.new(c * 10, c * 20, c * 30)
}
splitted = image.split
assert_equal(3, splitted.size)
splitted.each_with_index { |img, channel|
assert_equal(IplImage, img.class)
assert_equal(image.height, img.height)
assert_equal(image.width, img.width)
assert_equal(1, img.channel)
img.height.times { |j|
img.width.times { |i|
val = image[j, i][channel]
assert_cvscalar_equal(CvScalar.new(val), img[j, i])
}
}
}
end
def test_merge