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

modified and tested CvMat#find_contours and CvContour

This commit is contained in:
ser1zw 2011-02-14 04:40:25 +09:00
parent 505ab279b6
commit 9b304e424c
6 changed files with 95 additions and 53 deletions

1
.gitignore vendored
View file

@ -10,3 +10,4 @@ GRTAGS
GSYMS GSYMS
GTAGS GTAGS
OpenCV-2.2.0 OpenCV-2.2.0
ruby-1.9.2-p136

View file

@ -22,6 +22,9 @@ __NAMESPACE_BEGIN_CVCONTOUR
#define APPROX_POLY_ACCURACY(op) NUM2DBL(rb_hash_aref(op, ID2SYM(rb_intern("accuracy")))) #define APPROX_POLY_ACCURACY(op) NUM2DBL(rb_hash_aref(op, ID2SYM(rb_intern("accuracy"))))
#define APPROX_POLY_RECURSIVE(op) ({VALUE _recursive = rb_hash_aref(op, ID2SYM(rb_intern("recursive"))); NIL_P(_recursive) ? 0 : _recursive == Qfalse ? 0 : 1;}) #define APPROX_POLY_RECURSIVE(op) ({VALUE _recursive = rb_hash_aref(op, ID2SYM(rb_intern("recursive"))); NIL_P(_recursive) ? 0 : _recursive == Qfalse ? 0 : 1;})
VALUE rb_allocate(VALUE klass);
void cvcontour_free(void *ptr);
VALUE rb_klass; VALUE rb_klass;
VALUE VALUE
@ -51,7 +54,9 @@ define_ruby_class()
rb_klass = rb_define_class_under(opencv, "CvContour", cvseq); rb_klass = rb_define_class_under(opencv, "CvContour", cvseq);
rb_include_module(rb_klass, curve); rb_include_module(rb_klass, curve);
rb_include_module(rb_klass, pointset); rb_include_module(rb_klass, pointset);
rb_define_alloc_func(rb_klass, rb_allocate);
VALUE approx_option = rb_hash_new(); VALUE approx_option = rb_hash_new();
rb_define_const(rb_klass, "APPROX_OPTION", approx_option); rb_define_const(rb_klass, "APPROX_OPTION", approx_option);
rb_hash_aset(approx_option, ID2SYM(rb_intern("method")), INT2FIX(CV_POLY_APPROX_DP)); rb_hash_aset(approx_option, ID2SYM(rb_intern("method")), INT2FIX(CV_POLY_APPROX_DP));
@ -71,20 +76,37 @@ define_ruby_class()
rb_define_method(rb_klass, "measure_distance", RUBY_METHOD_FUNC(rb_measure_distance), 1); rb_define_method(rb_klass, "measure_distance", RUBY_METHOD_FUNC(rb_measure_distance), 1);
} }
VALUE
rb_allocate(VALUE klass)
{
CvContour *ptr = ALLOC(CvContour);
return Data_Wrap_Struct(klass, 0, cvcontour_free, ptr);
}
void
cvcontour_free(void *ptr)
{
if (ptr) {
CvContour *contour = (CvContour*)ptr;
if (contour->storage)
cvReleaseMemStorage(&(contour->storage));
}
}
VALUE VALUE
rb_initialize(int argc, VALUE *argv, VALUE self) rb_initialize(int argc, VALUE *argv, VALUE self)
{ {
/* CvMemStorage *storage;
VALUE storage; VALUE storage_value;
CvSeq *seq = 0; if (rb_scan_args(argc, argv, "01", &storage_value) > 0) {
rb_scan_args(argc, argv, "01", &storage); storage_value = CHECK_CVMEMSTORAGE(storage_value);
storage = CVMEMSTORAGE(storage_value);
}
else
storage = cvCreateMemStorage(0);
storage = CHECK_CVMEMSTORAGE(storage); DATA_PTR(self) = (CvContour*)cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvContour),
seq = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint), CVMEMSTORAGE(storage)); sizeof(CvPoint), storage);
DATA_PTR(self) = seq;
resist_root_object(seq, storage);
st_insert(cCvSeq::seqblock_klass, (st_data_t)seq, (st_data_t)klass);
*/
return self; return self;
} }
@ -205,12 +227,10 @@ rb_measure_distance(VALUE self, VALUE point)
return rb_float_new(cvPointPolygonTest(CVARR(self), VALUE_TO_CVPOINT2D32F(point), 1)); return rb_float_new(cvPointPolygonTest(CVARR(self), VALUE_TO_CVPOINT2D32F(point), 1));
} }
VALUE new_object() VALUE new_object()
{ {
VALUE storage = cCvMemStorage::new_object(); VALUE object = rb_allocate(rb_klass);
CvSeq *seq = cvCreateSeq(CV_SEQ_CONTOUR, sizeof(CvContour), sizeof(CvPoint), CVMEMSTORAGE(storage)); rb_initialize(0, NULL, object);
VALUE object = cCvSeq::new_sequence(cCvContour::rb_class(), seq, cCvPoint::rb_class(), storage);
return object; return object;
} }

