非常感谢知乎上的一位博主,详细记录了在windows系统clion上配置opencv4.2的过程,参考这里。
我的初衷应该和很多人一样,习惯了jetbrains的一些风格,而且十分厌恶VS占用的空间太大,所以就想另寻出路,在喜欢的clion上配置opencv环境。
前一段时间在ubuntu的clion上配置过opencv,但看论文什么的还都是在windows上,来回切换系统实在费劲,所以就想在windows上配置好环境。windows上配置此环境就不如在Ubuntu上如此顺利,遇到了一些列复杂的问题,我从昨天开始配置,一直弄到了今天下午结束,才算把这一过程熟悉,所以在此记录一下配置的过程,以备后用,如果能帮助到看到此blog的你,那你就感谢我和原博主吧。
下面的所有截图是我在按照原博主编译opencv4.2和我后来重新编译opencv4.5时的,可能有一些对应不上,但版本问题影响不大,看的时候稍微注意一下,祝你顺利!
在 github 上下载 opencv 的源码,我下载的版本也是和原博主下的一样(刚开始怕配置失败,和原博主保持了一致),但版本应该问题不大。
- opencv 源码 github地址
- opencv_contrib 源码 github地址
下载完并解压之后放在同一目录下(D:\OpenCV),如下图所示:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ITd3Halm-1610479952380)(D:\Typora\cpz笔记\笔记中的图片\下载opencv和contrib.png)]
编译器用来编译c++代码,运行c++代码必须依靠编译器,所以需要一个编译器,这里选择mingw编译器。
MinGW 官网下载地址
(照搬原博主的解释):
所以这里选择下载的是x86_64-posix-seh。(我用的是我之前下载的 x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0,也可以正常使用)
然后将 mingw 加入到系统变量PATH(Path)中:
D:\mingw64\bin
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dAKPAMxM-1610479952384)(D:\Typora\cpz笔记\笔记中的图片\mingw的版本信息.png)]
下载地址:
CLion官网下载地址
安装过程在此不做赘述,很简单,不会的另外搜索。
主要是下载好之后,将mingw选择为clion的编译器,依次在CLion中选择:
File—> Settings —> Build,Execution,Development —> Toolchains
在右侧的Environment处选择mingw的安装路径( 红色椭圆 )即可:[ 蓝色处保持默认Bundled即可 ]
CMake 官网下载地址:
CMake 官网下载
下载时选择最新版本的上一个版本即可:
[ Binary distribution—> windows —> .msi 的安装文件 ]
安装时,都保持默认选项即可。
按原博主的说法,如果之前曾安装过Anaconda或者python,请将这两个从系统环境变量Path/PATH 中先删除,不用卸载软件,因为在编译时会有冲突!!!别不信,我刚开始忘记删除,在编译时会跳红。
在OpenCV目录下新建mingw的build目录—minGW-build,用来保存make生成的一系列文件,在 2.1 下载opencv源码的同一目录下新建即可,如下图所示:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XlT2ryjP-1610479952389)(D:\Typora\cpz笔记\笔记中的图片\下载opencv和contrib.png)]
如果你是默认安装的CMake,那其路径应该是在:
C:\Program Files\CMake\bin
找到 cmake-gui.exe 双击运行。
下载原博主在其中分享的 网盘文件,然后将其拷贝到以下目录:< 这里注意版本,如果你在一开始下载的都不是opencv4.2的话,这里别直接copy并替换;不同版本的OpenCV的缺失文件(有的还会缺失ffmpeg_version.cmake)和下载地址是不一样的,我们需要到CMakeDownloadLog.txt文件中自己查找。下载完成后重命名为:MD值+’-’+下载的文件名,拷贝到.cache相应目录下:>
D:\OpenCV\opencv-4.2.0\opencv-4.2.0
替换原文件夹中的.cashe文件夹即可。
在下图中分别选择OpenCV的源码所在路径(蓝色矩形框) 和 3.2中新建的build目录(粉色矩形框):
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4ffvvx4g-1610479952391)(D:\Typora\cpz笔记\笔记中的图片\CMake编译.png)]
点击configure,选择MinGW Makefiles,点击finish,等待configure完。
一般情况下,在第一次configure的过程结束之前,会出现下面的四个错误,这是因为在此过程中有几个文件需要下载,但是下载失败了:
出错1:
FFMPEG: Download: opencv_videoio_ffmpeg.dll
Try 1 failed
CMake Warning at cmake/OpenCVDownload.cmake:202 (message):
FFMPEG: Download failed: 35;“SSL connect error”For details please refer to the download log file:
D:/OpenCV/opencv450_clion/minGW-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:3 (include)
modules/videoio/cmake/init.cmake:22 (add_backend)
cmake/OpenCVModule.cmake:312 (include)
cmake/OpenCVModule.cmake:375 (_add_modules_1)
modules/CMakeLists.txt:7 (ocv_glob_modules)
出错2:
FFMPEG: Download: opencv_videoio_ffmpeg_64.dll
Try 1 failed
CMake Warning at cmake/OpenCVDownload.cmake:202 (message):
FFMPEG: Download failed: 35;“SSL connect error”For details please refer to the download log file:
D:/OpenCV/opencv450_clion/minGW-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:3 (include)
modules/videoio/cmake/init.cmake:22 (add_backend)
cmake/OpenCVModule.cmake:312 (include)
cmake/OpenCVModule.cmake:375 (_add_modules_1)
modules/CMakeLists.txt:7 (ocv_glob_modules)
出错3:
FFMPEG: Download: ffmpeg_version.cmake
Try 1 failed
CMake Warning at cmake/OpenCVDownload.cmake:202 (message):
FFMPEG: Download failed: 35;“SSL connect error”For details please refer to the download log file:
D:/OpenCV/opencv450_clion/minGW-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:3 (include)
modules/videoio/cmake/init.cmake:22 (add_backend)
cmake/OpenCVModule.cmake:312 (include)
cmake/OpenCVModule.cmake:375 (_add_modules_1)
modules/CMakeLists.txt:7 (ocv_glob_modules)
出错4:
CMake Warning at cmake/OpenCVGenSetupVars.cmake:54 (message):
CONFIGURATION IS NOT SUPPORTED: validate setupvars script in install
directory
Call Stack (most recent call first):
CMakeLists.txt:976 (include)
先说错误4,出现错误4,可以将上面的 OPENCV_GENERATE_SETUPVARS 勾框去掉:
从错误的提示信息也可以看出,这是在下载两个文件的过程中出现了问题,解决方法也很简单:
到对应build文件夹下( D:\OpenCV\opencv450_clion\minGW-build ) 的文件 CMakeDownloadLog.txt 中查找对应文件的下载地址;< 都说是要科学上网,我也是科学上网下的 >
将下载的文件名称修改一下,改为:MD-“下载的文件名”;
MD的值是 CMakeDownloadLog.txt 文件中提示的一串十六进制的数。
将修改完名字的文件移动到.cache文件夹下( D:\OpenCV\opencv450_clion\opencv-4.5.0\opencv-4.5.0.cache )
这里说一下,其实在我弄opencv450环境时,在对应的.cache文件夹下( D:\OpenCV\opencv450_clion\opencv-4.5.0\opencv-4.5.0.cache )是有这缺失的文件的,只不过对应的大小都是0;
所以,你在对应网址下载完对应的这两个文件之后,将其移动(or copy)到对应的.cache文件夹下,然后复制 初始的对应的文件的名字,然后将该文件删除,然后将该文件对应的下载的文件重命名–粘贴即可。
从错误三的提示信息也可以看出,是由于 ffmpeg_version.cmake 文件的问题,根据这篇博客的做法,我编译成功了。
简单说,
从对应CMakeDownloadLog.txt 文件中找到ffmpeg_version.cmake 的下载地址
进入此网站
鼠标右键—> 另存为
选择存储路径时,直接选择.cache文件夹下的ffmpeg下即可
- ==保存类型那里选择 所有文件 ===
- 文件名字那里—> 直接点击一下该文件夹下的类似这样的文件:f8e65dbe4a3b4eedc0d2997e07c3f3fd-ffmpeg_version.cmake,名字就改好了
- 然后保存,覆盖即可
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kHtYKEXH-1610479952404)(D:\Typora\cpz笔记\笔记中的图片\opencv_contrib选择.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iFoPoWNt-1610479952413)(D:\Typora\cpz笔记\笔记中的图片\选择 examples.png)]
完成3.4.2之后,再次点击configure,等待完成即可。
如果我没猜错的话,再次configure又出错了,因为我们刚刚添加了 opencv_contrib 的modules,所以这时会有类似下面这样的错误,因为添加了之后,会在.cache文件夹下多出几个文件夹——data、xfeatures2d:
xfeatures2d/boostdesc: Download: boostdesc_bgm.i
Try 1 failed
CMake Warning at D:/OpenCV/opencv450_clion/opencv-4.5.0/opencv-4.5.0/cmake/OpenCVDownload.cmake:202 (message):
xfeatures2d/boostdesc: Download failed: 35;“SSL connect error”For details please refer to the download log file:
D:/OpenCV/opencv450_clion/minGW-build/CMakeDownloadLog.txt
Call Stack (most recent call first):
D:/OpenCV/opencv450_clion/opencv_contrib-4.5.0/opencv_contrib-4.5.0/modules/xfeatures2d/cmake/download_boostdesc.cmake:22 (ocv_download)
D:/OpenCV/opencv450_clion/opencv_contrib-4.5.0/opencv_contrib-4.5.0/modules/xfeatures2d/CMakeLists.txt:11 (download_boost_descriptors)xfeatures2d/boostdesc: Download: boostdesc_bgm_bi.i
Try 1 failed
CMake Warning at D:/OpenCV/opencv450_clion/opencv-4.5.0/opencv-4.5.0/cmake/OpenCVDownload.cmake:202 (message):
xfeatures2d/boostdesc: Download failed: 35;“SSL connect error”For details please refer to the download log file:
D:/OpenCV/opencv450_clion/minGW-build/CMakeDownloadLog.txt
Call Stack (most recent call first):
D:/OpenCV/opencv450_clion/opencv_contrib-4.5.0/opencv_contrib-4.5.0/modules/xfeatures2d/cmake/download_boostdesc.cmake:22 (ocv_download)
D:/OpenCV/opencv450_clion/opencv_contrib-4.5.0/opencv_contrib-4.5.0/modules/xfeatures2d/CMakeLists.txt:11 (download_boost_descriptors)xfeatures2d/boostdesc: Download: boostdesc_bgm_hd.i
Try 1 failed
CMake Warning at D:/OpenCV/opencv450_clion/opencv-4.5.0/opencv-4.5.0/cmake/OpenCVDownload.cmake:202 (message):
xfeatures2d/boostdesc: Download failed: 35;“SSL connect error”For details please refer to the download log file:
D:/OpenCV/opencv450_clion/minGW-build/CMakeDownloadLog.txt
Call Stack (most recent call first):
D:/OpenCV/opencv450_clion/opencv_contrib-4.5.0/opencv_contrib-4.5.0/modules/xfeatures2d/cmake/download_boostdesc.cmake:22 (ocv_download)
D:/OpenCV/opencv450_clion/opencv_contrib-4.5.0/opencv_contrib-4.5.0/modules/xfeatures2d/CMakeLists.txt:11 (download_boost_descriptors)xfeatures2d/boostdesc: Download: boostdesc_binboost_064.i
Try 1 failed
CMake Warning at D:/OpenCV/opencv450_clion/opencv-4.5.0/opencv-4.5.0/cmake/OpenCVDownload.cmake:202 (message):
xfeatures2d/boostdesc: Download failed: 35;“SSL connect error”For details please refer to the download log file:
D:/OpenCV/opencv450_clion/minGW-build/CMakeDownloadLog.txt
Call Stack (most recent call first):
D:/OpenCV/opencv450_clion/opencv_contrib-4.5.0/opencv_contrib-4.5.0/modules/xfeatures2d/cmake/download_boostdesc.cmake:22 (ocv_download)
D:/OpenCV/opencv450_clion/opencv_contrib-4.5.0/opencv_contrib-4.5.0/modules/xfeatures2d/CMakeLists.txt:11 (download_boost_descriptors)xfeatures2d/boostdesc: Download: boostdesc_binboost_128.i
Try 1 failed
CMake Warning at D:/OpenCV/opencv450_clion/opencv-4.5.0/opencv-4.5.0/cmake/OpenCVDownload.cmake:202 (message):
xfeatures2d/boostdesc: Download failed: 35;“SSL connect error”For details please refer to the download log file:
D:/OpenCV/opencv450_clion/minGW-build/CMakeDownloadLog.txt
Call Stack (most recent call first):
D:/OpenCV/opencv450_clion/opencv_contrib-4.5.0/opencv_contrib-4.5.0/modules/xfeatures2d/cmake/download_boostdesc.cmake:22 (ocv_download)
D:/OpenCV/opencv450_clion/opencv_contrib-4.5.0/opencv_contrib-4.5.0/modules/xfeatures2d/CMakeLists.txt:11 (download_boost_descriptors)xfeatures2d/boostdesc: Download: boostdesc_binboost_256.i
Try 1 failed
CMake Warning at D:/OpenCV/opencv450_clion/opencv-4.5.0/opencv-4.5.0/cmake/OpenCVDownload.cmake:202 (message):
xfeatures2d/boostdesc: Download failed: 35;“SSL connect error”For details please refer to the download log file:
D:/OpenCV/opencv450_clion/minGW-build/CMakeDownloadLog.txt
Call Stack (most recent call first):
D:/OpenCV/opencv450_clion/opencv_contrib-4.5.0/opencv_contrib-4.5.0/modules/xfeatures2d/cmake/download_boostdesc.cmake:22 (ocv_download)
D:/OpenCV/opencv450_clion/opencv_contrib-4.5.0/opencv_contrib-4.5.0/modules/xfeatures2d/CMakeLists.txt:11 (download_boost_descriptors)xfeatures2d/boostdesc: Download: boostdesc_lbgm.i
Try 1 failed
CMake Warning at D:/OpenCV/opencv450_clion/opencv-4.5.0/opencv-4.5.0/cmake/OpenCVDownload.cmake:202 (message):
xfeatures2d/boostdesc: Download failed: 35;“SSL connect error”For details please refer to the download log file:
D:/OpenCV/opencv450_clion/minGW-build/CMakeDownloadLog.txt
Call Stack (most recent call first):
D:/OpenCV/opencv450_clion/opencv_contrib-4.5.0/opencv_contrib-4.5.0/modules/xfeatures2d/cmake/download_boostdesc.cmake:22 (ocv_download)
D:/OpenCV/opencv450_clion/opencv_contrib-4.5.0/opencv_contrib-4.5.0/modules/xfeatures2d/CMakeLists.txt:11 (download_boost_descriptors)xfeatures2d/vgg: Download: vgg_generated_48.i
Try 1 failed
CMake Warning at D:/OpenCV/opencv450_clion/opencv-4.5.0/opencv-4.5.0/cmake/OpenCVDownload.cmake:202 (message):
xfeatures2d/vgg: Download failed: 35;“SSL connect error”For details please refer to the download log file:
D:/OpenCV/opencv450_clion/minGW-build/CMakeDownloadLog.txt
Call Stack (most recent call first):
D:/OpenCV/opencv450_clion/opencv_contrib-4.5.0/opencv_contrib-4.5.0/modules/xfeatures2d/cmake/download_vgg.cmake:16 (ocv_download)
D:/OpenCV/opencv450_clion/opencv_contrib-4.5.0/opencv_contrib-4.5.0/modules/xfeatures2d/CMakeLists.txt:12 (download_vgg_descriptors)xfeatures2d/vgg: Download: vgg_generated_64.i
Try 1 failed
CMake Warning at D:/OpenCV/opencv450_clion/opencv-4.5.0/opencv-4.5.0/cmake/OpenCVDownload.cmake:202 (message):
xfeatures2d/vgg: Download failed: 35;“SSL connect error”For details please refer to the download log file:
D:/OpenCV/opencv450_clion/minGW-build/CMakeDownloadLog.txt
Call Stack (most recent call first):
D:/OpenCV/opencv450_clion/opencv_contrib-4.5.0/opencv_contrib-4.5.0/modules/xfeatures2d/cmake/download_vgg.cmake:16 (ocv_download)
D:/OpenCV/opencv450_clion/opencv_contrib-4.5.0/opencv_contrib-4.5.0/modules/xfeatures2d/CMakeLists.txt:12 (download_vgg_descriptors)xfeatures2d/vgg: Download: vgg_generated_80.i
Try 1 failed
CMake Warning at D:/OpenCV/opencv450_clion/opencv-4.5.0/opencv-4.5.0/cmake/OpenCVDownload.cmake:202 (message):
xfeatures2d/vgg: Download failed: 35;“SSL connect error”For details please refer to the download log file:
D:/OpenCV/opencv450_clion/minGW-build/CMakeDownloadLog.txt
Call Stack (most recent call first):
D:/OpenCV/opencv450_clion/opencv_contrib-4.5.0/opencv_contrib-4.5.0/modules/xfeatures2d/cmake/download_vgg.cmake:16 (ocv_download)
D:/OpenCV/opencv450_clion/opencv_contrib-4.5.0/opencv_contrib-4.5.0/modules/xfeatures2d/CMakeLists.txt:12 (download_vgg_descriptors)xfeatures2d/vgg: Download: vgg_generated_120.i
Try 1 failed
CMake Warning at D:/OpenCV/opencv450_clion/opencv-4.5.0/opencv-4.5.0/cmake/OpenCVDownload.cmake:202 (message):
xfeatures2d/vgg: Download failed: 35;“SSL connect error”For details please refer to the download log file:
D:/OpenCV/opencv450_clion/minGW-build/CMakeDownloadLog.txt
Call Stack (most recent call first):
D:/OpenCV/opencv450_clion/opencv_contrib-4.5.0/opencv_contrib-4.5.0/modules/xfeatures2d/cmake/download_vgg.cmake:16 (ocv_download)
D:/OpenCV/opencv450_clion/opencv_contrib-4.5.0/opencv_contrib-4.5.0/modules/xfeatures2d/CMakeLists.txt:12 (download_vgg_descriptors)data: Download: face_landmark_model.dat
Try 1 failed
CMake Warning at D:/OpenCV/opencv450_clion/opencv-4.5.0/opencv-4.5.0/cmake/OpenCVDownload.cmake:202 (message):
data: Download failed: 35;“SSL connect error”For details please refer to the download log file:
D:/OpenCV/opencv450_clion/minGW-build/CMakeDownloadLog.txt
Call Stack (most recent call first):
D:/OpenCV/opencv450_clion/opencv_contrib-4.5.0/opencv_contrib-4.5.0/modules/face/CMakeLists.txt:13 (ocv_download)CMake Warning at D:/OpenCV/opencv450_clion/opencv_contrib-4.5.0/opencv_contrib-4.5.0/modules/face/CMakeLists.txt:26 (message):
Face: Can’t get model file for face alignment.
这个时候,你应该很熟悉这个错误了吧,对,完全按照错误三的方式一个一个文件去做就可以了。奥里给,冲!
configure没有红字报错了,我真有点喜极而泣,已经基本完成了:
没有错误之后,点击generate。
打开cmd,并打开minGW-build 的目录,然后输入:
mingw32-make -j8
这一步是耗时最长的,等待编译完成。
如果你很不幸,在这个过程中又遇到了错误,如下图所示:
gcc: error: long: No such file or directory
解决方法:
在CMake里面把STATS的选项去掉(如下图)—> configure —> generate —> 继续编译mingw32-make -j8
成功后如下图:(绿色的看着好舒服)
在cmd中输入:
mingw32-make install
完成后如下图所示:
完成后会在minGW-build文件夹下有一个install的文件夹,这个文件夹是我们编译生成的OpenCV库。
已经基本大功告成!
将你的install之后的bin目录添加到 环境变量 Path 中:
D:\OpenCV\opencv450_clion\minGW-build\install\x64\mingw\bin
在系统变量中,新建名称为 OpenCV_DIR的变量,变量值如下:
D:\OpenCV\opencv450_clion\minGW-build\install
cmake_minimum_required(VERSION 3.17)
project(opencv450_test)
set(CMAKE_CXX_STANDARD 14)
set(OpenCV_DIR "D:\\OpenCV\\opencv450_clion\\minGW-build")
add_executable(opencv450_imshow_test opencv450_imshow_test.cpp)
add_executable(opencv450_version_test opencv450_version_test.cpp)
FIND_PACKAGE(OpenCV REQUIRED)
IF (OpenCV_FOUND)
INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(opencv450_imshow_test ${OpenCV_LIBS})
TARGET_LINK_LIBRARIES(opencv450_version_test ${OpenCV_LIBS})
ELSE (OpenCV_FOUND)
MESSAGE(FATAL_ERROR "OpenCV library not found")
ENDIF (OpenCV_FOUND)
#include
#include
using namespace cv;
int main()
{
// ================= 测试1. 输出opencv的版本信息 ===================
std::cout << "OpenCV version : " << CV_VERSION << std::endl;
std::cout << "Major version : " << CV_MAJOR_VERSION << std::endl;
std::cout << "Minor version : " << CV_MINOR_VERSION << std::endl;
std::cout << "Subminor version : " << CV_SUBMINOR_VERSION << std::endl;
}
#include
#include
using namespace cv;
int main()
{
// ================= 测试2. 读入一张图片进行imshow显示 ===================
// 图片路径换成本地的图片路径,注意是两个斜杠
Mat src = imread("D:\\Clion_Project\\opencv450_test\\messi5.jpg");
if (src.empty())
{
printf("could not load image...\n");
return -1;
}
namedWindow("input image", WINDOW_AUTOSIZE);
imshow("input image", src);
waitKey(0);
destroyAllWindows();
return 0;
}
若上述完成之后,在CLion中出现报错:(code 后面是一长串数字)
Process finished with exit code -1XXXXXXXX
解决方法:
在右上角Edit configuration
在Working directory处添加:
D:\OpenCV\opencv450_clion\minGW-build\install\x64\mingw\bin
如下图所示:
这个什么原因我不太清楚,后来我又重新编译opencv450时,没加上面的目录,也可以运行成功。
这最近一周的时间,就在这上面来回打转了,可以说学到了一些东西,但又觉得每学到什么东西。出现错误的地方,给的提示信息也足够多,网上也有各种解决方法,配置结束之后,心里算是迈过了一个重大的门槛,又突然想起在本科的时候用树莓派装opencv的环境遇到各种各样的问题,但无奈到最后 都没有解决,也没有实现一些当时奇奇怪怪的想法,想想也是有些遗憾。
最近又感觉外国人在某些方面比我们超前许多,各种办公软件、开源库、编程语言、一些颠覆性的理论知识等等,都是几乎诞生在外国的,而我们( 如果你不是,那就是我自己 )学的挺多东西,都是import、#include,然后调用对应的接口函数,虽然这很方便,但也给我们一种我们学会了很多东西的错觉,每每想到这里,我真的打心眼里感觉可惜、可怕、可笑。
上述只是胡说八道而已,不同意的就不同意吧,同意的就同意吧,whatever。