最近在学习opencv的使用,在参照书籍《OpenCV3编程入门》实现SURF时遇到不少问题,下面做归纳总结。
错误 LNK2019 无法解析的外部符号 “public: static struct cv::Ptr __cdecl cv::xfeatures2d::SURF::create(double,int,int,bool,bool)” (?create@SURF@xfeatures2d@cv@@SA?AU?$Ptr@VSURF@xfeatures2d@cv@@@3@NHH_N0@Z)
因opencv_ffmpeg.dll、opencv_ffmpeg_64.dll和ffmpeg_version.cmake这三个文件下载失败,而无法生成opencv_xfeatures2d341d.lib
由于找不到opencv_features2d341d.dll……
几个巨坑的问题!!!
1.先贴出我的源码(照着书抄的)
main.c
#include <iostream>
#include <iomanip>
#include <windows.h>
#include <vector>
#include <time.h>
#include <stdio.h>
#include "../include/camera.h" //camera.h是自定义头文件
#include "../include/myopencv.h" //myopencv.h是自定义头文件
#pragma warning(disable:26812) //忽略相机状态枚举enum警告using namespace std;
using namespace cv; //opencv的命名空间int main(int argc, char argv[])
{//显示当前OpenCV的版本号printf("opencv version = %s\n", CV_VERSION);//载入两张原图并显示Mat src1 = imread("../picture/pic-test.jpg");Mat src2 = imread("../picture/logo.jpg");namedWindow("src1", WINDOW_KEEPRATIO);imshow("src1", src1);namedWindow("src2", WINDOW_KEEPRATIO);imshow("src2", src2);//定义SURF需要用到的变量和类int minHessian = 400; //SURF中的hessian阈值特征点检测算子// SURF与SurfFeatureDetector等价//定义一个SurfFeatureDetector(SURF) 特征检测类对象Ptr<cv::xfeatures2d::SurfFeatureDetector>detector = cv::xfeatures2d::SurfFeatureDetector::create(minHessian);std::vector<KeyPoint> keypoints_1, keypoints_2;//调用detect函数检测出SURF特征关键点,保存在vector容器中detector->detect(src1,keypoints_1);detector->detect(src2, keypoints_2);//绘制特征关键点Mat img_keypoints_1, img_keypoints_2;
drawKeypoints(src1,keypoints_1,img_keypoints_1,Scalar(0,255,0),DrawMatchesFlags::DEFAULT);drawKeypoints(src2, keypoints_2, img_keypoints_2, Scalar(0, 255, 0), DrawMatchesFlags::DEFAULT);//显示效果图namedWindow("特征点检测效果图1", WINDOW_KEEPRATIO);namedWindow("特征点检测效果图2", WINDOW_KEEPRATIO);imshow("特征点检测效果图1", img_keypoints_1);imshow("特征点检测效果图2", img_keypoints_2);waitKey(0);/*while (1);*/ return 0;
}
myopencv.h
#pragma once
#pragma comment(lib,"../lib/opencv_world341.lib") //Opencv的静态链接库
#pragma comment(lib,"../lib/opencv_world341d.lib")
#pragma comment(lib,"../lib/opencv_calib3d341d.lib")
#pragma comment(lib,"../lib/opencv_core341d.lib")
#pragma comment(lib,"../lib/opencv_dnn341d.lib")
#pragma comment(lib,"../lib/opencv_features2d341d.lib")
#pragma comment(lib,"../lib/opencv_flann341d.lib")
#pragma comment(lib,"../lib/opencv_highgui341d.lib")
#pragma comment(lib,"../lib/opencv_imgcodecs341d.lib")
#pragma comment(lib,"../lib/opencv_imgproc341d.lib")
#pragma comment(lib,"../lib/opencv_ml341d.lib")
#pragma comment(lib,"../lib/opencv_objdetect341d.lib")
#pragma comment(lib,"../lib/opencv_photo341d.lib")
#pragma comment(lib,"../lib/opencv_shape341d.lib")
#pragma comment(lib,"../lib/opencv_stitching341d.lib")
#pragma comment(lib,"../lib/opencv_superres341d.lib")
#pragma comment(lib,"../lib/opencv_video341d.lib")
#pragma comment(lib,"../lib/opencv_videoio341d.lib")
#pragma comment(lib,"../lib/opencv_videostab341d.lib")
#pragma comment(lib,"../lib/opencv_xfeatures2d341d.lib")#include <iostream>
#include <windows.h>
#include <vector>
#include "../include/opencv2/opencv.hpp"
#include "../include/opencv2/core/core.hpp"
#include "../include/opencv2/highgui/highgui.hpp"
#include "../include/opencv2/imgproc/imgproc.hpp"
#include "../include/opencv2/imgproc/imgproc_c.h"
#include "../opencv_contrib-3.4.1/modules/xfeatures2d/include/opencv2/xfeatures2d/nonfree.hpp"
#include "../opencv_contrib-3.4.1/modules/xfeatures2d/include/opencv2/xfeatures2d.hpp"
#include "../include/opencv2/features2d/hal/interface.h"//#define MYCOUTMAT //是否选择重载输出MAT
using namespace cv; //opencv的命名空间
using namespace std;#ifdef MYCOUTMATostream& operator << (ostream& output, Mat& M);
#endif // MYCOUTMATvoid DisPlayPixelValue_PtrWay(Mat img, int start_pixelrow, int strat_pixelcol, int num_row, int num_col);
int DisPlayPixelValue_PiontWay(Mat img, int row, int col, int channel);
2.根据上面两个文件讲一讲我遇到的问题
首先建议大家下载一个everything,帮助查找文件,有些文件可能并不是没生成,而是生成的地方和别人不一样!!!
(1)nonfree.hpp和xfeatures2d.hpp文件在哪
在实现SURF时要用到这两个.hpp文件,而这两个文件不在opencv文件夹中,而是在opencv_contrib-3.4.1文件夹中,可参考我的路径找找。除此之外你可能还有修改一下这两个文件里头文件的include路径,例如下面,当然这个不是都一样的,取决于对应文件在个人电脑中的路径,这让人头大。
nonfree.hpp
原本是
#include<opencv2/features2d.hpp>
但根据需要我改成了
#include "../../../../../../include/opencv2/features2d/features2d.hpp"
两个.hpp文件找到后,并修改其中的include路径,就能定义SurfFeatureDetector类了。
//定义一个SurfFeatureDetector(SURF) 特征检测类对象Ptr<cv::xfeatures2d::SurfFeatureDetector>detector = cv::xfeatures2d::SurfFeatureDetector::create(minHessian);
(2)错误 LNK2019 无法解析的外部符号 “public: static struct cv::Ptr __cdecl cv::xfeatures2d::SURF::create(double,int,int,bool,bool)” (?create@SURF@xfeatures2d@cv@@SA?AU?$Ptr@VSURF@xfeatures2d@cv@@@3@NHH_N0@Z)
①找到问题
在成功引用了nonfree.hpp和xfeatures2d.hpp文件后,编写代码就没有遇到什么问题了,但是运行后爆出上面的问题,脑子直接炸了。
百度后发现不少人有这个问题,参见https://blog.csdn.net/weixin_38383877/article/details/88417475
问题是
opencv_xfeatures2d320d.lib
opencv_features2d320d.lib
在哪里?
②生成这两个.lib
生成这两lib,关键在于如何使用cmake,这里推荐两个博主文章做参考,它们在细节上稍有区别,大家根据自己情况选择,我也不清楚为什么,玄学的很
https://blog.csdn.net/weixin_45645281/article/details/114937793
https://blog.csdn.net/weixin_42012977/article/details/82992962
参考了他们的文章,我在配置时做了这两件事:1)OPENCV_EXTRA_MODULES_PATH中
添加opencv_contrib-3.4.1/opencv_contrib-3.4.1/modules的路径;2)WITH_CUDA未勾选
在cmake中Configure和generate完后就可以Open Project了,这里推荐一篇博主文章
https://blog.csdn.net/excelNo1/article/details/118142075
看其中的第十步骤,编译CMakeTargets中的INSTALL,最后窗口会生成144项(我只有133不知道为什么),反正最后能生成我们要的.lib就行了
接着我们到工程的install文件夹下就能看到很多lib文件了,其中就有我们需要的。
建议将这些.lib文件都添加到自己的工程里。另外有时在cmake配置时因opencv_ffmpeg.dll、opencv_ffmpeg_64.dll和ffmpeg_version.cmake这三个文件下载失败,而无法生成opencv_xfeatures2d341d.lib,可以参考这两篇文章
https://www.cnblogs.com/zss0502/p/12694737.html
https://blog.csdn.net/qq_34801642/article/details/105577349
到此为止,.lib生成完毕,然而程序还是跑不了,接着往下看
③由于找不到opencv_features2d341d.dll……程序无法执行
在cmake生成lib文件时也会生成很多dll文件,它们在install文件夹的bin目录下,在运行程序时需要调用它们。
解决方法是把bin目录添加到电脑的系统变量PATH里,我是复制了bin文件夹放到opencv里在添加到系统变量PATH里
这个感觉和安装opencv时添加build\x64\vc15\bin的路径的目的是一样的,因为build\x64\vc15\bin目录下也有不少dll文件,例如opencv_world341d.dll
完成以上工作,最后SURF程序就可以运行了。