windows下Clion配置opencv

所需环境

MinGw + Cmake + Clion + opencv

安装MinGw

参考:install MinGw

安装Cmake

参考:install Cmake

Cmake下载网址:Cmake download

:Cmake最好安装跟Clion中配置一样的版本,省的麻烦

安装 opencv

  1. 下载地址 :opencv download

  2. 解压到 opencv4文件夹中

  3. 解压后:

  4. 配置环境变量:

Clion 配置

编译opencv源码

  1. 打开Cmake-GUI,选择源码路径和输出路径

  2. 点击Configure,选择MinGW Makefiles,点击Finish,开始编译

  3. 等待一段时间后,会有很多报红,再次点击Configure,红色消失,点击Generate

  4. 进入输出目录,在cmd 运行下面代码,等待完成

    1
    mingw32-make -j8

  5. 运行mingw32-make install,等待片刻,输出目录下会多出install文件夹

  6. 添加…\install\x64\mingw\bin 添加到path系统环境变量环境变量

编辑CMakeLists.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cmake_minimum_required(VERSION 3.13)
project(learnOpencv)

set(CMAKE_CXX_STANDARD 11)

# Where to find CMake modules and OpenCV
set(OpenCV_DIR "D:\\software\\opencv4\\MinGW64_build\\install")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})

add_executable(learnOpencv test.cpp)

# add libs you need
set(OpenCV_LIBS opencv_core opencv_imgproc opencv_highgui opencv_imgcodecs)
# linking
target_link_libraries(learnOpencv ${OpenCV_LIBS})

注意:opencv4必须要c++11支持

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <opencv2\opencv.hpp>

using namespace cv;

int main()
{
Mat img = imread("D:\\code-workspace\\Clion-workspace\\learnOpencv\\images\\1.png",WINDOW_AUTOSIZE);
namedWindow("刘亦菲");
imshow("刘亦菲", img);

waitKey(0);
return 0;
}