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

modified CvMat#rect_sub_pix and CvMat#quadrangle_sub_pix, and added some tests

This commit is contained in:
ser1zw 2011-01-16 17:15:56 +09:00
parent 78f9362f89
commit fcae1a1d44
3 changed files with 202 additions and 152 deletions

View file

@ -320,8 +320,8 @@ void define_ruby_class()
rb_define_method(rb_klass, "good_features_to_track", RUBY_METHOD_FUNC(rb_good_features_to_track), -1);
rb_define_method(rb_klass, "sample_line", RUBY_METHOD_FUNC(rb_sample_line), 2);
rb_define_method(rb_klass, "rect_sub_pix", RUBY_METHOD_FUNC(rb_rect_sub_pix), 2);
rb_define_method(rb_klass, "quadrangle_sub_pix", RUBY_METHOD_FUNC(rb_quadrangle_sub_pix), 2);
rb_define_method(rb_klass, "rect_sub_pix", RUBY_METHOD_FUNC(rb_rect_sub_pix), -1);
rb_define_method(rb_klass, "quadrangle_sub_pix", RUBY_METHOD_FUNC(rb_quadrangle_sub_pix), -1);
rb_define_method(rb_klass, "resize", RUBY_METHOD_FUNC(rb_resize), -1);
rb_define_method(rb_klass, "warp_affine", RUBY_METHOD_FUNC(rb_warp_affine), -1);
rb_define_singleton_method(rb_klass, "rotation", RUBY_METHOD_FUNC(rb_rotation), 3);
@ -3266,7 +3266,7 @@ rb_sample_line(int argc, VALUE *argv, VALUE self)
/*
* call-seq:
* rect_sub_pix(<i>center,size</i>) -> cvmat
* rect_sub_pix(<i>center[, size = self.size]</i>) -> cvmat
*
* Retrieves pixel rectangle from image with sub-pixel accuracy.
* Extracts pixels from <i>self</i>.
@ -3277,26 +3277,41 @@ rb_sample_line(int argc, VALUE *argv, VALUE self)
* In this case, the replication border mode is used to get pixel values beyond the image boundaries.
*/
VALUE
rb_rect_sub_pix(VALUE self, VALUE center, VALUE size)
rb_rect_sub_pix(int argc, VALUE *argv, VALUE self)
{
VALUE dest = new_object(VALUE_TO_CVSIZE(size), cvGetElemType(CVARR(self)));
VALUE center, size;
CvSize _size;
if (rb_scan_args(argc, argv, "11", &center, &size) < 2)
_size = cvGetSize(CVARR(self));
else
_size = VALUE_TO_CVSIZE(size);
VALUE dest = new_object(_size, cvGetElemType(CVARR(self)));
cvGetRectSubPix(CVARR(self), CVARR(dest), VALUE_TO_CVPOINT2D32F(center));
return dest;
}
/*
* call-seq:
* quandrangle_sub_pix(<i>map_matrix</i>) -> cvmat
* quandrangle_sub_pix(<i>map_matrix[, size = self.size]</i>) -> cvmat
*
* Retrives pixel quadrangle from image with sub-pixel accuracy.
* Extracts pixel from <i>self</i> at sub-pixel accuracy and store them:
*/
VALUE
rb_quadrangle_sub_pix(VALUE self, VALUE map_matrix, VALUE size)
rb_quadrangle_sub_pix(int argc, VALUE *argv, VALUE self)
{
VALUE map_matrix, size;
CvSize _size;
if (rb_scan_args(argc, argv, "11", &map_matrix, &size) < 2)
_size = cvGetSize(CVARR(self));
else
_size = VALUE_TO_CVSIZE(size);
if (!rb_obj_is_kind_of(map_matrix, cCvMat::rb_class()))
rb_raise(rb_eTypeError, "argument 1 (map matrix) should be %s (2x3).", rb_class2name(cCvMat::rb_class()));
VALUE dest = new_object(VALUE_TO_CVSIZE(size), cvGetElemType(CVARR(self)));
VALUE dest = new_object(_size, cvGetElemType(CVARR(self)));
cvGetQuadrangleSubPix(CVARR(self), CVARR(dest), CVMAT(map_matrix));
return dest;
}

View file

@ -175,8 +175,8 @@ VALUE rbi_find_corner_sub_pix(int argc, VALUE *argv, VALUE self);
VALUE rb_good_features_to_track(int argc, VALUE *argv, VALUE self);
VALUE rb_sample_line(int argc, VALUE *argv, VALUE self);
VALUE rb_rect_sub_pix(VALUE self, VALUE center, VALUE size);
VALUE rb_quadrangle_sub_pix(VALUE self, VALUE map_matrix, VALUE size);
VALUE rb_rect_sub_pix(int argc, VALUE *argv, VALUE self);
VALUE rb_quadrangle_sub_pix(int argc, VALUE *argv, VALUE self);
VALUE rb_resize(int argc, VALUE *argv, VALUE self);
VALUE rb_warp_affine(int argc, VALUE *argv, VALUE self);
VALUE rb_rotation(VALUE self, VALUE center, VALUE angle, VALUE scale);

View file

@ -193,6 +193,41 @@ class TestCvMat_imageprocessing < OpenCVTestCase
mat0.good_features_to_track(0.2, 5, :max => 0)
}
end
def test_sample_line
flunk('FIXME: CvMat#sample_line is not implemented yet.')
end
def test_rect_sub_pix
mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH)
center = CvPoint2D32f.new(mat0.width / 2, mat0.height / 2)
mat1 = mat0.rect_sub_pix(center)
mat2 = mat0.rect_sub_pix(center, mat0.size)
mat3 = mat0.rect_sub_pix(center, CvSize.new(512, 512))
assert_equal('b3dc0e31260dd42b5341471e23e825d3', hash_img(mat1))
assert_equal('b3dc0e31260dd42b5341471e23e825d3', hash_img(mat2))
assert_equal('cc27ce8f4068efedcd31c4c782c3825c', hash_img(mat3))
end
def test_quadrangle_sub_pix
mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH)
angle = 60 * Math::PI / 180
map_matrix = CvMat.new(2, 3, :cv32f, 1)
map_matrix[0] = CvScalar.new(Math.cos(angle))
map_matrix[1] = CvScalar.new(-Math.sin(angle))
map_matrix[2] = CvScalar.new(mat0.width * 0.5)
map_matrix[3] = CvScalar.new(-map_matrix[1][0])
map_matrix[4] = map_matrix[0]
map_matrix[5] = CvScalar.new(mat0.height * 0.5)
mat1 = mat0.quadrangle_sub_pix(map_matrix)
mat2 = mat0.quadrangle_sub_pix(map_matrix, mat0.size)
mat3 = mat0.quadrangle_sub_pix(map_matrix, CvSize.new(512, 512))
assert_equal('f170c05fa50c3ac2a762d7b3f5c4ae2f', hash_img(mat1))
assert_equal('f170c05fa50c3ac2a762d7b3f5c4ae2f', hash_img(mat2))
assert_equal('4d949d5083405381ad9ea09dcd95e5a2', hash_img(mat3))
end
end