Visual Studio 2022配置OpenCV455+CMake(Win11)
记录下CMake配置过程。
1 下载
1.1 OpenCV下载
(1)Github源码:https://github.com/opencv/opencv 选择4.5.5版本。
(2)Github扩展源码:https://github.com/opencv/opencv_contrib 选择4.5.5版本。
(3)下载后得到的是两个压缩包,解压。
1.2 CMake下载
官网下载:https://cmake.org/download/ 选择Windows x64 Installer版。
2 CMake编译
(1)第一次配置
选择源码、生成路径,VS2022版本x64,等待完成。
(2)调整配置
1.勾选 OPENCV_EXTRA_NONFREE 项
2.勾选BUILD_opencv_world 项
3.添加OPENCV_EXTRA_MODULES_PATH:E:/Tools/OpenCV/opencv_contrib-4.5.5/modules(即 opencv_contrib/modules 路径)
4.Configure,等待完成
3 遇到的警告及解决方案
3.1 ippicv无法下载
CMake Warning at cmake/OpenCVDownload.cmake:202 (message):
IPPICV: Download failed: 6;"Couldn't resolve host name"
For details please refer to the download log file:
E:/Tools/OpenCV/OpenCV-4.5.5-Build/CMakeDownloadLog.txt
Call Stack (most recent call first):
3rdparty/ippicv/ippicv.cmake:37 (ocv_download)
cmake/OpenCVFindIPP.cmake:259 (download_ippicv)
cmake/OpenCVFindLibsPerf.cmake:12 (include)
CMakeLists.txt:732 (include)
解决方案:参考
- 打开E:\Tools\OpenCV\OpenCV-4.5.5-Build文件夹下的CMakeDownloadLog.txt找到
#do_unpack "ippicv_2020_win_intel64_20191018_general.zip" "879741a7946b814455eee6c6ffde2984" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/a56b6ac6f030c312b2dce17430eef13aed9af274/ippicv/ippicv_2020_win_intel64_20191018_general.zip" "E:/Tools/OpenCV/OpenCV-4.5.5-Build/3rdparty/ippicv"
#check_md5 "E:/Tools/OpenCV/opencv-4.5.5/.cache/ippicv/879741a7946b814455eee6c6ffde2984-ippicv_2020_win_intel64_20191018_general.zip"
https://raw.githubusercontent.com/opencv/opencv_3rdparty/a56b6ac6f030c312b2dce17430eef13aed9af274/ippicv/ippicv_2020_win_intel64_20191018_general.zip
为下载地址,在https://ghproxy.com/的搜索框输入即可下载- 重命名为
879741a7946b814455eee6c6ffde2984-ippicv_2020_win_intel64_20191018_general.zip
- 将重命名后的文件放入E:\Tools\OpenCV\opencv-4.5.5.cache\ippicv中
3.2 ffmpeg无法下载
CMake Warning at cmake/OpenCVDownload.cmake:202 (message):
FFMPEG: Download failed: 6;"Couldn't resolve host name"
For details please refer to the download log file:
E:/Tools/OpenCV/OpenCV-4.5.5-Build/CMakeDownloadLog.txt
Call Stack (most recent call first):
3rdparty/ffmpeg/ffmpeg.cmake:20 (ocv_download)
modules/videoio/cmake/detect_ffmpeg.cmake:14 (download_win_ffmpeg)
modules/videoio/cmake/init.cmake:7 (include)
modules/videoio/cmake/init.cmake:11 (add_backend)
cmake/OpenCVModule.cmake:298 (include)
cmake/OpenCVModule.cmake:361 (_add_modules_1)
cmake/OpenCVModule.cmake:385 (ocv_glob_modules)
CMakeLists.txt:927 (ocv_register_modules)
解决方案:
- opencv_videoio_ffmpeg.dll:
https://raw.githubusercontent.com/opencv/opencv_3rdparty/4d348507d156ec797a88a887cfa7f9129a35afac/ffmpeg/opencv_videoio_ffmpeg.dll
重命名为eece4ec8304188117ffc7d5dfd0fc0ae-opencv_videoio_ffmpeg.dll
- opencv_videoio_ffmpeg_64.dll:
https://raw.githubusercontent.com/opencv/opencv_3rdparty/4d348507d156ec797a88a887cfa7f9129a35afac/ffmpeg/opencv_videoio_ffmpeg_64.dll
重命名为20deefbfe023c8b8d11a52e5a6527c6a-opencv_videoio_ffmpeg_64.dll
- ffmpeg_version.cmake:
https://raw.githubusercontent.com/opencv/opencv_3rdparty/4d348507d156ec797a88a887cfa7f9129a35afac/ffmpeg/ffmpeg_version.cmake
右键另存为8862c87496e2e8c375965e1277dee1c7-ffmpeg_version.cmake
- 将这三个文件放入E:\Tools\OpenCV\opencv-4.5.5.cache\ffmpeg中
3.3 wechat_qrcode无法下载
与1.3.1和1.3.2操作类似。
全部完成后点Generate,等待完成。
4 Visual Studio编译
(1)生成后点Open Project,选择Debug x64或Release x64。
(2)找到ALL_BUILD,右键生成,等待完成。
(3)找到INSTALL,右键,仅当前项目,只生成 INSTALL,等待完成。
(4)E:\Tools\OpenCV\OpenCV-4.5.5-Build\install 该路径为环境配置所需。
5 环境配置
1.添加系统环境变量E:\Tools\OpenCV\OpenCV-4.5.5-Build\install\x64\vc17\bin
2.新建项目属性表(这里配置的是Release版)
3.VC++目录–包含目录添加路径:
E:\Tools\OpenCV\OpenCV-4.5.5-Build\install\include
E:\Tools\OpenCV\OpenCV-4.5.5-Build\install\include\opencv2
4.VC++目录–库目录添加路径:
E:\Tools\OpenCV\OpenCV-4.5.5-Build\install\x64\vc17\lib
5.链接器–输入–附加依赖项添加:
opencv_world455.lib(Release版)
opencv_world455d.lib(Debug版)
6 测试
#include <opencv2/opencv.hpp>
#include <opencv2/core/utils/logger.hpp>//日志头文件
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
//utils::logging::setLogLevel(utils::logging::LOG_LEVEL_SILENT);//不输出日志
//utils::logging::setLogLevel(utils::logging::LOG_LEVEL_ERROR);//只输出错误日志
Mat src = imread("C:/Users/12421/Desktop/Lena.jpg");
if (src.empty())
{
cout << "please cheak image path" << endl;
return -1;
}
Mat src_x, src_y, src_xy;
flip(src, src_x, 0);//沿x轴对称
flip(src, src_y, 1);//沿y轴对称
flip(src, src_xy, -1);//沿x轴对称,沿y轴对称
imshow("src", src);
imshow("src_x", src_x);
imshow("src_y", src_y);
imshow("src_xy", src_xy);
waitKey(0);
return 0;
}
原图:
结果:
7 其他
1.帮助文档下载:https://docs.opencv.org/
2.OpenCV455Debug版本运行时CMD框会显示很多日志,关闭的方法:参考
添加头文件#include <opencv2/core/utils/logger.hpp>
main()函数里添加语句cv::utils::logging::setLogLevel(utils::logging::LOG_LEVEL_SILENT);
3.Visual Studio编译中第二步出现opencv_test_objdetect test_qrcode_encode.cpp错误,打开并删除日文注释即可。参考