前提准备
win10-clion环境准备下载opencv源码
1、下载最新版本Opencv4软件压缩包
2、解压
得到:
编译
生成Makefile
在cmake里面找到OPENCV_ENABLE_ALLOCATOR_STATS或者在search里面搜索,然后把后面的勾选去掉
点击Configurate
错误1:
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:/software/opencv/install/CMakeDownloadLog.txtCall 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)
解决方法
进入 网站查询真实地址。打开C:\Windows\System32\drivers\etc\hosts,(需要使用管理员权限打开)
追加199.232.96.133 如下:
重新Configure
, 配置成功。
配置成功之后选择Generate
:
表示生成了Makefile
编译
生成好Makefile之后,我们开始编译Opencv源代码
即可打开cmd,然后执行命令mingw32-make install
等待编译完成
配置环境变量
编译成功之后我们只需要将opencv\build\include
,opencv\install\bin
,opencv\install\lib
目录添加到环境变量中即可
clion中开始使用
cmake_minimum_required(VERSION 3.15)project(untitled)set(CMAKE_CXX_STANDARD 14)# Find OpenCV, you may need to set OpenCV_DIR variable# to the absolute path to the directory containing OpenCVConfig.cmake file# via the command line or GUI# set(OpenCV_DIR "E:\\software\\opencv\\build\\x64\\vc15\\lib")find_package( OpenCV REQUIRED)# If the package has been found, several variables will# be set, you can find the full list with descriptions# in the OpenCVConfig.cmake file.# Print some message showing some of themmessage(STATUS "OpenCV library status:")message(STATUS " version: ${OpenCV_VERSION}")message(STATUS " libraries: ${OpenCV_LIBS}")message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")# Add OpenCV headers location to your include pathsinclude_directories( ${OpenCV_INCLUDE_DIRS} )# Declare the executable target built from your sourcesadd_executable(${PROJECT_NAME} main.cpp )target_link_libraries( ${PROJECT_NAME} ${OpenCV_LIBS})
#include <stdio.h>#include <opencv2/opencv.hpp>using namespace cv;int main(int argc, char** argv ){Mat image;image = imread( "C:\\Users\\oceanstar\\Pictures\\Camera Roll\\test.jpg", 1 );if ( !image.data ){printf("No image data \n");return -1;}namedWindow("Display Image", WINDOW_AUTOSIZE );imshow("Display Image", image);waitKey(0);return 0;}
编译运行:
> mkdir build> cd build> CMake -G "MinGW Makefiles" ../> mingw32-make