From e5ec274a04786dfd88841620455346631e834559 Mon Sep 17 00:00:00 2001 From: ser1zw Date: Mon, 19 Dec 2011 03:45:50 +0900 Subject: [PATCH] added CvFeatureTree --- ext/opencv/cvfeaturetree.cpp | 124 +++++++++++++++++++++++++++++++++++ ext/opencv/cvfeaturetree.h | 55 ++++++++++++++++ ext/opencv/opencv.cpp | 1 + ext/opencv/opencv.h | 2 + test/test_cvfeaturetree.rb | 65 ++++++++++++++++++ 5 files changed, 247 insertions(+) create mode 100644 ext/opencv/cvfeaturetree.cpp create mode 100644 ext/opencv/cvfeaturetree.h create mode 100755 test/test_cvfeaturetree.rb diff --git a/ext/opencv/cvfeaturetree.cpp b/ext/opencv/cvfeaturetree.cpp new file mode 100644 index 0000000..498470a --- /dev/null +++ b/ext/opencv/cvfeaturetree.cpp @@ -0,0 +1,124 @@ +/************************************************************ + + cvfeaturetree.cpp - + + $Author: ser1zw $ + + Copyright (C) 2011 ser1zw + +************************************************************/ +#include "cvfeaturetree.h" +/* + * Document-class: OpenCV::CvFeatureTree + */ +__NAMESPACE_BEGIN_OPENCV +__NAMESPACE_BEGIN_CVFEATURETREE + +VALUE rb_klass; + +VALUE +rb_class() +{ + return rb_klass; +} + +void +mark_feature_tree(void *ptr) +{ + if (ptr) { + VALUE desc = ((CvFeatureTreeWrap*)ptr)->desc; + rb_gc_mark(desc); + } +} + +void +rb_release_feature_tree(void *ptr) +{ + if (ptr) { + CvFeatureTree* ft = ((CvFeatureTreeWrap*)ptr)->feature_tree; + cvReleaseFeatureTree(ft); + } +} + +VALUE +rb_allocate(VALUE klass) +{ + CvFeatureTreeWrap* ptr; + return Data_Make_Struct(klass, CvFeatureTreeWrap, mark_feature_tree, + rb_release_feature_tree, ptr); +} + +void +define_ruby_class() +{ + if (rb_klass) + return; + /* + * opencv = rb_define_module("OpenCV"); + * + * note: this comment is used by rdoc. + */ + VALUE opencv = rb_module_opencv(); + + rb_klass = rb_define_class_under(opencv, "CvFeatureTree", rb_cObject); + rb_define_alloc_func(rb_klass, rb_allocate); + rb_define_private_method(rb_klass, "initialize", RUBY_METHOD_FUNC(rb_initialize), 1); + + rb_define_method(rb_klass, "find_features", RUBY_METHOD_FUNC(rb_find_features), 3); +} + +/* + * call-seq: + * new(desc) + * + * Create a new kd-tree + */ +VALUE +rb_initialize(VALUE self, VALUE desc) +{ + CvMat* desc_mat = CVMAT_WITH_CHECK(desc); + CvFeatureTreeWrap* self_ptr = (CvFeatureTreeWrap*)DATA_PTR(self); + free(self_ptr); + self_ptr = ALLOC(CvFeatureTreeWrap); + try { + self_ptr->feature_tree = cvCreateKDTree(desc_mat); + } + catch (cv::Exception& e) { + raise_cverror(e); + } + self_ptr->desc = desc; +} + +/* + * call-seq: + * find_features(desc, rows, cols, k, emax) -> array(results, dist) + * + * Find features from kd-tree + * + * desc: m x d matrix of (row-)vectors to find the nearest neighbors of. + * k: The number of neighbors to find. + * emax: The maximum number of leaves to visit. + * + * return + * results: m x k set of row indices of matching vectors (referring to matrix passed to cvCreateFeatureTree). Contains -1 in some columns if fewer than k neighbors found. + * dist: m x k matrix of distances to k nearest neighbors. + */ +VALUE +rb_find_features(VALUE self, VALUE desc, VALUE k, VALUE emax) +{ + CvMat* desc_mat = CVMAT_WITH_CHECK(desc); + int _k = NUM2INT(k); + VALUE results = cCvMat::new_object(desc_mat->rows, _k, CV_32SC1); + VALUE dist = cCvMat::new_object(desc_mat->rows, _k, CV_64FC1); + try { + cvFindFeatures(CVFEATURETREE(self), desc_mat, CVMAT(results), CVMAT(dist), _k, NUM2INT(emax)); + } + catch (cv::Exception& e) { + raise_cverror(e); + } + return rb_assoc_new(results, dist); +} + +__NAMESPACE_END_OPENCV +__NAMESPACE_END_CVFEATURETREE + diff --git a/ext/opencv/cvfeaturetree.h b/ext/opencv/cvfeaturetree.h new file mode 100644 index 0000000..5389aa9 --- /dev/null +++ b/ext/opencv/cvfeaturetree.h @@ -0,0 +1,55 @@ +/************************************************************ + + cvfeaturetree.h - + + $Author: ser1zw $ + + Copyright (C) 2011 ser1zw + +************************************************************/ +#ifndef RUBY_OPENCV_CVFEATURETREE_H +#define RUBY_OPENCV_CVFEATURETREE_H + +#include "opencv.h" + +#define __NAMESPACE_BEGIN_CVFEATURETREE namespace cCvFeatureTree { +#define __NAMESPACE_END_CVFEATURETREE } + +__NAMESPACE_BEGIN_OPENCV +__NAMESPACE_BEGIN_CVFEATURETREE + +VALUE rb_class(); +void define_ruby_class(); +VALUE rb_allocate(VALUE klass); +VALUE rb_initialize(VALUE self, VALUE desc); +VALUE rb_find_features(VALUE self, VALUE desc, VALUE k, VALUE emax); + +__NAMESPACE_END_CVFEATURETREE + +typedef struct _CvFeatureTreeWrap { + CvFeatureTree* feature_tree; + VALUE desc; +} CvFeatureTreeWrap; + +inline CvFeatureTree* +CVFEATURETREE(VALUE object) +{ + CvFeatureTreeWrap* ptr; + Data_Get_Struct(object, CvFeatureTreeWrap, ptr); + return ptr->feature_tree; +} + +inline CvFeatureTree* +CVFEATURETREE_WITH_CHECK(VALUE object) +{ + if (!rb_obj_is_kind_of(object, cCvFeatureTree::rb_class())) + raise_typeerror(object, cCvFeatureTree::rb_class()); + return CVFEATURETREE(object); +} + +__NAMESPACE_END_OPENCV + +#endif // RUBY_OPENCV_CVFEATURETREE + + + diff --git a/ext/opencv/opencv.cpp b/ext/opencv/opencv.cpp index 025dce1..7c89488 100644 --- a/ext/opencv/opencv.cpp +++ b/ext/opencv/opencv.cpp @@ -670,6 +670,7 @@ extern "C" { mOpenCV::cCvCircle32f::define_ruby_class(); mOpenCV::cCvConDensation::define_ruby_class(); + mOpenCV::cCvFeatureTree::define_ruby_class(); mOpenCV::cCvConnectedComp::define_ruby_class(); mOpenCV::cCvAvgComp::define_ruby_class(); diff --git a/ext/opencv/opencv.h b/ext/opencv/opencv.h index 3eebb58..8f51b7a 100644 --- a/ext/opencv/opencv.h +++ b/ext/opencv/opencv.h @@ -130,6 +130,8 @@ extern "C" { #include "cvsurfpoint.h" #include "cvsurfparams.h" +#include "cvfeaturetree.h" + // GUI #include "gui.h" #include "window.h" diff --git a/test/test_cvfeaturetree.rb b/test/test_cvfeaturetree.rb new file mode 100755 index 0000000..eee6fd6 --- /dev/null +++ b/test/test_cvfeaturetree.rb @@ -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::CvFeatureTree +class TestCvFeatureTree < OpenCVTestCase + def test_initialize + desc = CvMat.new(1, 1, :cv32f, 1) + ft = CvFeatureTree.new(desc) + assert_equal(CvFeatureTree, ft.class) + + assert_raise(TypeError) { + CvFeatureTree.new(DUMMY_OBJ) + } + end + + def test_find_feature + dim = 2 + points = [] + points << [99, 51] + points << [52, 57] + points << [57, 42] + points << [13, 39] + points << [15, 68] + points << [75, 11] + points << [69, 62] + points << [52, 46] + points << [0, 64] + points << [67, 16] + + desc1 = CvMat.new(points.size, dim, :cv32f, 1) + desc1.set_data(points) + + pt = [[50, 50], [11, 40]] + desc2 = CvMat.new(pt.size, dim, :cv32f, 1) + desc2.set_data(pt) + + ft = CvFeatureTree.new(desc1) + results, dist = ft.find_features(desc2, 1, 10) + + assert_equal(CvMat, results.class) + assert_equal(CvMat, dist.class) + + assert_equal(7, results[0][0].to_i) + assert_in_delta(4.472, dist[0][0], 0.001) + + assert_equal(3, results[1][0].to_i) + assert_in_delta(2.236, dist[1][0], 0.001) + + assert_raise(TypeError) { + ft.find_features(DUMMY_OBJ, 1, 10) + } + assert_raise(TypeError) { + ft.find_features(desc2, DUMMY_OBJ, 10) + } + assert_raise(TypeError) { + ft.find_features(desc2, 1, DUMMY_OBJ) + } + end +end +