参考链接:如何在CLion上配置使用OpenCV? - 不相识不打的回答 - 知乎 https://www.zhihu.com/question/47331502/answer/166576437
环境:windows 10 专业版 64bit + MinGW 3.22 + cmake 3.8.1(cmake版本应选较新的版本比如这个版本) + clion 2017.1(我的是clion2021.2.3) + opencv3.20(可以是较新的版本,我自己用的4.5.4)
Cmake下载:
https://cmake.org/download/ (下载页面)
https://cmake.org/files/v3.8/cmake-3.8.1-win64-x64.msi (cmake-3.8.1-win64-x64)
opencv下载:
http://opencv.org/releases.html (下载页面)
https://sourceforge.net/projects/opencvlibrary/files/opencv-win/3.2.0/opencv-3.2.0-vc14.exe/download (opencv-win/3.2.0)
假设你已经安装配置好cmake、mingw,并且已经下载了文件opencv-3.2.0-vc14.exe
下载opencv:
点击windows选项会跳转到sourceforge网页,等待几秒后自动下载
下载即可
1. 打开opencv-3.2.0-vc14.exe,提取文件
完成后:
2. 打开CMake选择源代码目录为D:/opencv/sources,完成操作后将文件保存到 D:\opencv\mingw-build (这个mingw-build是新建的文件夹,用来保存cmake操作之后的文件)
点击Configure ,在弹出的窗口选择类型为MinGW Makefiles.
3.等待配置完成,按照个人需要选择相关的entry(比如可以去除python,doc等)。然后再次点击Configure,你会发现红色区域变白,此时可以点击Generate,等待操作完成。
4.接下来打开cmd,进入D:\opencv\mingw-build,输入mingw32-make,等待等待…(过程可能会稍微漫长一些)
我build了3次,第3次通过了,前两次都报错了。
Error 1:
D:\opencv\sources\modules\highgui\src\window_w32.cpp: In function 'int icvCreateTrackbar(const char*, const char*, int*, int, CvTrackbarCallback, CvTrackbarCallback2, void*)':
D:\opencv\sources\modules\highgui\src\window_w32.cpp:1853:81: error: 'BTNS_AUTOSIZE' was not declared in this scope
错误的原因是有几个变量没有找到,因为在mingw的头文件commctrl.h中,对于上面的变量宏的定义实际上是由_WIN32_IE是否定义来决定的,我们打开D:\MinGW\include\commctrl.h中的代码查看:
修改成:
Error 2:
Building CXX object modules/ts/CMakeFiles/opencv_ts.dir/src/ts_gtest.cpp.obj
D:\opencv\source\opencv\modules\ts\src\ts_gtest.cpp: In constructor 'testing::internal::Mutex::Mutex()':
D:\opencv\source\opencv\modules\ts\src\ts_gtest.cpp:8829:45: error: cannot convert 'CRITICAL_SECTION* {aka _RTL_CRITICAL_SECTION*}' to 'GTEST_CRITICAL_SECTION* {aka CRITICAL_SECTION*}' in initialization
在https://github.com/opencv/opencv/issues/8105 中找到原因,MinGW defines _CRITICAL_SECTION and _RTL_CRITICAL_SECTION as two separate (equivalent) structs, instead of using typedef.
修改文件D:\opencv\sources\modules\ts\include\opencv2\ts\ts_gtest.h 中的条件编译便可。具体可以查看https://github.com/opencv/opencv/pull/8123/files
我直接下载了github上的ts_gtest.h,覆盖了本地的ts_gtest.h
第三次build没有报错,接下来继续在cmd中输入mingw32-make install,该命令会生成一个install文件夹。
5.将D:\opencv\mingw-build\install\x86\mingw\bin 添加到path系统环境变量,重启
6.打开Clion,测试
首先编辑CMakeLists.txt
cmake_minimum_required(VERSION 3.7)
project(assigment)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
# Where to find CMake modules and OpenCV
set(OpenCV_DIR "D:\\opencv\\mingw-build\\install")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(test_cv main.cpp)
# add libs you need
set(OpenCV_LIBS opencv_core opencv_imgproc opencv_highgui opencv_imgcodecs)
# linking
target_link_libraries(test_cv ${OpenCV_LIBS})
测试代码:
#include "iostream"
#include
using namespace std;
using namespace cv;
int main() {
Mat img = imread("lena.jpg");
if (img.empty()) {
cout << "Error" << endl;
return -1;
}
imshow("Lena", img);
waitKey();
return 0;
}
结果如图
===========================================
参考:
[1] use OpenCV with Clion IDE on Windows
[2] OpenCV开发环境配置-Windows/MinGW/Clion/CMake - lhyz - 博客园
[3] Fix for #8105 (compiling with mingw32) by msk-repo01 · Pull Request #8123 · opencv/opencv