opencv3.2 环境下实现 SURF算法:错误调试
环境:opencv3.2+VS2015 Debug X64 实现SURF算法
代码来自网上,并未修改,知识为了将其调通,遇见错误:
严重性 代码 说明 项目 文件 行 禁止显示状态
错误 LNK2019 无法解析的外部符号 "public: static struct cv::Ptr<class cv::xfeatures2d::SURF> __cdecl cv::xfeatures2d::SURF::create(double,int,int,bool,bool)" (?create@SURF@xfeatures2d@cv@@SA?AU?$Ptr@VSURF@xfeatures2d@cv@@@3@NHH_N0@Z),该符号在函数 main 中被引用 SURF_Algorithm D:\work\project\SURF_Algorithm\SURF_Algorithm\SURF.obj 1
原因是lib文件没有引入
opencv_xfeatures2d320d.lib
opencv_features2d320d.lib
源码:
1 #include <opencv2/opencv.hpp> 2 #include <opencv2/xfeatures2d.hpp> 3 #include <opencv2/xfeatures2d/nonfree.hpp> 4 #include <iostream> 5 6 #include<iostream> 7 using namespace std; 8 using namespace cv; 9 using namespace cv::xfeatures2d; 10 int main() 11 { 12 Mat srcImage1 = imread("D://1.jpg", 1); 13 Mat srcImage2 = imread("D://1.jpg", 1); 14 if (!srcImage1.data || !srcImage2.data) 15 { 16 cout << "读取图片出错" << endl; 17 return false; 18 } 19 20 imshow("原始图1", srcImage1); 21 imshow("原始图2", srcImage2); 22 23 int minHessian = 100; 24 Ptr<SurfFeatureDetector> detector = SurfFeatureDetector::create(minHessian); 25 26 vector<cv::KeyPoint> key_points_1, key_points_2; 27 28 Mat dstImage1, dstImage2; 29 detector->detectAndCompute(srcImage1, Mat(), key_points_1, dstImage1); 30 detector->detectAndCompute(srcImage2, Mat(), key_points_2, dstImage2);//可以分成detect和compute 31 32 Mat img_keypoints_1, img_keypoints_2; 33 drawKeypoints(srcImage1, key_points_1, img_keypoints_1, Scalar::all(-1), DrawMatchesFlags::DEFAULT); 34 drawKeypoints(srcImage2, key_points_2, img_keypoints_2, Scalar::all(-1), DrawMatchesFlags::DEFAULT); 35 36 Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("FlannBased"); 37 vector<DMatch>mach; 38 39 matcher->match(dstImage1, dstImage2, mach); 40 double Max_dist = 0; 41 double Min_dist = 100; 42 for (int i = 0; i < dstImage1.rows; i++) 43 { 44 double dist = mach[i].distance; 45 if (dist < Min_dist)Min_dist = dist; 46 if (dist > Max_dist)Max_dist = dist; 47 } 48 cout << "最短距离" << Min_dist << endl; 49 cout << "最长距离" << Max_dist << endl; 50 51 vector<DMatch>goodmaches; 52 for (int i = 0; i < dstImage1.rows; i++) 53 { 54 if (mach[i].distance < 2 * Min_dist) 55 goodmaches.push_back(mach[i]); 56 } 57 Mat img_maches; 58 drawMatches(srcImage1, key_points_1, srcImage2, key_points_2, goodmaches, img_maches); 59 60 for (int i = 0; i < goodmaches.size(); i++) 61 { 62 cout << "符合条件的匹配:" << goodmaches[i].queryIdx << "--" << goodmaches[i].trainIdx << endl; 63 } 64 imshow("效果图1", img_keypoints_1); 65 imshow("效果图2", img_keypoints_2); 66 imshow("匹配效果", img_maches); 67 68 waitKey(0); 69 return 0; 70 }
效果图
环境图像:
opencv文件夹下相关文件的修改:
以上添加的文件均为cmake后获取得到。