1.前期准备工作
整个流程: cmake配置与生成 -> VS编译生成与installer ->
重新配置opencv/include/libraries/附加依赖项
opencv github首页地址:https://github.com/opencv/
默认安装了visual studio 2019 – window10 X64
下载Opencv4.5.2 点击下载; 【国内镜像下载】
下载opencv4.5.2扩展模块(opencv_contrib-4.5.2) 点击下载;
百度网盘:https://pan.baidu.com/s/1ZNgBW-KgHPGSOLUHeBae9w?pwd=iq5o 提取码:iq5o
下载Cmake3.21 点击下载对应的平台即可;
将Opencv4.5.2解压到指定盘(D:\opencv4.5.2);
将opencv4.5.2扩展模块解压到指定盘(D:\opencv_contrib-4.5.2);
将Cmake3.21解压到指定盘(D:\cmake3.21);
二、开始Cmake编译
1、打开Cmake软件, 开始配置;
2、添加Opencv源码的存放位置 -> 选择Cmake编译后文件保存的位置 -> 配置
3、选择安装的VS的版本 -> 点击finish -> 等待配置完成;
4、点击configure,会出现大片红色,这里,先更改红色中的部分选项:(使用search快速查找)
① (可选) 勾选 BUILD_opencv_world 主要是把所有的lib文件都弄到一个opencv_world450d.lib中方便配置。
若安装了Anaconda 则不勾选,否则 opencv编译之后找不到opencv_worldxxx.lib。
②勾选 OPENCV_ENABLE_NONFREE 是为了在编译成功后可以使用具有专利保护的算法,如果该变量不被选中,就不能使用具有专利保护的算法;
③找到 OPENCV_EXTRA_MODULES_PATH 变量,选择我们刚才下载的opencv_contrib安装包里的modules文件夹。如果这个变量为空,只是安装了OpenCV的基础版。
去除不需要编译的模块(可选)
输入 test 查找 OPENCV_PERF_TESTS、BUILD_TESTS、BUILD_opencv_python_tests 不勾选:
输入 java 查找BUILD_JAVA、BUILD_opencv_java_bingdings_generator不勾选:
输入 python 查找BUILD_opencv_python3、BUILD_opencv_python_bingdings_generator 不勾选:
④ 查看CMakeDownloadLog文件;将日志中出现https的下载链接,即需要下载的文件手动下载并重命名MD5-文件名放至D:\opencv-4.5.2.cache\暂存文件夹 github文件下载加速(limit20M)、 或者在此加速 、或者在此加速
若你编译的版本与我的一样直接使用:百度网盘共享地址 链接 , 提取码:voq1
⑤再次点击【Configure】直到所有红色消失后点击【Generate】开始编译,编译成功会在 D:\opencv_build 下生成许多文件
⑤再次点击【Configure】直到所有红色消失后点击【Generate】开始编译,编译成功会在 D:\opencv_build 下生成许多文件
三、VS2019编译OpenCV源代码
点击 【open project】
点击【生成】-> 【批生成】,勾划 ALL BUILD 与 INSTALL,点击右侧【生成】
右击 CMakeTargets 下的 INSTALL ,点击 【仅用于项目】->【仅生成INSTALL】:
CMake中定义的安装目录(生成文件位置)
四、配置到到项目中
此时编译完成就绪:
配置OpenCV环境变量
【此电脑 -> 属性 -> 高级系统设置 -> 环境变量 -> 系统变量 -> 找到Path -> 编辑 -> 新建】,
添加环境变量D:\opencv_build\install\x64\vc16\bin ->确定。(将之前使用的opencv环境变量删除)
把新的库文件配置到到项目中
vs2019配置opencv
新建一个空项目,点击 ”项目“ -> “属性” 。
VC++目录 :
包含目录:D:\opencv_build\install\include,D:\opencv_build\install\include\opencv2
库 目 录 :D:\opencv_build\install\x64\vc16\lib
链接器–>输入–>附加依赖项,添加: (注意添加的库与编译选项要一致,需要注意debug比release的文件名多了个d),使用命令行tree /f 输出文件名,方便复制粘贴。
复制*.lib文件,*452d.lib为debug版本,共104项
opencv_aruco452.lib
opencv_aruco452d.lib
opencv_bgsegm452.lib
opencv_bgsegm452d.lib
opencv_bioinspired452.lib
opencv_bioinspired452d.lib
opencv_calib3d452.lib
opencv_calib3d452d.lib
opencv_ccalib452.lib
opencv_ccalib452d.lib
opencv_core452.lib
opencv_core452d.lib
opencv_datasets452.lib
opencv_datasets452d.lib
opencv_dnn452.lib
opencv_dnn452d.lib
opencv_dnn_objdetect452.lib
opencv_dnn_objdetect452d.lib
opencv_dnn_superres452.lib
opencv_dnn_superres452d.lib
opencv_dpm452.lib
opencv_dpm452d.lib
opencv_face452.lib
opencv_face452d.lib
opencv_features2d452.lib
opencv_features2d452d.lib
opencv_flann452.lib
opencv_flann452d.lib
opencv_fuzzy452.lib
opencv_fuzzy452d.lib
opencv_gapi452.lib
opencv_gapi452d.lib
opencv_hdf452.lib
opencv_hdf452d.lib
opencv_hfs452.lib
opencv_hfs452d.lib
opencv_highgui452.lib
opencv_highgui452d.lib
opencv_imgcodecs452.lib
opencv_imgcodecs452d.lib
opencv_imgproc452.lib
opencv_imgproc452d.lib
opencv_img_hash452.lib
opencv_img_hash452d.lib
opencv_intensity_transform452.lib
opencv_intensity_transform452d.lib
opencv_line_descriptor452.lib
opencv_line_descriptor452d.lib
opencv_mcc452.lib
opencv_mcc452d.lib
opencv_ml452.lib
opencv_ml452d.lib
opencv_objdetect452.lib
opencv_objdetect452d.lib
opencv_optflow452.lib
opencv_optflow452d.lib
opencv_phase_unwrapping452.lib
opencv_phase_unwrapping452d.lib
opencv_photo452.lib
opencv_photo452d.lib
opencv_plot452.lib
opencv_plot452d.lib
opencv_quality452.lib
opencv_quality452d.lib
opencv_rapid452.lib
opencv_rapid452d.lib
opencv_reg452.lib
opencv_reg452d.lib
opencv_rgbd452.lib
opencv_rgbd452d.lib
opencv_saliency452.lib
opencv_saliency452d.lib
opencv_shape452.lib
opencv_shape452d.lib
opencv_stereo452.lib
opencv_stereo452d.lib
opencv_stitching452.lib
opencv_stitching452d.lib
opencv_structured_light452.lib
opencv_structured_light452d.lib
opencv_superres452.lib
opencv_superres452d.lib
opencv_surface_matching452.lib
opencv_surface_matching452d.lib
opencv_text452.lib
opencv_text452d.lib
opencv_tracking452.lib
opencv_tracking452d.lib
opencv_video452.lib
opencv_video452d.lib
opencv_videoio452.lib
opencv_videoio452d.lib
opencv_videostab452.lib
opencv_videostab452d.lib
opencv_wechat_qrcode452.lib
opencv_wechat_qrcode452d.lib
opencv_xfeatures2d452.lib
opencv_xfeatures2d452d.lib
opencv_ximgproc452.lib
opencv_ximgproc452d.lib
opencv_xobjdetect452.lib
opencv_xobjdetect452d.lib
opencv_xphoto452.lib
opencv_xphoto452d.lib
五、测试
由于sift专利过期,sift已经移到主库了,故代码如下:无需#include <opencv2/xfeatures2d.hpp>
void QuickDemo::sift_Demo(Mat& image) {
int numFeature = 500;
Mat img_descriptors;
cv::Ptr<SIFT> detector = SIFT::create(numFeature);
vector<KeyPoint> KeyPoint1;
detector->detectAndCompute(image, Mat(), KeyPoint1, img_descriptors);
drawKeypoints(image, KeyPoint1, img_descriptors, Scalar(0, 255, 255));
imshow("img_descriptors", img_descriptors);
}
从本篇文章开始,此后文章中opencv以及其拓展模块以此版本为准。
鸣谢
编译时下载报错解决:https://blog.csdn.net/weixin_43851329/article/details/114335959
编译错误处理:
错误1:CMake Warning at cmake/OpenCVGenSetupVars.cmake:54 (message):
去除勾选:OPENCV_GENERATE_SETUPVARS
错误2:CMake Warning at cmake/OpenCVDetectCXXCompiler.cmake:147 (message): OpenCV does not recognize MSVC_VERSION “1923”
排查出报错的位置。在 opencv-x.x.x\cmake\OpenCVDetectCXXCompiler.cmake 文件的147行。
详情查看:https://blog.csdn.net/qq_34851605/article/details/104432994