mirror of
https://github.com/ruby-opencv/ruby-opencv
synced 2023-03-27 23:22:12 -04:00
modified and tested CvConnectedComp
This commit is contained in:
parent
741f83daff
commit
37e95115e0
3 changed files with 94 additions and 3 deletions
|
@ -50,14 +50,29 @@ define_ruby_class()
|
|||
rb_define_method(rb_klass, "area", RUBY_METHOD_FUNC(rb_area), 0);
|
||||
rb_define_method(rb_klass, "value", RUBY_METHOD_FUNC(rb_value), 0);
|
||||
rb_define_method(rb_klass, "rect", RUBY_METHOD_FUNC(rb_rect), 0);
|
||||
rb_define_method(rb_klass, "rect=", RUBY_METHOD_FUNC(rb_set_rect), 0);
|
||||
rb_define_method(rb_klass, "rect=", RUBY_METHOD_FUNC(rb_set_rect), 1);
|
||||
rb_define_method(rb_klass, "contour", RUBY_METHOD_FUNC(rb_contour), 0);
|
||||
}
|
||||
|
||||
void
|
||||
cvconnectedcomp_free(void *ptr)
|
||||
{
|
||||
if (ptr) {
|
||||
CvConnectedComp* connected_comp = (CvConnectedComp*)ptr;
|
||||
if (connected_comp->contour) {
|
||||
CvContour *contour = (CvContour*)connected_comp->contour;
|
||||
if (contour->storage)
|
||||
cvReleaseMemStorage(&(contour->storage));
|
||||
}
|
||||
free(ptr);
|
||||
}
|
||||
}
|
||||
|
||||
VALUE
|
||||
rb_allocate(VALUE klass)
|
||||
{
|
||||
CvConnectedComp *ptr;
|
||||
return Data_Make_Struct(klass, CvConnectedComp, 0, -1, ptr);
|
||||
return Data_Make_Struct(klass, CvConnectedComp, 0, cvconnectedcomp_free, ptr);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -97,6 +112,15 @@ rb_set_rect(VALUE self, VALUE rect)
|
|||
return self;
|
||||
}
|
||||
|
||||
/*
|
||||
* Return optional component boundary
|
||||
*/
|
||||
VALUE
|
||||
rb_contour(VALUE self)
|
||||
{
|
||||
return REFER_OBJECT(cCvContour::rb_class(), &CVCONNECTEDCOMP(self)->contour, self);
|
||||
}
|
||||
|
||||
VALUE
|
||||
new_object()
|
||||
{
|
||||
|
|
|
@ -35,7 +35,9 @@ VALUE new_object(CvConnectedComp comp);
|
|||
|
||||
__NAMESPACE_END_CVCONNECTEDCOMP
|
||||
|
||||
inline CvConnectedComp *CVCONNECTEDCOMP(VALUE object){
|
||||
inline CvConnectedComp*
|
||||
CVCONNECTEDCOMP(VALUE object)
|
||||
{
|
||||
CvConnectedComp *ptr;
|
||||
Data_Get_Struct(object, CvConnectedComp, ptr);
|
||||
return ptr;
|
||||
|
|
65
test/test_cvconnectedcomp.rb
Executable file
65
test/test_cvconnectedcomp.rb
Executable file
|
@ -0,0 +1,65 @@
|
|||
#!/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::CvConnectedComp
|
||||
class TestCvConnectedComp < OpenCVTestCase
|
||||
def setup
|
||||
mat0 = create_cvmat(128, 128, :cv8u, 1) { |j, i, c|
|
||||
if (i >= 32 and i < 96) and (j >= 32 and j < 96)
|
||||
CvScalar.new(255)
|
||||
elsif (i >= 16 and i < 112) and (j >= 16 and j < 112)
|
||||
CvScalar.new(192)
|
||||
else
|
||||
CvScalar.new(128)
|
||||
end
|
||||
}
|
||||
|
||||
point = CvPoint.new(20, 20)
|
||||
@mat, @connected_comp, @mask = mat0.flood_fill(point, 0, CvScalar.new(0), CvScalar.new(64),
|
||||
{:connectivity => 8, :fixed_range => true, :mask_only => true})
|
||||
end
|
||||
|
||||
def test_initialize
|
||||
connected_comp = CvConnectedComp.new
|
||||
assert_not_nil(connected_comp)
|
||||
assert_equal(CvConnectedComp, connected_comp.class)
|
||||
assert_not_nil(connected_comp.area)
|
||||
assert_not_nil(connected_comp.value)
|
||||
assert_not_nil(connected_comp.rect)
|
||||
assert_not_nil(connected_comp.contour)
|
||||
end
|
||||
|
||||
def test_area
|
||||
assert_in_delta(9216.0, @connected_comp.area, 0.01)
|
||||
end
|
||||
|
||||
def test_value
|
||||
assert_equal(CvScalar, @connected_comp.value.class)
|
||||
assert_cvscalar_equal(CvScalar.new(220, 0, 0, 0), @connected_comp.value)
|
||||
end
|
||||
|
||||
def test_rect
|
||||
assert_equal(CvRect, @connected_comp.rect.class)
|
||||
assert_equal(16, @connected_comp.rect.x)
|
||||
assert_equal(16, @connected_comp.rect.y)
|
||||
assert_equal(96, @connected_comp.rect.width)
|
||||
assert_equal(96, @connected_comp.rect.height)
|
||||
|
||||
connected_comp.rect = CvRect.new(1, 2, 3, 4);
|
||||
assert_equal(1, @connected_comp.rect.x)
|
||||
assert_equal(2, @connected_comp.rect.y)
|
||||
assert_equal(3, @connected_comp.rect.width)
|
||||
assert_equal(4, @connected_comp.rect.height)
|
||||
end
|
||||
|
||||
def test_contour
|
||||
assert_equal(CvContour, @connected_comp.contour.class)
|
||||
assert_not_nil(@connected_comp.contour)
|
||||
end
|
||||
end
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue