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

fix uncaught exception in CvMat#match_descriptors on OSX Mavericks

This commit is contained in:
ser1zw 2013-12-07 22:34:08 +09:00
parent bbfaeb29c1
commit 6991e2b6e4
3 changed files with 52 additions and 49 deletions

View file

@ -4,9 +4,9 @@ include OpenCV
data = File.dirname(__FILE__) data = File.dirname(__FILE__)
query = IplImage.load File.join(data, 'query.png'), CV_LOAD_IMAGE_GRAYSCALE query = CvMat.load File.join(data, 'query.png'), CV_LOAD_IMAGE_GRAYSCALE
image_files = ['1.png', '2.png', '3.png'].map{|f| File.join(data, 'train', f)} image_files = ['1.png', '2.png', '3.png'].map{|f| File.join(data, 'train', f)}
images = image_files.map{|f| IplImage.load f, CV_LOAD_IMAGE_GRAYSCALE} images = image_files.map{|f| CvMat.load f, CV_LOAD_IMAGE_GRAYSCALE}
matchs = query.match_descriptors(images) matchs = query.match_descriptors(images)

View file

@ -5386,7 +5386,6 @@ rb_match_descriptors(int argc, VALUE *argv, VALUE self)
{ {
VALUE images, detector_type, descriptor_type, matcher_type; VALUE images, detector_type, descriptor_type, matcher_type;
rb_scan_args(argc, argv, "13", &images, &detector_type, &descriptor_type, &matcher_type); rb_scan_args(argc, argv, "13", &images, &detector_type, &descriptor_type, &matcher_type);
if (RARRAY_LEN(images) == 0) { if (RARRAY_LEN(images) == 0) {
return rb_hash_new(); return rb_hash_new();
} }
@ -5400,45 +5399,44 @@ rb_match_descriptors(int argc, VALUE *argv, VALUE self)
matcher_type = rb_str_new2("FlannBased"); matcher_type = rb_str_new2("FlannBased");
} }
cv::Mat queryImage = CVMAT(self); VALUE _matches = rb_hash_new();
try {
cv::Mat queryImage(CVMAT(self));
std::vector<cv::Mat> trainImages; std::vector<cv::Mat> trainImages;
for(int i=0; i < RARRAY_LEN(images); i++) { for(int i = 0, n = RARRAY_LEN(images); i < n; i++) {
trainImages.push_back(CVMAT_WITH_CHECK(RARRAY_PTR(images)[i])); trainImages.push_back(CVMAT_WITH_CHECK(RARRAY_PTR(images)[i]));
} }
cv::Ptr<cv::FeatureDetector> featureDetector = cv::FeatureDetector::create(RSTRING_PTR(detector_type)); cv::Ptr<cv::FeatureDetector> featureDetector = cv::FeatureDetector::create(StringValueCStr(detector_type));
if (featureDetector.empty()) { if (featureDetector.empty()) {
rb_raise(rb_eArgError, "Could not create feature detector by given detector type: %s", RSTRING_PTR(detector_type)); rb_raise(rb_eArgError, "Could not create feature detector by given detector type: %s", StringValueCStr(detector_type));
} }
cv::Ptr<cv::DescriptorExtractor> descriptorExtractor = cv::DescriptorExtractor::create(RSTRING_PTR(descriptor_type)); cv::Ptr<cv::DescriptorExtractor> descriptorExtractor = cv::DescriptorExtractor::create(StringValueCStr(descriptor_type));
if (descriptorExtractor.empty()) { if (descriptorExtractor.empty()) {
rb_raise(rb_eArgError, "Could not create descriptor extractor by given descriptor type: %s", RSTRING_PTR(descriptor_type)); rb_raise(rb_eArgError, "Could not create descriptor extractor by given descriptor type: %s", StringValueCStr(descriptor_type));
} }
cv::Ptr<cv::DescriptorMatcher> descriptorMatcher; cv::Ptr<cv::DescriptorMatcher> descriptorMatcher;
try { try {
descriptorMatcher = cv::DescriptorMatcher::create(RSTRING_PTR(matcher_type)); descriptorMatcher = cv::DescriptorMatcher::create(StringValueCStr(matcher_type));
} }
catch(cv::Exception& e) { catch(cv::Exception& e) {
rb_raise(rb_eArgError, "Could not create descriptor matcher by given matcher type: %s", RSTRING_PTR(matcher_type)); rb_raise(rb_eArgError, "Could not create descriptor matcher by given matcher type: %s", StringValueCStr(matcher_type));
} }
std::vector<cv::KeyPoint> queryKeypoints; std::vector<cv::KeyPoint> queryKeypoints;
std::vector<std::vector<cv::KeyPoint> > trainKeypoints; std::vector<std::vector<cv::KeyPoint> > trainKeypoints;
featureDetector->detect(queryImage, queryKeypoints); featureDetector->detect(queryImage, queryKeypoints);
featureDetector->detect(trainImages, trainKeypoints); featureDetector->detect(trainImages, trainKeypoints);
cv::Mat queryDescriptors; cv::Mat queryDescriptors;
std::vector<cv::Mat> trainDescriptors; std::vector<cv::Mat> trainDescriptors;
descriptorExtractor->compute(queryImage, queryKeypoints, queryDescriptors); descriptorExtractor->compute(queryImage, queryKeypoints, queryDescriptors);
descriptorExtractor->compute(trainImages, trainKeypoints, trainDescriptors); descriptorExtractor->compute(trainImages, trainKeypoints, trainDescriptors);
std::vector<cv::DMatch> matches; std::vector<cv::DMatch> matches;
descriptorMatcher->add(trainDescriptors); descriptorMatcher->add(trainDescriptors);
descriptorMatcher->train(); descriptorMatcher->train();
descriptorMatcher->match(queryDescriptors, matches); descriptorMatcher->match(queryDescriptors, matches);
VALUE _matches = rb_hash_new(); for (size_t i = 0, n = matches.size(); i < n; i++) {
for (size_t i=0; i<matches.size(); i++) {
VALUE match = INT2FIX(matches[i].imgIdx); VALUE match = INT2FIX(matches[i].imgIdx);
VALUE count = rb_hash_lookup(_matches, match); VALUE count = rb_hash_lookup(_matches, match);
if (NIL_P(count)) { if (NIL_P(count)) {
@ -5448,6 +5446,11 @@ rb_match_descriptors(int argc, VALUE *argv, VALUE self)
} }
rb_hash_aset(_matches, match, count); rb_hash_aset(_matches, match, count);
} }
}
catch (cv::Exception& e) {
raise_cverror(e);
}
return _matches; return _matches;
} }

View file

@ -18,7 +18,7 @@ class TestCvMat_matching < OpenCVTestCase
end end
def read_test_image(*path) def read_test_image(*path)
IplImage.load File.join(data_dir, *path), CV_LOAD_IMAGE_GRAYSCALE CvMat.load File.join(data_dir, *path), CV_LOAD_IMAGE_GRAYSCALE
end end
def test_match_descriptors def test_match_descriptors