View file

@ -139,22 +139,25 @@ rb_initialize(int argc, VALUE *argv, VALUE self)
CvSeq *seq = NULL; CvSeq *seq = NULL;
storage = CHECK_CVMEMSTORAGE(storage); storage = CHECK_CVMEMSTORAGE(storage);
int type = 0, size = 0; int type = 0, size = 0;
if(klass == cCvIndex::rb_class()){ if (klass == cCvIndex::rb_class()) {
type = CV_SEQ_ELTYPE_INDEX; type = CV_SEQ_ELTYPE_INDEX;
size = sizeof(CvIndex); size = sizeof(CvIndex);
}else if(klass == cCvPoint::rb_class()){ }
else if (klass == cCvPoint::rb_class()) {
type = CV_SEQ_ELTYPE_POINT; type = CV_SEQ_ELTYPE_POINT;
size = sizeof(CvPoint); size = sizeof(CvPoint);
}else if(klass == cCvPoint2D32f::rb_class()){ }
else if (klass == cCvPoint2D32f::rb_class()) {
type = CV_SEQ_ELTYPE_POINT; type = CV_SEQ_ELTYPE_POINT;
size = sizeof(CvPoint2D32f); size = sizeof(CvPoint2D32f);
}else if(klass == cCvPoint3D32f::rb_class()){ }
else if (klass == cCvPoint3D32f::rb_class()) {
type = CV_SEQ_ELTYPE_POINT3D; type = CV_SEQ_ELTYPE_POINT3D;
size = sizeof(CvPoint3D32f); size = sizeof(CvPoint3D32f);
} }
// auto_extend(self); // auto_extend(self);
// todo: more various class will be support. // todo: more various class will be support.
if(!size) if (!size)
rb_raise(rb_eTypeError, "unsupport %s class for sequence-block.", rb_class2name(klass)); rb_raise(rb_eTypeError, "unsupport %s class for sequence-block.", rb_class2name(klass));
seq = cvCreateSeq(type, sizeof(CvSeq), size, CVMEMSTORAGE(storage)); seq = cvCreateSeq(type, sizeof(CvSeq), size, CVMEMSTORAGE(storage));
DATA_PTR(self) = seq; DATA_PTR(self) = seq;

37
test/test_cvcontour.rb Executable file
View file

@ -0,0 +1,37 @@
#!/usr/bin/env ruby
# -*- mode: ruby; coding: utf-8-unix -*-
require 'test/unit'
require 'opencv'
require File.expand_path(File.dirname(__FILE__)) + '/helper'
include OpenCV
# Tests for OpenCV::CvContour
class TestCvContour < OpenCVTestCase
def test_initialize
contour = CvContour.new
assert_not_nil(contour)
assert_equal(CvContour, contour.class)
assert(contour.is_a? CvSeq)
end
def test_rect
contour = CvContour.new
assert_not_nil(contour.rect)
assert_equal(CvRect, contour.rect.class)
end
def test_color
contour = CvContour.new
assert_equal(0, contour.color)
contour.color = 1
assert_equal(1, contour.color)
end
def test_reserved
reserved = CvContour.new.reserved
assert_equal(Array, reserved.class)
assert_equal(3, reserved.size)
end
end

View file

@ -1238,26 +1238,5 @@ class TestCvMat_imageprocessing < OpenCVTestCase
assert_equal(4, contours.total) assert_equal(4, contours.total)
assert_equal(4, contours.h_next.total) assert_equal(4, contours.h_next.total)
end 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 end

View file

@ -221,20 +221,22 @@ class TestCvSeq < OpenCVTestCase
assert_equal(60, seq1[1].y) assert_equal(60, seq1[1].y)
end end
def test_h_prev # These methods are tested in TestCvMat_imageprocessing#test_find_contours
flunk('FIXME: CvSeq#h_prev is not tested yet.') # (test_cvmat_imageprocessing.rb)
end # def test_h_prev
# flunk('FIXME: CvSeq#h_prev is not tested yet.')
# end
def test_h_next # def test_h_next
flunk('FIXME: CvSeq#h_next is not tested yet.') # flunk('FIXME: CvSeq#h_next is not tested yet.')
end # end
def test_v_prev # def test_v_prev
flunk('FIXME: CvSeq#v_prev is not tested yet.') # flunk('FIXME: CvSeq#v_prev is not tested yet.')
end # end
def test_v_next # def test_v_next
flunk('FIXME: CvSeq#v_next is not tested yet.') # flunk('FIXME: CvSeq#v_next is not tested yet.')
end # end
end end