我一直在尝试使用蛮力匹配器在两个图像上匹配一些特征,这是完整的代码:
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <vector>
#include<iostream>
int main()
{
cv::Ptr<IplImage> img = cvLoadImage("img.jpg",1);
cv::Ptr<IplImage> img1 = cvLoadImage("img1.jpg",1);
cv::Mat image(img,false);
cv::Mat image1(img1,false);
cv::resize(image,image,cv::Size(image.cols/5,image.rows/7));
cv::resize(image1,image1,cv::Size(image1.cols/5,image1.rows/7));
cv::waitKey(0);
std::vector<cv::KeyPoint> keypoints;
std::vector<cv::KeyPoint> keypoints1;
cv::SurfFeatureDetector surf(20000);
surf.detect(image,keypoints);
surf.detect(image1,keypoints1);
cv::drawKeypoints(image,keypoints,image,cv::Scalar(0,0,255),cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
cv::drawKeypoints(image1,keypoints1,image1,cv::Scalar(0,0,255),cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
cv::imshow("1",image);
cv::imshow("2",image1);
cv::waitKey(0);
cv::SurfDescriptorExtractor surfDesc;
cv::Mat descriptors;
cv::Mat descriptors1;
surfDesc.compute(image,keypoints,descriptors);
surfDesc.compute(image1,keypoints1,descriptors1);
std::cout<<sizeof(unsigned long);
cv::BruteForceMatcher<cv::L2<float>>matcher;
std::vector<cv::DMatch>matches;
matcher.match( descriptors, descriptors1, matches );
cv::Mat img_matches;
drawMatches( image, keypoints, image1, keypoints1,matches, img_matches, cv::Scalar::all(-1), cv::Scalar::all(-1),std::vector<char>(), cv::DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
imshow( "Matches", img_matches );
cv::waitKey(0);
return 0;
}
该程序在matcher.match 处以“bad alloc”通信中断,但问题是 关键点向量(可能),因为它表明它的大小为 0,容量为数百亿,这是 ridicoulus(因此是 bad alloc 问题)。函数 drawKeypoints运行良好,它在图像上绘制了一组合理的关键点,尽管有关键点矢量参数,但这些关键点看起来不错。我应该在链接器- >其他依赖项中添加它,我已经包含了没有“d”的库名称。所以例如我有 opencv_highgui230而不是opencv_highgui230d. 这是我无法更改的必需品,因为如果库名称添加了“d”,程序会在启动后立即退出,并显示“应用程序无法成功运行......”通信。我有 win 7 64,VS 2010,我正在调试模式下运行程序。我真的很感谢匹配器的一些帮助