環境: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)" ([email protected]@[email protected]@@[email protected]@[email protected]@@@[email protected][email protected]),該符号在函數 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後擷取得到。
轉載于:https://www.cnblogs.com/linmengran/p/7928071.html