mirror of
https://github.com/ruby-opencv/ruby-opencv
synced 2023-03-27 23:22:12 -04:00
modified and tested CvMat#find_contours
This commit is contained in:
parent
f7234628f5
commit
505ab279b6
5 changed files with 142 additions and 9 deletions
|
@ -4210,29 +4210,27 @@ rb_find_contours_bang(int argc, VALUE *argv, VALUE self)
|
|||
SUPPORT_8UC1_ONLY(self);
|
||||
VALUE find_contours_option, klass, element_klass, storage;
|
||||
rb_scan_args(argc, argv, "01", &find_contours_option);
|
||||
CvSeq *contour = 0;
|
||||
CvSeq *contour = NULL;
|
||||
find_contours_option = FIND_CONTOURS_OPTION(find_contours_option);
|
||||
int mode = FC_MODE(find_contours_option);
|
||||
int method = FC_METHOD(find_contours_option);
|
||||
int header, header_size, element_size;
|
||||
int header_size, element_size;
|
||||
if (method == CV_CHAIN_CODE) {
|
||||
klass = cCvChain::rb_class();
|
||||
element_klass = cCvChainCode::rb_class();
|
||||
header = CV_SEQ_CHAIN_CONTOUR;
|
||||
header_size = sizeof(CvChain);
|
||||
element_size = sizeof(CvChainCode);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
klass = cCvContour::rb_class();
|
||||
element_klass = cCvPoint::rb_class();
|
||||
header = CV_SEQ_CONTOUR;
|
||||
header_size = sizeof(CvContour);
|
||||
element_size = sizeof(CvPoint);
|
||||
}
|
||||
storage = cCvMemStorage::new_object();
|
||||
if(cvFindContours(CVARR(self), CVMEMSTORAGE(storage), &contour, header, mode, method, FC_OFFSET(find_contours_option)) == 0)
|
||||
if(cvFindContours(CVARR(self), CVMEMSTORAGE(storage),
|
||||
&contour, header_size, mode, method, FC_OFFSET(find_contours_option)) == 0)
|
||||
return Qnil;
|
||||
if(!contour)
|
||||
contour = cvCreateSeq(header, header_size, element_size, CVMEMSTORAGE(storage));
|
||||
return cCvSeq::new_sequence(klass, contour, element_klass, storage);
|
||||
}
|
||||
|
||||
|
|
|
@ -215,6 +215,20 @@ define_ruby_module()
|
|||
rb_define_const(rb_module, "CV_THRESH_TOZERO_INV", INT2FIX(CV_THRESH_TOZERO_INV));
|
||||
rb_define_const(rb_module, "CV_THRESH_OTSU", INT2FIX(CV_THRESH_OTSU));
|
||||
|
||||
/* Retrieval mode */
|
||||
rb_define_const(rb_module, "CV_RETR_EXTERNAL", INT2FIX(CV_RETR_EXTERNAL));
|
||||
rb_define_const(rb_module, "CV_RETR_LIST", INT2FIX(CV_RETR_LIST));
|
||||
rb_define_const(rb_module, "CV_RETR_CCOMP", INT2FIX(CV_RETR_CCOMP));
|
||||
rb_define_const(rb_module, "CV_RETR_TREE", INT2FIX(CV_RETR_TREE));
|
||||
|
||||
/* Approximation method */
|
||||
rb_define_const(rb_module, "CV_CHAIN_CODE", INT2FIX(CV_CHAIN_CODE));
|
||||
rb_define_const(rb_module, "CV_CHAIN_APPROX_NONE", INT2FIX(CV_CHAIN_APPROX_NONE));
|
||||
rb_define_const(rb_module, "CV_CHAIN_APPROX_SIMPLE", INT2FIX(CV_CHAIN_APPROX_SIMPLE));
|
||||
rb_define_const(rb_module, "CV_CHAIN_APPROX_TC89_L1", INT2FIX(CV_CHAIN_APPROX_TC89_L1));
|
||||
rb_define_const(rb_module, "CV_CHAIN_APPROX_TC89_KCOS", INT2FIX(CV_CHAIN_APPROX_TC89_KCOS));
|
||||
rb_define_const(rb_module, "CV_LINK_RUNS", INT2FIX(CV_LINK_RUNS));
|
||||
|
||||
VALUE inversion_method = rb_hash_new();
|
||||
/* {:lu, :svd, :svd_sym(:svd_symmetric)}: Inversion method */
|
||||
rb_define_const(rb_module, "INVERSION_METHOD", inversion_method);
|
||||
|
|
BIN
test/samples/contours.jpg
Normal file
BIN
test/samples/contours.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.4 KiB |
|
@ -10,6 +10,7 @@ include OpenCV
|
|||
class TestCvMat_imageprocessing < OpenCVTestCase
|
||||
FILENAME_LENA256x256 = File.expand_path(File.dirname(__FILE__)) + '/samples/lena-256x256.jpg'
|
||||
FILENAME_LENA32x32 = File.expand_path(File.dirname(__FILE__)) + '/samples/lena-32x32.jpg'
|
||||
FILENAME_CONTOURS = File.expand_path(File.dirname(__FILE__)) + '/samples/contours.jpg'
|
||||
|
||||
def test_sobel
|
||||
mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_GRAYSCALE)
|
||||
|
@ -1153,5 +1154,110 @@ class TestCvMat_imageprocessing < OpenCVTestCase
|
|||
assert_cvscalar_equal(CvScalar.new(220, 0, 0, 0), comp5.value)
|
||||
assert_equal('33e01cdd72d7630e4231ffa63557da3e', hash_img(mask5))
|
||||
end
|
||||
|
||||
def test_find_contours
|
||||
mat0 = CvMat.load(FILENAME_CONTOURS, CV_LOAD_IMAGE_GRAYSCALE)
|
||||
|
||||
# Make binary image
|
||||
mat0.height.times { |j|
|
||||
mat0.width.times { |i|
|
||||
mat0[j, i] = (mat0[j, i][0] < 128) ? CvColor::Black : CvColor::White
|
||||
}
|
||||
}
|
||||
|
||||
[mat0.find_contours, mat0.find_contours(:mode => CV_RETR_LIST),
|
||||
mat0.find_contours(:method => CV_CHAIN_APPROX_SIMPLE),
|
||||
mat0.find_contours(:mode => CV_RETR_LIST, :method => CV_CHAIN_APPROX_SIMPLE)].each { |contours|
|
||||
assert_not_nil(contours)
|
||||
assert_equal(8, contours.total)
|
||||
assert_not_nil(contours.h_next)
|
||||
assert_equal(4, contours.h_next.total)
|
||||
assert_not_nil(contours.h_next.h_next)
|
||||
assert_equal(8, contours.h_next.h_next.total)
|
||||
assert_not_nil(contours.h_next.h_next.h_next)
|
||||
assert_equal(4, contours.h_next.h_next.h_next.total)
|
||||
assert_nil(contours.v_next)
|
||||
assert_nil(contours.h_next.v_next)
|
||||
assert_nil(contours.h_next.h_next.v_next)
|
||||
assert_nil(contours.h_next.h_next.h_next.v_next)
|
||||
}
|
||||
|
||||
contours = mat0.find_contours(:mode => CV_RETR_TREE)
|
||||
assert_not_nil(contours)
|
||||
assert_equal(4, contours.total)
|
||||
assert_not_nil(contours.v_next)
|
||||
assert_equal(8, contours.v_next.total)
|
||||
assert_nil(contours.v_next.v_next)
|
||||
assert_not_nil(contours.h_next)
|
||||
assert_equal(4, contours.h_next.total)
|
||||
assert_not_nil(contours.h_next.v_next)
|
||||
assert_equal(8, contours.h_next.v_next.total)
|
||||
assert_nil(contours.h_next.v_next.v_next)
|
||||
|
||||
contours = mat0.find_contours(:mode => CV_RETR_CCOMP)
|
||||
assert_not_nil(contours)
|
||||
assert_equal(4, contours.total)
|
||||
assert_not_nil(contours.v_next)
|
||||
assert_equal(8, contours.v_next.total)
|
||||
assert_nil(contours.v_next.v_next)
|
||||
assert_not_nil(contours.h_next)
|
||||
assert_equal(4, contours.h_next.total)
|
||||
assert_not_nil(contours.h_next.v_next)
|
||||
assert_equal(8, contours.h_next.v_next.total)
|
||||
assert_nil(contours.h_next.v_next.v_next)
|
||||
|
||||
contours = mat0.find_contours(:mode => CV_RETR_EXTERNAL)
|
||||
assert_not_nil(contours)
|
||||
assert_equal(4, contours.total)
|
||||
assert_nil(contours.v_next)
|
||||
assert_not_nil(contours.h_next)
|
||||
assert_equal(4, contours.h_next.total)
|
||||
assert_nil(contours.h_next.v_next)
|
||||
|
||||
contours = mat0.find_contours(:mode => CV_RETR_TREE, :method => CV_CHAIN_APPROX_NONE)
|
||||
assert_not_nil(contours)
|
||||
assert_equal(474, contours.total)
|
||||
assert_not_nil(contours.v_next)
|
||||
assert_equal(318, contours.v_next.total)
|
||||
assert_nil(contours.v_next.v_next)
|
||||
assert_not_nil(contours.h_next)
|
||||
assert_equal(396, contours.h_next.total)
|
||||
assert_not_nil(contours.h_next.v_next)
|
||||
assert_equal(240, contours.h_next.v_next.total)
|
||||
assert_nil(contours.h_next.v_next.v_next)
|
||||
|
||||
contours = mat0.find_contours(:mode => CV_RETR_EXTERNAL, :method => CV_CHAIN_CODE)
|
||||
assert_equal(474, contours.total)
|
||||
assert_equal(396, contours.h_next.total)
|
||||
|
||||
contours = mat0.find_contours(:mode => CV_RETR_EXTERNAL, :method => CV_CHAIN_APPROX_TC89_L1)
|
||||
assert_equal(4, contours.total)
|
||||
assert_equal(4, contours.h_next.total)
|
||||
|
||||
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)
|
||||
end
|
||||
|
||||
def show(mat0, contours)
|
||||
mat1 = mat0.clone.set_zero
|
||||
contours.each { |a|
|
||||
mat1[a.y, a.x] = CvColor::White
|
||||
}
|
||||
|
||||
contours.h_next.each { |a|
|
||||
mat1[a.y, a.x] = CvColor::White
|
||||
}
|
||||
|
||||
contours.h_next.h_next.each { |a|
|
||||
mat1[a.y, a.x] = CvColor::White
|
||||
}
|
||||
|
||||
contours.h_next.h_next.h_next.each { |a|
|
||||
mat1[a.y, a.x] = CvColor::White
|
||||
}
|
||||
|
||||
snap mat0, mat1
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -51,6 +51,21 @@ class TestOpenCV < OpenCVTestCase
|
|||
assert_equal(3, CV_THRESH_TOZERO)
|
||||
assert_equal(4, CV_THRESH_TOZERO_INV)
|
||||
assert_equal(8, CV_THRESH_OTSU)
|
||||
|
||||
# Retrieval mode
|
||||
assert_equal(0, CV_RETR_EXTERNAL)
|
||||
assert_equal(1, CV_RETR_LIST)
|
||||
assert_equal(2, CV_RETR_CCOMP)
|
||||
assert_equal(3, CV_RETR_TREE)
|
||||
|
||||
# Approximation method
|
||||
assert_equal(0, CV_CHAIN_CODE)
|
||||
assert_equal(1, CV_CHAIN_APPROX_NONE)
|
||||
assert_equal(2, CV_CHAIN_APPROX_SIMPLE)
|
||||
assert_equal(3, CV_CHAIN_APPROX_TC89_L1)
|
||||
assert_equal(4, CV_CHAIN_APPROX_TC89_KCOS)
|
||||
assert_equal(5, CV_LINK_RUNS)
|
||||
|
||||
end
|
||||
|
||||
def test_symbols
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue