diff --git a/ext/opencv/cvmat.cpp b/ext/opencv/cvmat.cpp index 3bb801b..cfc3d2a 100644 --- a/ext/opencv/cvmat.cpp +++ b/ext/opencv/cvmat.cpp @@ -4749,16 +4749,14 @@ rb_match_template(int argc, VALUE *argv, VALUE self) else method_flag = CVMETHOD("MATCH_TEMPLATE_METHOD", method); - if (!(rb_obj_is_kind_of(templ, cCvMat::rb_class()))) - rb_raise(rb_eTypeError, "argument 1 (template) should be %s.", rb_class2name(cCvMat::rb_class())); - if (cvGetElemType(CVARR(self)) != cvGetElemType(CVARR(templ))) - rb_raise(rb_eTypeError, "template should be same type of self."); - CvSize src_size = cvGetSize(CVARR(self)); - CvSize template_size = cvGetSize(CVARR(templ)); + CvArr* self_ptr = CVARR(self); + CvMat* templ_ptr = CVMAT_WITH_CHECK(templ); + CvSize src_size = cvGetSize(self_ptr); + CvSize template_size = cvGetSize(templ_ptr); result = cCvMat::new_object(src_size.height - template_size.height + 1, src_size.width - template_size.width + 1, CV_32FC1); - cvMatchTemplate(CVARR(self), CVARR(templ), CVARR(result), method_flag); + cvMatchTemplate(self_ptr, templ_ptr, CVARR(result), method_flag); return result; } @@ -4875,7 +4873,8 @@ rb_cam_shift(VALUE self, VALUE window, VALUE criteria) VALUE comp, box; comp = cCvConnectedComp::new_object(); box = cCvBox2D::new_object(); - cvCamShift(CVARR(self), VALUE_TO_CVRECT(window), VALUE_TO_CVTERMCRITERIA(criteria), CVCONNECTEDCOMP(comp), CVBOX2D(box)); + cvCamShift(CVARR(self), VALUE_TO_CVRECT(window), VALUE_TO_CVTERMCRITERIA(criteria), + CVCONNECTEDCOMP(comp), CVBOX2D(box)); return rb_ary_new3(2, comp, box); } @@ -4989,18 +4988,19 @@ rb_optical_flow_hs(int argc, VALUE *argv, VALUE self) int use_previous = 0; rb_scan_args(argc, argv, "13", &prev, &velx, &vely, &options); options = OPTICAL_FLOW_HS_OPTION(options); - if (!rb_obj_is_kind_of(prev, cCvMat::rb_class())) - rb_raise(rb_eTypeError, "argument 1 (previous image) should be %s", rb_class2name(cCvMat::rb_class())); + CvMat *velx_ptr, *vely_ptr; if (NIL_P(velx) && NIL_P(vely)) { velx = cCvMat::new_object(cvGetSize(CVARR(self)), CV_MAKETYPE(CV_32F, 1)); vely = cCvMat::new_object(cvGetSize(CVARR(self)), CV_MAKETYPE(CV_32F, 1)); - } else { - if (rb_obj_is_kind_of(velx, cCvMat::rb_class()) && rb_obj_is_kind_of(vely, cCvMat::rb_class())) - use_previous = 1; - else - rb_raise(rb_eArgError, "Necessary to give both argument 2(previous velocity field x) and argument 3(previous velocity field y)"); + velx_ptr = CVMAT(velx); + vely_ptr = CVMAT(vely); } - cvCalcOpticalFlowHS(CVARR(prev), CVARR(self), use_previous, CVARR(velx), CVARR(vely), + else { + use_previous = 1; + velx_ptr = CVMAT_WITH_CHECK(velx); + vely_ptr = CVMAT_WITH_CHECK(vely); + } + cvCalcOpticalFlowHS(CVMAT_WITH_CHECK(prev), CVARR(self), use_previous, velx_ptr, vely_ptr, HS_LAMBDA(options), HS_CRITERIA(options)); return rb_ary_new3(2, velx, vely); } @@ -5019,11 +5019,10 @@ rb_optical_flow_lk(VALUE self, VALUE prev, VALUE win_size) { SUPPORT_8UC1_ONLY(self); VALUE velx, vely; - if (!rb_obj_is_kind_of(prev, cCvMat::rb_class())) - rb_raise(rb_eTypeError, "argument 1 (previous image) should be %s", rb_class2name(cCvMat::rb_class())); velx = cCvMat::new_object(cvGetSize(CVARR(self)), CV_MAKETYPE(CV_32F, 1)); vely = cCvMat::new_object(cvGetSize(CVARR(self)), CV_MAKETYPE(CV_32F, 1)); - cvCalcOpticalFlowLK(CVARR(prev), CVARR(self), VALUE_TO_CVSIZE(win_size), CVARR(velx), CVARR(vely)); + cvCalcOpticalFlowLK(CVMAT_WITH_CHECK(prev), CVARR(self), VALUE_TO_CVSIZE(win_size), + CVARR(velx), CVARR(vely)); return rb_ary_new3(2, velx, vely); } @@ -5054,31 +5053,61 @@ rb_optical_flow_lk(VALUE self, VALUE prev, VALUE win_size) VALUE rb_optical_flow_bm(int argc, VALUE *argv, VALUE self) { +// SUPPORT_8UC1_ONLY(self); +// VALUE prev, velx, vely, options; +// int use_previous = 0; +// rb_scan_args(argc, argv, "13", &prev, &velx, &vely, &options); +// options = OPTICAL_FLOW_BM_OPTION(options); +// CvSize +// image_size = cvGetSize(CVARR(self)), +// block_size = BM_BLOCK_SIZE(options), +// shift_size = BM_SHIFT_SIZE(options), +// max_range = BM_MAX_RANGE(options), +// velocity_size = cvSize((image_size.width - block_size.width) / shift_size.width, +// (image_size.height - block_size.height) / shift_size.height); +// if (NIL_P(velx) && NIL_P(vely)) { +// velx = cCvMat::new_object(velocity_size, CV_MAKETYPE(CV_32F, 1)); +// vely = cCvMat::new_object(velocity_size, CV_MAKETYPE(CV_32F, 1)); +// } +// else { +// if (rb_obj_is_kind_of(velx, cCvMat::rb_class()) && rb_obj_is_kind_of(vely, cCvMat::rb_class())) +// use_previous = 1; +// else +// rb_raise(rb_eArgError, "Necessary to give both argument 2(previous velocity field x) and argument 3(previous velocity field y)"); +// } +// cvCalcOpticalFlowBM(CVARR(prev), CVARR(self), +// block_size, shift_size, max_range, use_previous, +// CVARR(velx), CVARR(vely)); +// return rb_ary_new3(2, velx, vely); + SUPPORT_8UC1_ONLY(self); VALUE prev, velx, vely, options; int use_previous = 0; rb_scan_args(argc, argv, "13", &prev, &velx, &vely, &options); options = OPTICAL_FLOW_BM_OPTION(options); + CvArr* self_ptr = CVARR(self); CvSize - image_size = cvGetSize(CVARR(self)), + image_size = cvGetSize(self_ptr), block_size = BM_BLOCK_SIZE(options), shift_size = BM_SHIFT_SIZE(options), max_range = BM_MAX_RANGE(options), velocity_size = cvSize((image_size.width - block_size.width) / shift_size.width, - (image_size.height - block_size.height) / shift_size.height); + (image_size.height - block_size.height) / shift_size.height); + CvMat *velx_ptr, *vely_ptr; if (NIL_P(velx) && NIL_P(vely)) { velx = cCvMat::new_object(velocity_size, CV_MAKETYPE(CV_32F, 1)); vely = cCvMat::new_object(velocity_size, CV_MAKETYPE(CV_32F, 1)); + velx_ptr = CVMAT(velx); + vely_ptr = CVMAT(vely); } else { - if (rb_obj_is_kind_of(velx, cCvMat::rb_class()) && rb_obj_is_kind_of(vely, cCvMat::rb_class())) - use_previous = 1; - else - rb_raise(rb_eArgError, "Necessary to give both argument 2(previous velocity field x) and argument 3(previous velocity field y)"); + use_previous = 1; + velx_ptr = CVMAT_WITH_CHECK(velx); + vely_ptr = CVMAT_WITH_CHECK(vely); } - cvCalcOpticalFlowBM(CVARR(prev), CVARR(self), - block_size, shift_size, max_range, use_previous, - CVARR(velx), CVARR(vely)); + cvCalcOpticalFlowBM(CVMAT_WITH_CHECK(prev), self_ptr, + block_size, shift_size, max_range, use_previous, + velx_ptr, vely_ptr); return rb_ary_new3(2, velx, vely); } @@ -5281,7 +5310,7 @@ rb_compute_correspond_epilines(VALUE klass, VALUE points, VALUE which_image, VAL rb_raise(rb_eArgError, "input points should 2xN, Nx2 or 3xN, Nx3 matrix(N >= 7)."); correspondent_lines = cCvMat::new_object(n, 3, CV_MAT_DEPTH(points_ptr->type)); - cvComputeCorrespondEpilines(points_ptr, FIX2INT(which_image), CVMAT_WITH_CHECK(fundamental_matrix), + cvComputeCorrespondEpilines(points_ptr, NUM2INT(which_image), CVMAT_WITH_CHECK(fundamental_matrix), CVMAT(correspondent_lines)); return correspondent_lines; } @@ -5302,35 +5331,10 @@ rb_extract_surf(int argc, VALUE *argv, VALUE self) rb_scan_args(argc, argv, "12", &_params, &_mask, &_keypoints); // Prepare arguments - if (!rb_obj_is_kind_of(_params, cCvSURFParams::rb_class())) { - rb_raise(rb_eTypeError, "wrong argument type %s (expected %s)", - rb_class2name(CLASS_OF(_params)), rb_class2name(cCvSURFParams::rb_class())); - } - CvSURFParams params = *CVSURFPARAMS(_params); - CvMat* mask; - if (NIL_P(_mask)) - mask = NULL; - else { - if (!rb_obj_is_kind_of(_mask, cCvMat::rb_class())) { - rb_raise(rb_eTypeError, "wrong argument type %s (expected %s)", - rb_class2name(CLASS_OF(_mask)), rb_class2name(cCvMat::rb_class())); - } - mask = CVMAT(_mask); - } - CvSeq* keypoints; - if (NIL_P(_keypoints)) { - keypoints = NULL; - } - else { - if (!rb_obj_is_kind_of(_keypoints, cCvSeq::rb_class())) { - rb_raise(rb_eTypeError, "wrong argument type %s (expected %s)", - rb_class2name(CLASS_OF(_keypoints)), rb_class2name(cCvSeq::rb_class())); - } - keypoints = CVSEQ(_keypoints); - } - + CvSURFParams params = *CVSURFPARAMS_WITH_CHECK(_params); + CvMat* mask = MASK(_mask); + CvSeq* keypoints = NIL_P(_keypoints) ? NULL : CVSEQ_WITH_CHECK(_keypoints); int use_provided = (keypoints == NULL) ? 0 : 1; - VALUE storage = cCvMemStorage::new_object(); CvSeq* descriptors = NULL; @@ -5343,11 +5347,10 @@ rb_extract_surf(int argc, VALUE *argv, VALUE self) const int DIM_SIZE = (params.extended) ? 128 : 64; const int NUM_KEYPOINTS = keypoints->total; VALUE _descriptors = rb_ary_new2(NUM_KEYPOINTS); - int m, n; - for (m = 0; m < NUM_KEYPOINTS; ++m) { + for (int m = 0; m < NUM_KEYPOINTS; ++m) { VALUE elem = rb_ary_new2(DIM_SIZE); float *descriptor = (float*)cvGetSeqElem(descriptors, m); - for (n = 0; n < DIM_SIZE; ++n) { + for (int n = 0; n < DIM_SIZE; ++n) { rb_ary_store(elem, n, rb_float_new(descriptor[n])); } rb_ary_store(_descriptors, m, elem); diff --git a/ext/opencv/cvmat.h b/ext/opencv/cvmat.h index 6d8cd34..83b6d5a 100644 --- a/ext/opencv/cvmat.h +++ b/ext/opencv/cvmat.h @@ -299,12 +299,14 @@ MASK(VALUE object) { if (NIL_P(object)) return NULL; - else if (rb_obj_is_kind_of(object, cCvMat::rb_class()) && - CV_MAT_DEPTH(CVMAT(object)->type) == CV_8UC1 && - CV_MAT_CN(CVMAT(object)->type) == 1) - return CVMAT(object); - else - rb_raise(rb_eTypeError, "object is not a mask."); + else { + CvMat* obj_ptr = CVMAT_WITH_CHECK(object); + if (CV_MAT_DEPTH(obj_ptr->type) == CV_8UC1 && + CV_MAT_CN(obj_ptr->type) == 1) + return obj_ptr; + else + rb_raise(rb_eTypeError, "Mask should be 8bit 1-channel matrix."); + } } __NAMESPACE_END_OPENCV diff --git a/ext/opencv/cvseq.h b/ext/opencv/cvseq.h index 56d695f..2c39680 100644 --- a/ext/opencv/cvseq.h +++ b/ext/opencv/cvseq.h @@ -62,6 +62,14 @@ CVSEQ(VALUE object) return ptr; } +inline CvSeq* +CVSEQ_WITH_CHECK(VALUE object) +{ + if (!rb_obj_is_kind_of(object, cCvSeq::rb_class())) + raise_typeerror(object, cCvSeq::rb_class()); + return CVSEQ(object); +} + __NAMESPACE_END_OPENCV #endif // RUBY_OPENCV_CVSEQ_H diff --git a/ext/opencv/cvsurfparams.h b/ext/opencv/cvsurfparams.h index dd837fb..d6772e1 100644 --- a/ext/opencv/cvsurfparams.h +++ b/ext/opencv/cvsurfparams.h @@ -45,6 +45,14 @@ CVSURFPARAMS(VALUE object) return ptr; } +inline CvSURFParams* +CVSURFPARAMS_WITH_CHECK(VALUE object) +{ + if (!rb_obj_is_kind_of(object, cCvSURFParams::rb_class())) + raise_typeerror(object, cCvSURFParams::rb_class()); + return CVSURFPARAMS(object); +} + __NAMESPACE_END_OPENCV #endif // RUBY_OPENCV_CVSURFPARAMS_H diff --git a/ext/opencv/opencv.h b/ext/opencv/opencv.h index e91ab6e..098762c 100644 --- a/ext/opencv/opencv.h +++ b/ext/opencv/opencv.h @@ -261,16 +261,9 @@ CVMETHOD(const char *name, VALUE method, int ifnone = 0) method = rb_str_intern(method); case T_SYMBOL: value = rb_hash_aref(rb_const_get(rb_module_opencv(), rb_intern(name)), method); - if (NIL_P(value)) { - rb_warn("invalid opencv method type (see OpenCV::%s)", name); - return ifnone; - } - else { - return FIX2INT(value); - } + return NIL_P(value) ? ifnone : FIX2INT(value); default: - rb_raise(rb_eTypeError, "wrong argument type %s (expected %s)", - rb_class2name(CLASS_OF(method)), rb_class2name(rb_cSymbol)); + raise_typeerror(method, rb_cSymbol); } return ifnone; } diff --git a/ext/opencv/pointset.cpp b/ext/opencv/pointset.cpp index 558bdbe..3fa5f72 100644 --- a/ext/opencv/pointset.cpp +++ b/ext/opencv/pointset.cpp @@ -179,26 +179,33 @@ CVPOINTS_FROM_POINT_SET(VALUE object, CvPoint **pointset) VALUE storage; CvSeq *seq = 0; CvPoint2D32f p32; - if(rb_obj_is_kind_of(object, cCvSeq::rb_class())){ - if(CV_IS_SEQ_POINT_SET(CVSEQ(object))){ + if (rb_obj_is_kind_of(object, cCvSeq::rb_class())) { + if (CV_IS_SEQ_POINT_SET(CVSEQ(object))) { *pointset = (CvPoint*)cvCvtSeqToArray(CVSEQ(object), rb_cvAlloc(CVSEQ(object)->total * CVSEQ(object)->elem_size)); return CVSEQ(object)->total; - }else{ - rb_raise(rb_eTypeError, "sequence is not contain %s or %s.", + } + else { + rb_raise(rb_eTypeError, "sequence does not contain %s or %s.", rb_class2name(cCvPoint::rb_class()), rb_class2name(cCvPoint2D32f::rb_class())); } - }else if(rb_obj_is_kind_of(object, cCvMat::rb_class())){ + } + else if (rb_obj_is_kind_of(object, cCvMat::rb_class())) { /* to do */ rb_raise(rb_eNotImpError, "CvMat to CvSeq conversion not implemented."); - }else if(rb_obj_is_kind_of(object, rb_cArray)){ - *pointset = (CvPoint*)rb_cvAlloc(RARRAY_LEN(object) * sizeof(CvPoint)); - for(int i = 0; i < RARRAY_LEN(object); i++){ - (*pointset)[i].x = NUM2INT(rb_funcall(rb_ary_entry(object, i), rb_intern("x"), 0)); - (*pointset)[i].y = NUM2INT(rb_funcall(rb_ary_entry(object, i), rb_intern("y"), 0)); + } + else if (rb_obj_is_kind_of(object, rb_cArray)) { + int len = RARRAY_LEN(object); + *pointset = (CvPoint*)rb_cvAlloc(len * sizeof(CvPoint)); + ID id_x = rb_intern("x"); + ID id_y = rb_intern("y"); + for (int i = 0; i < len; ++i) { + (*pointset)[i].x = NUM2INT(rb_funcall(rb_ary_entry(object, i), id_x, 0)); + (*pointset)[i].y = NUM2INT(rb_funcall(rb_ary_entry(object, i), id_y, 0)); } - return RARRAY_LEN(object); - }else{ + return len; + } + else { rb_raise(rb_eTypeError, "Can't convert CvSeq(PointSet)."); } } diff --git a/test/test_cvmat.rb b/test/test_cvmat.rb index 73d1c50..fe1372e 100755 --- a/test/test_cvmat.rb +++ b/test/test_cvmat.rb @@ -2823,10 +2823,12 @@ class TestCvMat < OpenCVTestCase assert_raise(TypeError) { CvMat.compute_correspond_epilines(DUMMY_OBJ, 1, f_mat) } + assert_raise(TypeError) { + CvMat.compute_correspond_epilines(mat1, DUMMY_OBJ, f_mat) + } assert_raise(TypeError) { CvMat.compute_correspond_epilines(mat1, 1, DUMMY_OBJ) } - CvMat.compute_correspond_epilines(mat1, DUMMY_OBJ, f_mat) end end diff --git a/test/test_cvmat_imageprocessing.rb b/test/test_cvmat_imageprocessing.rb index 0a6a4a2..3086136 100755 --- a/test/test_cvmat_imageprocessing.rb +++ b/test/test_cvmat_imageprocessing.rb @@ -1841,6 +1841,14 @@ class TestCvMat_imageprocessing < OpenCVTestCase # pt2 = CvPoint.new(pt1.x + templ.width, pt1.y + templ.height) # mat.rectangle!(pt1, pt2, :color => CvColor::Black, :thickness => 3) # snap mat, templ, result + + + assert_raise(TypeError) { + mat.match_template(DUMMY_OBJ) + } + assert_raise(TypeError) { + mat.match_template(templ, DUMMY_OBJ) + } end def test_match_shapes @@ -1954,8 +1962,24 @@ class TestCvMat_imageprocessing < OpenCVTestCase # raise error assert_raise(TypeError) { - mat.snake_image(points, alpha, arr_beta, gamma, size, term_criteria) + mat.snake_image(DUMMY_OBJ, arr_alpha, arr_beta, arr_gamma, size, term_criteria) } + assert_raise(TypeError) { + mat.snake_image(points, DUMMY_OBJ, arr_beta, arr_gamma, size, term_criteria) + } + assert_raise(TypeError) { + mat.snake_image(points, arr_alpha, DUMMY_OBJ, arr_gamma, size, term_criteria) + } + assert_raise(TypeError) { + mat.snake_image(points, arr_alpha, arr_beta, DUMMY_OBJ, size, term_criteria) + } + assert_raise(TypeError) { + mat.snake_image(points, arr_alpha, arr_beta, arr_gamma, DUMMY_OBJ, term_criteria) + } + assert_raise(TypeError) { + mat.snake_image(points, arr_alpha, arr_beta, arr_gamma, size, DUMMY_OBJ) + } + mat.snake_image(points, arr_alpha, arr_beta, arr_gamma, size, term_criteria, DUMMY_OBJ) assert_raise(ArgumentError) { mat.snake_image(points, arr_alpha[0 .. num_points / 2], arr_beta, arr_gamma, size, term_criteria) @@ -1978,7 +2002,7 @@ class TestCvMat_imageprocessing < OpenCVTestCase CvColor::White end } - + [curr.optical_flow_hs(prev, nil, nil, :lambda => 0.0005, :criteria => CvTermCriteria.new(1, 0.001)), curr.optical_flow_hs(prev)].each { |velx, vely| assert_equal('d437cd896365c509b5d16fd5f2d7e498', hash_img(velx)) @@ -2007,19 +2031,16 @@ class TestCvMat_imageprocessing < OpenCVTestCase assert_equal('da33e266aece70ed69dcf074acd8fd4e', hash_img(vely)) assert_raise(TypeError) { - curr.optical_flow_hs('foobar') + curr.optical_flow_hs(DUMMY_OBJ) } - - assert_raise(ArgumentError) { - curr.optical_flow_hs(prev, 'foo', prev_vely) + assert_raise(TypeError) { + curr.optical_flow_hs(prev, DUMMY_OBJ, prev_vely) } - - assert_raise(ArgumentError) { - curr.optical_flow_hs(prev, prev_velx, 'bar') + assert_raise(TypeError) { + curr.optical_flow_hs(prev, prev_velx, DUMMY_OBJ) } - - assert_raise(ArgumentError) { - curr.optical_flow_hs(prev, 'foo', 'bar') + assert_raise(TypeError) { + curr.optical_flow_hs(prev, prev_velx, prev_vely, DUMMY_OBJ) } end @@ -2049,7 +2070,10 @@ class TestCvMat_imageprocessing < OpenCVTestCase assert_equal('e7524c292e95e374fdb588f0b516938e', hash_img(vely)) assert_raise(TypeError) { - curr.optical_flow_lk('foobar', CvSize.new(3, 3)) + curr.optical_flow_lk(DUMMY_OBJ, CvSize.new(3, 3)) + } + assert_raise(TypeError) { + curr.optical_flow_lk(prev, DUMMY_OBJ) } end @@ -2094,16 +2118,17 @@ class TestCvMat_imageprocessing < OpenCVTestCase assert_equal('6ad6b7a5c935379c0df4b9ec5666f3de', hash_img(velx)) assert_equal('b317b0b9d4fdb0e5cd40beb0dd4143b4', hash_img(vely)) - assert_raise(ArgumentError) { - curr.optical_flow_bm(prev, 'foo', prev_vely) + assert_raise(TypeError) { + curr.optical_flow_bm(DUMMY_OBJ) } - - assert_raise(ArgumentError) { - curr.optical_flow_bm(prev, prev_velx, 'bar') + assert_raise(TypeError) { + curr.optical_flow_bm(prev, DUMMY_OBJ, prev_vely) } - - assert_raise(ArgumentError) { - curr.optical_flow_bm(prev, 'foo', 'bar') + assert_raise(TypeError) { + curr.optical_flow_bm(prev, prev_velx, DUMMY_OBJ) + } + assert_raise(TypeError) { + curr.optical_flow_bm(prev, prev_velx, prev_vely, DUMMY_OBJ) } end @@ -2144,13 +2169,13 @@ class TestCvMat_imageprocessing < OpenCVTestCase # raise exceptions because of invalid arguments assert_raise(TypeError) { - mat0.extract_surf(CvMat.new(1, 1, :cv8u)) + mat0.extract_surf(DUMMY_OBJ) } assert_raise(TypeError) { - mat0.extract_surf(CvSURFParams.new(500), 'foobar') + mat0.extract_surf(CvSURFParams.new(500), DUMMY_OBJ) } assert_raise(TypeError) { - mat0.extract_surf(CvSURFParams.new(500), mask, mat0) + mat0.extract_surf(CvSURFParams.new(500), mask, DUMMY_OBJ) } ## Uncomment the following lines to show the result