如何在CLion上配置使用OpenCV?

我的CLion是用mingw的,请问大家是不是可以只配置cmakelists就能运行opencv程序了啊?我试了好几天都失败了。用mingw build也失败了。有谁成功过吗?原理到底是怎么样的呀?都要疯了。。。
关注者
5
被浏览
3066

4 个回答

环境:windows 10 专业版 64bit + MinGW 3.22 + cmake 3.8.1 + clion 2017.1 + opencv3.20

准备工作:

Cmake下载:

cmake.org/download/ (下载页面)

cmake.org/files/v3.8/cm (cmake-3.8.1-win64-x64)

opencv下载:

opencv.org/releases.htm (下载页面)

sourceforge.net/project (opencv-win/3.2.0)

正式开始:

假设你已经安装配置好cmake、mingw,并且已经下载了文件opencv-3.2.0-vc14.exe

1. 打开opencv-3.2.0-vc14.exe,提取文件


完成后:



2. 打开CMake选择源代码目录为D:/opencv/sources,完成操作后将文件保存到 D:\opencv\mingw-build


点击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

github.com/opencv/openc 中找到原因,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 中的条件编译便可。具体可以查看github.com/opencv/openc

我直接下载了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<opencv2/opencv.hpp>
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

我的环境是Ubuntu 16.06 + OpenCV 2.4.13。Cmake官网可以查到OpenCV的配置方式。

find_package(OpenCV REQUIRED)

target_link_libraries(${OpenCV_LIBS})

这两个加到CmakeLists里就可以了。