【发布时间】:2017-01-27 10:03:09
【问题描述】:
我想在 OpenCV 中使用 surf 比较这两张图片,并使用 BFMatcher 进行匹配。
代码如下:
//read the image
cv::Mat image1= cv::imread("C:\\Users\\The\\Desktop\\cub.jpg");
cv::Mat image2= cv::imread("C:\\Users\\The\\Desktop\\cat.jpg");
//detect the key points using surf
cv::SurfFeatureDetector surf(300,4,2,true,false);
//image 1 key points
vector<cv::KeyPoint> image1Keypoints;
//detect the key points in image 1 using surf
surf.detect(image1,image1Keypoints);
//draw the keypoints of image1
cv::drawKeypoints(image1,image1Keypoints,image1,
cv::Scalar(255,255,255),cv::DrawMatchesFlags::DRAW_OVER_OUTIMG);
cv::namedWindow("Original image 1 with keypoints");
cv::imshow("Original image 1 with keypoints",image1);
// image 2 key points
std::vector<cv::KeyPoint> image2keypoints;
//detect the key points in image 1 using surf
surf.detect(image2,image2keypoints);
//draw the key points
cv::drawKeypoints(image2,keypoints1,image2,
cv::Scalar(255,255,255),cv::DrawMatchesFlags::DRAW_OVER_OUTIMG);
//the extracted image display
cv::namedWindow("image 2 with keypoints");
cv::imshow("image 2 with keypoints",image2);
cv::SiftDescriptorExtractor sift;
cv::Mat image1Descriptor;
cv::Mat image2Desccriptor;
sift.compute(image1,
image1Keypoints,
image1Descriptor);
sift.compute(image2,
keypoints1,
image2Desccriptor);
cv::BFMatcher matcher(cv::NORM_L2,true);
std::vector<cv::DMatch> matches;
matcher.match(image2Desccriptor,image1Descriptor,matches);
cout<<"match=";
//the number of matched features between the two images
cout<<matches.size()<<endl;
cv::Mat imageMatches;
cv::drawMatches(image2,keypoints1,image1,image1Keypoints,
matches,imageMatches,cv::DrawMatchesFlags::DRAW_OVER_OUTIMG);
cv::namedWindow("matches");
cv::imshow("matches",imageMatches);
cv::waitKey(0);
大约有 833 个匹配的特征,但图像完全不同。
我的代码有什么问题以及如何减少匹配数。我可以通过更改 surf 参数来减少匹配的数量,但这是因为它检测到的特征数量很少。例如,将 siftFeature 检测器更改为:
cv::SurfFeatureDetector surf(1000,4,2,true,false);
我能够得到matches=77,但它仍然是大量的匹配并且匹配减少了,因为与上述数字相比,我得到的特征数量较少。
【问题讨论】:
-
当我滚动时,我看到了一个故事...... :-D
-
您正在比较小图像块(32x32 左右),即描述符...匹配描述符并不意味着比较图像。匹配后还需要做一些其他的步骤。
-
@POW 我的问题是对于两个不同的图像,怎么可能有这么多的匹配以及如何减少匹配的数量?
-
@Miki 你能详细说明“匹配后你必须使用其他一些步骤”的意思吗?我是opencv的初学者。
-
例如,估计匹配关键点的变换,并检查异常值的数量。更少的异常值意味着更多相似的图像。或者在 BoW、SVM 等分类器中使用描述符。
标签: c++ opencv image-processing computer-vision surf