8

我正在尝试实现一个程序,该程序将输入两个图像,一个是单独的盒子图像,另一个是场景中的盒子。基本上,该程序应该在这两个图像中找到关键点,并将显示与关键点匹配的图像。最后,我希望看到两个输入图像的附加图像以及连接的匹配关键点。我的代码如下:

#include <opencv2\opencv.hpp>
#include <iostream>

int main(int argc, const char* argv[]) {
   cv::Mat input1 = cv::imread("input.jpg", 1); //Load as grayscale
   //cv::cvtColor(input1,input1,CV_BGR2GRAY);
   //second input load as grayscale
   cv::Mat input2 = cv::imread("input2.jpg",1);
   cv::SiftFeatureDetector detector;
   //cv::SiftFeatureDetector
   detector(
      1, 1,
      cv::SIFT::CommonParams::DEFAULT_NOCTAVES,
      cv::SIFT::CommonParams::DEFAULT_NOCTAVE_LAYERS,
      cv::SIFT::CommonParams::DEFAULT_FIRST_OCTAVE,
      cv::SIFT::CommonParams::FIRST_ANGLE );
   std::vector<cv::KeyPoint> keypoints1;
   detector.detect(input1, keypoints1);
   // Add results to image and save.
   cv::Mat output1;
   cv::drawKeypoints(input1, keypoints1, output1);
   cv::imshow("Sift_result1.jpg", output1);
   cv::imwrite("Sift_result1.jpg",output1);
   //keypoints array for input 2
   std::vector<cv::KeyPoint> keypoints2;
   //output array for ouput 2
   cv::Mat output2;
   //Sift extractor of opencv
   cv::SiftDescriptorExtractor extractor;
   cv::Mat descriptors1,descriptors2;
   cv::BruteForceMatcher<cv::L2<float>> matcher;
   cv::vector<cv::DMatch> matches;
   cv::Mat img_matches;
   detector.detect(input2,keypoints2);
   cv::drawKeypoints(input2,keypoints2,output2);
   cv::imshow("Sift_result2.jpg",output2);
   cv::imwrite("Sift_result2.jpg",output2);
   extractor.compute(input1,keypoints1,descriptors1);
   extractor.compute(input2,keypoints2,descriptors2);
   matcher.match(descriptors1,descriptors2,matches);
   //show result
   cv::drawMatches(input1,keypoints1,input2,keypoints2,matches,img_matches);
   cv::imshow("matches",img_matches);
   cv::imwrite("matches.jpg",img_matches);
   cv::waitKey();
   return 0;
}

问题是有两个多于预期的匹配。我试图调试程序并查看关键点向量内部的内容等等,一切看起来都很好,至少我认为它们是,关键点是通过方向等检测到的。

我正在使用 OpenCV v2.3 并检查了它的文档以了解我正在使用的类的类型,并试图解决问题,但没有奏效。我为此工作了 3 天并没有取得太大的进步。

这是我从程序中得到的输出。

我应该删除图像。

我知道这不应该给我太多的匹配,因为我已经在 matlab 中用另一个非常好的实现测试了完全相同的图像。

4

2 回答 2

6

而不是使用 BruteForceMatcher 尝试使用 FlannBasedMatcher 并计算关键点之间的最大和最小距离以仅保留良好的匹配。有关示例,请参阅“使用 FLANN 进行特征匹配”。

于 2012-04-05T05:41:09.140 回答
0

我在 SIFT 中遇到了同样的问题。我使用了 knn 匹配器(K=3)。并迭代地遵循以下程序

{
Calculated best affine transform with least square method.

Found out the transform for all keypoints in source image.

Checked out MaxError and MinError.

Points with Error near MaxError are removed from the matching list
}
于 2013-05-06T10:37:24.777 回答