mirror of
https://github.com/ruby-opencv/ruby-opencv
synced 2023-03-27 23:22:12 -04:00
added type check to some CvMat methods (7)
This commit is contained in:
parent
366e9e2bc0
commit
c003ee81f0
3 changed files with 163 additions and 28 deletions
|
@ -360,7 +360,7 @@ void define_ruby_class()
|
|||
rb_define_method(rb_klass, "flood_fill!", RUBY_METHOD_FUNC(rb_flood_fill_bang), -1);
|
||||
rb_define_method(rb_klass, "find_contours", RUBY_METHOD_FUNC(rb_find_contours), -1);
|
||||
rb_define_method(rb_klass, "find_contours!", RUBY_METHOD_FUNC(rb_find_contours_bang), -1);
|
||||
rb_define_method(rb_klass, "pyr_segmentation", RUBY_METHOD_FUNC(rb_pyr_segmentation), -1);
|
||||
rb_define_method(rb_klass, "pyr_segmentation", RUBY_METHOD_FUNC(rb_pyr_segmentation), 3);
|
||||
rb_define_method(rb_klass, "pyr_mean_shift_filtering", RUBY_METHOD_FUNC(rb_pyr_mean_shift_filtering), -1);
|
||||
rb_define_method(rb_klass, "watershed", RUBY_METHOD_FUNC(rb_watershed), 1);
|
||||
|
||||
|
@ -4116,16 +4116,16 @@ rb_threshold_to_zero_inverse(int argc, VALUE *argv, VALUE self)
|
|||
VALUE
|
||||
rb_pyr_down(int argc, VALUE *argv, VALUE self)
|
||||
{
|
||||
VALUE filter_type, dest;
|
||||
rb_scan_args(argc, argv, "01", &filter_type);
|
||||
VALUE dest;
|
||||
int filter = CV_GAUSSIAN_5x5;
|
||||
if (argc > 0) {
|
||||
VALUE filter_type = argv[0];
|
||||
switch (TYPE(filter_type)) {
|
||||
case T_SYMBOL:
|
||||
// currently suport CV_GAUSSIAN_5x5 only.
|
||||
break;
|
||||
default:
|
||||
rb_raise(rb_eArgError, "argument 1 (filter_type) should be Symbol.");
|
||||
raise_typeerror(filter_type, rb_cSymbol);
|
||||
}
|
||||
}
|
||||
CvSize original_size = cvGetSize(CVARR(self));
|
||||
|
@ -4159,7 +4159,7 @@ rb_pyr_up(int argc, VALUE *argv, VALUE self)
|
|||
// currently suport CV_GAUSSIAN_5x5 only.
|
||||
break;
|
||||
default:
|
||||
rb_raise(rb_eArgError, "argument 1 (filter_type) should be Symbol.");
|
||||
raise_typeerror(filter_type, rb_cSymbol);
|
||||
}
|
||||
}
|
||||
CvSize original_size = cvGetSize(CVARR(self));
|
||||
|
@ -4324,7 +4324,7 @@ rb_find_contours_bang(int argc, VALUE *argv, VALUE self)
|
|||
header_size = sizeof(CvContour);
|
||||
}
|
||||
storage = cCvMemStorage::new_object();
|
||||
if(cvFindContours(CVARR(self), CVMEMSTORAGE(storage),
|
||||
if (cvFindContours(CVARR(self), CVMEMSTORAGE(storage),
|
||||
&contour, header_size, mode, method, FC_OFFSET(find_contours_option)) == 0) {
|
||||
return Qnil;
|
||||
}
|
||||
|
@ -4348,20 +4348,19 @@ rb_find_contours_bang(int argc, VALUE *argv, VALUE self)
|
|||
* <b>support single-channel or 3-channel 8bit unsigned image only</b>
|
||||
*/
|
||||
VALUE
|
||||
rb_pyr_segmentation(int argc, VALUE *argv, VALUE self)
|
||||
rb_pyr_segmentation(VALUE self, VALUE level, VALUE threshold1, VALUE threshold2)
|
||||
{
|
||||
SUPPORT_8U_ONLY(self);
|
||||
SUPPORT_C1C3_ONLY(self);
|
||||
VALUE level, threshold1, threshold2, storage, dest;
|
||||
rb_scan_args(argc, argv, "30", &level, &threshold1, &threshold2);
|
||||
VALUE storage, dest;
|
||||
IplImage *src = IPLIMAGE(self);
|
||||
int l = FIX2INT(level);
|
||||
int l = NUM2INT(level);
|
||||
double t1 = NUM2DBL(threshold1), t2 = NUM2DBL(threshold2);
|
||||
CvRect roi = cvGetImageROI(src);
|
||||
if (l <= 0)
|
||||
rb_raise(rb_eArgError, "argument 1 (level) should be > 0.");
|
||||
if(((roi.width | roi.height) & ((1 << l) - 1)) != 0)
|
||||
rb_raise(rb_eArgError, "bad image size on level %d.", FIX2INT(level));
|
||||
rb_raise(rb_eArgError, "bad image size on level %d.", l);
|
||||
if (t1 < 0)
|
||||
rb_raise(rb_eArgError, "argument 2 (threshold for establishing the link) should be >= 0.");
|
||||
if (t2 < 0)
|
||||
|
@ -4375,7 +4374,7 @@ rb_pyr_segmentation(int argc, VALUE *argv, VALUE self)
|
|||
CVMEMSTORAGE(storage),
|
||||
&comp,
|
||||
l, t1, t2);
|
||||
if(!comp)
|
||||
if (!comp)
|
||||
comp = cvCreateSeq(CV_SEQ_CONNECTED_COMP, sizeof(CvSeq), sizeof(CvConnectedComp), CVMEMSTORAGE(storage));
|
||||
return rb_ary_new3(2, dest, cCvSeq::new_sequence(cCvSeq::rb_class(), comp, cCvConnectedComp::rb_class(), storage));
|
||||
}
|
||||
|
@ -4436,7 +4435,7 @@ rb_pyr_mean_shift_filtering(int argc, VALUE *argv, VALUE self)
|
|||
VALUE
|
||||
rb_watershed(VALUE self, VALUE markers)
|
||||
{
|
||||
cvWatershed(CVARR(self), CVARR(markers));
|
||||
cvWatershed(CVARR(self), CVMAT_WITH_CHECK(markers));
|
||||
return markers;
|
||||
}
|
||||
|
||||
|
@ -4449,12 +4448,12 @@ rb_watershed(VALUE self, VALUE markers)
|
|||
VALUE
|
||||
rb_moments(int argc, VALUE *argv, VALUE self)
|
||||
{
|
||||
VALUE is_binary, moments;
|
||||
VALUE is_binary;
|
||||
rb_scan_args(argc, argv, "01", &is_binary);
|
||||
IplImage image = *IPLIMAGE(self);
|
||||
int cn = CV_MAT_CN(cvGetElemType(CVARR(self)));
|
||||
moments = rb_ary_new();
|
||||
for(int i = 1; i <= cn; i++) {
|
||||
VALUE moments = rb_ary_new();
|
||||
for (int i = 1; i <= cn; ++i) {
|
||||
cvSetImageCOI(&image, i);
|
||||
rb_ary_push(moments, cCvMoments::new_object(&image, TRUE_OR_FALSE(is_binary, 0)));
|
||||
}
|
||||
|
@ -4493,6 +4492,8 @@ rb_hough_lines(int argc, VALUE *argv, VALUE self)
|
|||
VALUE method, rho, theta, threshold, p1, p2;
|
||||
rb_scan_args(argc, argv, "42", &method, &rho, &theta, &threshold, &p1, &p2);
|
||||
int method_flag = CVMETHOD("HOUGH_TRANSFORM_METHOD", method, INVALID_TYPE);
|
||||
if (method_flag == INVALID_TYPE)
|
||||
rb_raise(rb_eArgError, "Invalid method: %d", method_flag);
|
||||
VALUE storage = cCvMemStorage::new_object();
|
||||
CvSeq *seq = cvHoughLines2(CVARR(copy(self)), CVMEMSTORAGE(storage),
|
||||
method_flag, NUM2DBL(rho), NUM2DBL(theta), NUM2INT(threshold),
|
||||
|
@ -4506,7 +4507,6 @@ rb_hough_lines(int argc, VALUE *argv, VALUE self)
|
|||
return cCvSeq::new_sequence(cCvSeq::rb_class(), seq, cCvTwoPoints::rb_class(), storage);
|
||||
break;
|
||||
default:
|
||||
rb_raise(rb_eArgError, "Invalid method: %d", method_flag);
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -4593,6 +4593,8 @@ rb_hough_circles(int argc, VALUE *argv, VALUE self)
|
|||
&min_radius, &max_radius);
|
||||
storage = cCvMemStorage::new_object();
|
||||
int method_flag = CVMETHOD("HOUGH_TRANSFORM_METHOD", method, INVALID_TYPE);
|
||||
if (method_flag == INVALID_TYPE)
|
||||
rb_raise(rb_eArgError, "Invalid method: %d", method_flag);
|
||||
CvSeq *seq = cvHoughCircles(CVARR(self), CVMEMSTORAGE(storage),
|
||||
method_flag, NUM2DBL(dp), NUM2DBL(min_dist),
|
||||
NUM2DBL(param1), NUM2DBL(param2),
|
||||
|
@ -4635,10 +4637,10 @@ rb_inpaint(VALUE self, VALUE inpaint_method, VALUE mask, VALUE radius)
|
|||
const int INVALID_TYPE = -1;
|
||||
VALUE dest;
|
||||
int method = CVMETHOD("INPAINT_METHOD", inpaint_method, INVALID_TYPE);
|
||||
if (!(rb_obj_is_kind_of(mask, cCvMat::rb_class())) || cvGetElemType(CVARR(mask)) != CV_8UC1)
|
||||
rb_raise(rb_eTypeError, "argument 1 (mask) should be mask image.");
|
||||
if (method == INVALID_TYPE)
|
||||
rb_raise(rb_eArgError, "Invalid method");
|
||||
dest = new_mat_kind_object(cvGetSize(CVARR(self)), self);
|
||||
cvInpaint(CVARR(self), CVARR(mask), CVARR(dest), NUM2DBL(radius), method);
|
||||
cvInpaint(CVARR(self), MASK(mask), CVARR(dest), NUM2DBL(radius), method);
|
||||
return dest;
|
||||
}
|
||||
|
||||
|
|
|
@ -222,7 +222,7 @@ VALUE rb_flood_fill(int argc, VALUE *argv, VALUE self);
|
|||
VALUE rb_flood_fill_bang(int argc, VALUE *argv, VALUE self);
|
||||
VALUE rb_find_contours(int argc, VALUE *argv, VALUE self);
|
||||
VALUE rb_find_contours_bang(int argc, VALUE *argv, VALUE self);
|
||||
VALUE rb_pyr_segmentation(int argc, VALUE *argv, VALUE self);
|
||||
VALUE rb_pyr_segmentation(VALUE self, VALUE level, VALUE threshold1, VALUE threshold2);
|
||||
VALUE rb_pyr_mean_shift_filtering(int argc, VALUE *argv, VALUE self);
|
||||
VALUE rb_watershed(VALUE self, VALUE markers);
|
||||
|
||||
|
|
|
@ -1180,9 +1180,19 @@ class TestCvMat_imageprocessing < OpenCVTestCase
|
|||
0, 0, 0]
|
||||
test_proc.call(CV_THRESH_TOZERO_INV, :tozero_inv, expected, 4)
|
||||
|
||||
assert_raise(ArgumentError) {
|
||||
mat0.threshold(1, 2, :foobar)
|
||||
assert_raise(TypeError) {
|
||||
mat0.threshold(DUMMY_OBJ, 2, :binary)
|
||||
}
|
||||
assert_raise(TypeError) {
|
||||
mat0.threshold(1, DUMMY_OBJ, :binary)
|
||||
}
|
||||
assert_raise(TypeError) {
|
||||
mat0.threshold(1, 2, DUMMY_OBJ)
|
||||
}
|
||||
assert_raise(ArgumentError) {
|
||||
mat0.threshold(1, 2, :dummy)
|
||||
}
|
||||
mat0.threshold(1, 2, :binary, DUMMY_OBJ)
|
||||
end
|
||||
|
||||
def test_threshold_binary
|
||||
|
@ -1277,6 +1287,10 @@ class TestCvMat_imageprocessing < OpenCVTestCase
|
|||
|
||||
assert_equal('de9ff2ffcf8e43f28564a201cf90b7f4', hash_img(mat1))
|
||||
assert_equal('de9ff2ffcf8e43f28564a201cf90b7f4', hash_img(mat2))
|
||||
|
||||
assert_raise(TypeError) {
|
||||
mat0.pyr_down(DUMMY_OBJ)
|
||||
}
|
||||
end
|
||||
|
||||
def test_pyr_up
|
||||
|
@ -1286,6 +1300,10 @@ class TestCvMat_imageprocessing < OpenCVTestCase
|
|||
|
||||
assert_equal('02430c6cf143d3d104e25bc829f1fa93', hash_img(mat1))
|
||||
assert_equal('02430c6cf143d3d104e25bc829f1fa93', hash_img(mat2))
|
||||
|
||||
assert_raise(TypeError) {
|
||||
mat0.pyr_up(DUMMY_OBJ)
|
||||
}
|
||||
end
|
||||
|
||||
def test_flood_fill
|
||||
|
@ -1353,6 +1371,22 @@ class TestCvMat_imageprocessing < OpenCVTestCase
|
|||
assert_equal(96, comp5.rect.height)
|
||||
assert_cvscalar_equal(CvScalar.new(220, 0, 0, 0), comp5.value)
|
||||
assert_equal('33e01cdd72d7630e4231ffa63557da3e', hash_img(mask5))
|
||||
|
||||
assert_raise(TypeError) {
|
||||
mat0.flood_fill(DUMMY_OBJ, 0)
|
||||
}
|
||||
assert_raise(TypeError) {
|
||||
mat0.flood_fill(point, DUMMY_OBJ)
|
||||
}
|
||||
assert_raise(TypeError) {
|
||||
mat0.flood_fill(point, 0, DUMMY_OBJ)
|
||||
}
|
||||
assert_raise(TypeError) {
|
||||
mat0.flood_fill(point, 0, CvScalar.new(0), DUMMY_OBJ)
|
||||
}
|
||||
assert_raise(TypeError) {
|
||||
mat0.flood_fill(point, 0, CvScalar.new(0), CvScalar.new(64), DUMMY_OBJ)
|
||||
}
|
||||
end
|
||||
|
||||
def test_find_contours
|
||||
|
@ -1437,6 +1471,10 @@ class TestCvMat_imageprocessing < OpenCVTestCase
|
|||
contours = mat0.find_contours(:mode => CV_RETR_EXTERNAL, :method => CV_CHAIN_APPROX_TC89_KCOS)
|
||||
assert_equal(4, contours.total)
|
||||
assert_equal(4, contours.h_next.total)
|
||||
|
||||
assert_raise(TypeError) {
|
||||
mat0.find_contours(DUMMY_OBJ)
|
||||
}
|
||||
end
|
||||
|
||||
def test_pyr_segmentation
|
||||
|
@ -1450,18 +1488,49 @@ class TestCvMat_imageprocessing < OpenCVTestCase
|
|||
img2, seq2 = img0.pyr_segmentation(2, 255, 50)
|
||||
assert_equal('963b26f51b14f175fbbf128e9b9e979f', hash_img(img2))
|
||||
assert_equal(11, seq2.total)
|
||||
end
|
||||
|
||||
assert_raise(ArgumentError) {
|
||||
img0.pyr_segmentation(-1, 255, 50)
|
||||
}
|
||||
assert_raise(ArgumentError) {
|
||||
img0.pyr_segmentation(1000, 255, 50)
|
||||
}
|
||||
assert_raise(ArgumentError) {
|
||||
img0.pyr_segmentation(4, -1, 50)
|
||||
}
|
||||
assert_raise(ArgumentError) {
|
||||
img0.pyr_segmentation(4, 255, -1)
|
||||
}
|
||||
assert_raise(TypeError) {
|
||||
img0.pyr_segmentation(DUMMY_OBJ, 255, 50)
|
||||
}
|
||||
assert_raise(TypeError) {
|
||||
img0.pyr_segmentation(4, DUMMY_OBJ, 50)
|
||||
}
|
||||
assert_raise(TypeError) {
|
||||
img0.pyr_segmentation(4, 255, DUMMY_OBJ)
|
||||
}
|
||||
end
|
||||
|
||||
def test_pyr_mean_shift_filtering
|
||||
mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH)
|
||||
mat1 = mat0.pyr_mean_shift_filtering(30, 30)
|
||||
mat2 = mat0.pyr_mean_shift_filtering(30, 30, 2)
|
||||
mat3 = mat0.pyr_mean_shift_filtering(30, 30, nil, CvTermCriteria.new(3, 0.01))
|
||||
|
||||
|
||||
assert_equal('6887e96bc5dfd552f76ac5411b394775', hash_img(mat1))
|
||||
assert_equal('3cd9c4983fcabeafa04be200d5e08841', hash_img(mat2))
|
||||
assert_equal('e37f0157f93fe2a98312ae6b768e8295', hash_img(mat3))
|
||||
|
||||
assert_raise(TypeError) {
|
||||
mat0.pyr_mean_shift_filtering(DUMMY_OBJ, 30)
|
||||
}
|
||||
assert_raise(TypeError) {
|
||||
mat0.pyr_mean_shift_filtering(30, DUMMY_OBJ)
|
||||
}
|
||||
assert_raise(TypeError) {
|
||||
mat0.pyr_mean_shift_filtering(30, 30, 2, DUMMY_OBJ)
|
||||
}
|
||||
end
|
||||
|
||||
def test_watershed
|
||||
|
@ -1473,6 +1542,10 @@ class TestCvMat_imageprocessing < OpenCVTestCase
|
|||
|
||||
mat1 = mat0.watershed(marker)
|
||||
assert_equal('ee6bec03296039c8df1899d3edc4684e', hash_img(mat1))
|
||||
|
||||
assert_raise(TypeError) {
|
||||
mat0.watershed(DUMMY_OBJ)
|
||||
}
|
||||
end
|
||||
|
||||
def test_hough_lines
|
||||
|
@ -1493,9 +1566,31 @@ class TestCvMat_imageprocessing < OpenCVTestCase
|
|||
assert_equal(4, seq.size)
|
||||
}
|
||||
|
||||
[CV_HOUGH_MULTI_SCALE, :multi_scale].each { |method|
|
||||
seq = mat.hough_lines(method, 1, Math::PI / 180, 40, 2, 3)
|
||||
assert_equal(9, seq.size)
|
||||
# [CV_HOUGH_MULTI_SCALE, :multi_scale].each { |method|
|
||||
# seq = mat.hough_lines(method, 1, Math::PI / 180, 40, 2, 3)
|
||||
# assert_equal(9, seq.size)
|
||||
# }
|
||||
|
||||
assert_raise(TypeError) {
|
||||
mat.hough_lines(DUMMY_OBJ, 1, Math::PI / 180, 40, 2, 3)
|
||||
}
|
||||
assert_raise(TypeError) {
|
||||
mat.hough_lines(CV_HOUGH_STANDARD, DUMMY_OBJ, Math::PI / 180, 40, 2, 3)
|
||||
}
|
||||
assert_raise(TypeError) {
|
||||
mat.hough_lines(CV_HOUGH_STANDARD, 1, DUMMY_OBJ, 40, 2, 3)
|
||||
}
|
||||
assert_raise(TypeError) {
|
||||
mat.hough_lines(CV_HOUGH_STANDARD, 1, Math::PI / 180, DUMMY_OBJ, 2, 3)
|
||||
}
|
||||
assert_raise(TypeError) {
|
||||
mat.hough_lines(CV_HOUGH_STANDARD, 1, Math::PI / 180, 40, DUMMY_OBJ, 3)
|
||||
}
|
||||
assert_raise(TypeError) {
|
||||
mat.hough_lines(CV_HOUGH_STANDARD, 1, Math::PI / 180, 40, 2, DUMMY_OBJ)
|
||||
}
|
||||
assert_raise(ArgumentError) {
|
||||
mat.hough_lines(:dummy, 1, Math::PI / 180, 40, 2, DUMMY_OBJ)
|
||||
}
|
||||
end
|
||||
|
||||
|
@ -1591,6 +1686,31 @@ class TestCvMat_imageprocessing < OpenCVTestCase
|
|||
# mat0.circle!(circle.center, circle.radius, :color => CvColor::Red, :thickness => 2)
|
||||
# }
|
||||
# snap mat0
|
||||
|
||||
assert_raise(TypeError) {
|
||||
mat.hough_circles(DUMMY_OBJ, 1.5, 40, 100, 50, 10, 50)
|
||||
}
|
||||
assert_raise(TypeError) {
|
||||
mat.hough_circles(CV_HOUGH_GRADIENT, DUMMY_OBJ, 40, 100, 50, 10, 50)
|
||||
}
|
||||
assert_raise(TypeError) {
|
||||
mat.hough_circles(CV_HOUGH_GRADIENT, 1.5, DUMMY_OBJ, 100, 50, 10, 50)
|
||||
}
|
||||
assert_raise(TypeError) {
|
||||
mat.hough_circles(CV_HOUGH_GRADIENT, 1.5, 40, DUMMY_OBJ, 50, 10, 50)
|
||||
}
|
||||
assert_raise(TypeError) {
|
||||
mat.hough_circles(CV_HOUGH_GRADIENT, 1.5, 40, 100, DUMMY_OBJ, 10, 50)
|
||||
}
|
||||
assert_raise(TypeError) {
|
||||
mat.hough_circles(CV_HOUGH_GRADIENT, 1.5, 40, 100, 50, DUMMY_OBJ, 50)
|
||||
}
|
||||
assert_raise(TypeError) {
|
||||
mat.hough_circles(CV_HOUGH_GRADIENT, 1.5, 40, 100, 50, 10, DUMMY_OBJ)
|
||||
}
|
||||
assert_raise(ArgumentError) {
|
||||
mat.hough_circles(:dummy, 1.5, 40, 100, 50, 10, DUMMY_OBJ)
|
||||
}
|
||||
end
|
||||
|
||||
def test_hough_circles_gradient
|
||||
|
@ -1631,6 +1751,19 @@ class TestCvMat_imageprocessing < OpenCVTestCase
|
|||
# result_ns = mat.inpaint(:ns, mask, 10)
|
||||
# result_telea = mat.inpaint(:telea, mask, 10)
|
||||
# snap mat, result_ns, result_telea
|
||||
|
||||
assert_raise(TypeError) {
|
||||
mat.inpaint(DUMMY_OBJ, mask, 10)
|
||||
}
|
||||
assert_raise(TypeError) {
|
||||
mat.inpaint(:ns, DUMMY_OBJ, 10)
|
||||
}
|
||||
assert_raise(TypeError) {
|
||||
mat.inpaint(:ns, mask, DUMMY_OBJ)
|
||||
}
|
||||
assert_raise(ArgumentError) {
|
||||
mat.inpaint(:dummy, mask, 10)
|
||||
}
|
||||
end
|
||||
|
||||
def test_inpaint_ns
|
||||
|
|
Loading…
Add table
Reference in a new issue