add CvMat#subspace_project and CvMat#subspace_reconstruct

This commit is contained in:
ser1zw 2013-10-06 00:29:38 +09:00
parent 5c811bda74
commit 7da8b403fc
3 changed files with 77 additions and 0 deletions

View File

@ -412,6 +412,9 @@ void define_ruby_class()
rb_define_method(rb_klass, "extract_surf", RUBY_METHOD_FUNC(rb_extract_surf), -1);
rb_define_method(rb_klass, "subspace_project", RUBY_METHOD_FUNC(rb_subspace_project), 2);
rb_define_method(rb_klass, "subspace_reconstruct", RUBY_METHOD_FUNC(rb_subspace_reconstruct), 2);
rb_define_method(rb_klass, "save_image", RUBY_METHOD_FUNC(rb_save_image), -1);
rb_define_alias(rb_klass, "save", "save_image");
@ -5842,6 +5845,55 @@ rb_extract_surf(int argc, VALUE *argv, VALUE self)
return rb_assoc_new(_keypoints, _descriptors);
}
/*
* call-seq:
* subspace_project(w, mean) -> cvmat
*/
VALUE
rb_subspace_project(VALUE self, VALUE w, VALUE mean)
{
VALUE projection;
try {
cv::Mat w_mat(CVMAT_WITH_CHECK(w));
cv::Mat mean_mat(CVMAT_WITH_CHECK(mean));
cv::Mat self_mat(CVMAT(self));
cv::Mat pmat = cv::subspaceProject(w_mat, mean_mat, self_mat);
projection = new_object(pmat.rows, pmat.cols, pmat.type());
CvMat tmp = pmat;
cvCopy(&tmp, CVMAT(projection));
}
catch (cv::Exception& e) {
raise_cverror(e);
}
return projection;
}
/*
* call-seq:
* subspace_reconstruct(w, mean) -> cvmat
*/
VALUE
rb_subspace_reconstruct(VALUE self, VALUE w, VALUE mean)
{
VALUE result;
try {
cv::Mat w_mat(CVMAT_WITH_CHECK(w));
cv::Mat mean_mat(CVMAT_WITH_CHECK(mean));
cv::Mat self_mat(CVMAT(self));
cv::Mat rmat = cv::subspaceReconstruct(w_mat, mean_mat, self_mat);
result = new_object(rmat.rows, rmat.cols, rmat.type());
CvMat tmp = rmat;
cvCopy(&tmp, CVMAT(result));
}
catch (cv::Exception& e) {
raise_cverror(e);
}
return result;
}
VALUE
new_object(int rows, int cols, int type)
{

View File

@ -239,6 +239,9 @@ VALUE rb_compute_correspond_epilines(VALUE klass, VALUE points, VALUE which_imag
/* Feature detection and description */
VALUE rb_extract_surf(int argc, VALUE *argv, VALUE self);
VALUE rb_subspace_project(VALUE self, VALUE w, VALUE mean);
VALUE rb_subspace_reconstruct(VALUE self, VALUE w, VALUE mean);
// HighGUI function
VALUE rb_save_image(int argc, VALUE *argv, VALUE self);

View File

@ -2976,5 +2976,27 @@ class TestCvMat < OpenCVTestCase
# Uncomment the following line to show the result
# snap *results
end
def test_subspace_project
w = CvMat.new(10, 20, :cv32f, 1)
mean = CvMat.new(w.rows, 1, :cv32f, 1)
mat = CvMat.new(w.cols, w.rows, :cv32f, 1)
result = mat.subspace_project(w, mean)
assert_equal(CvMat, result.class)
assert_equal(w.cols, result.rows)
assert_equal(w.cols, result.cols)
end
def test_subspace_reconstruct
w = CvMat.new(10, 20, :cv32f, 1)
mean = CvMat.new(w.rows, 1, :cv32f, 1)
mat = CvMat.new(w.cols, w.cols, :cv32f, 1)
result = mat.subspace_reconstruct(w, mean)
assert_equal(CvMat, result.class)
assert_equal(w.cols, result.rows)
assert_equal(w.rows, result.cols)
end
end