mirror of
https://github.com/ruby-opencv/ruby-opencv
synced 2023-03-27 23:22:12 -04:00
added CvFeatureTree
This commit is contained in:
parent
d37cefb3aa
commit
e5ec274a04
5 changed files with 247 additions and 0 deletions
124
ext/opencv/cvfeaturetree.cpp
Normal file
124
ext/opencv/cvfeaturetree.cpp
Normal file
|
@ -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
|
||||
|
55
ext/opencv/cvfeaturetree.h
Normal file
55
ext/opencv/cvfeaturetree.h
Normal file
|
@ -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
|
||||
|
||||
|
||||
|
|
@ -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();
|
||||
|
|
|
@ -130,6 +130,8 @@ extern "C" {
|
|||
#include "cvsurfpoint.h"
|
||||
#include "cvsurfparams.h"
|
||||
|
||||
#include "cvfeaturetree.h"
|
||||
|
||||
// GUI
|
||||
#include "gui.h"
|
||||
#include "window.h"
|
||||
|
|
65
test/test_cvfeaturetree.rb
Executable file
65
test/test_cvfeaturetree.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::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
|
||||
|
Loading…
Reference in a new issue