鉴于VS自己体积的庞大和项目依赖管理方式的不便,因此本文采用Clion经过CMake进行项目结构管理。php
安装以上软件,基本都是一路 next ,最后作一下系统环境变量的配置(右击我点电脑->属性->高级系统设置->环境变量->系统变量->编辑Path,添加对应软件bin所在目录,中间分号分隔)。最后打开Clion,依次进入Setting->Build,Execution,Deployment->Toolchains,设置编译用工具链:html
ps:编译好的OpenCV MSVC版及 MinGW版ios
MSVC版c++
下载的 OpenCV 文件夹会有:git
使用 MSVC 的话,直接在 build/x64/vc14 里面就有了,配置好路径便可使用
OpenCV 没有为咱们编译好 MinGW 版,因此只能用户自行编译,下面就是介绍 MinGW 版的编译流程以及中间可能遇到的错误的排除github
这里有须要的话能够直接下载使用我编译好了的 OpenCV-3.4.1 x64windows
(ps:这里若是你以前安装了Anaconda或者Python,请把这两个软件暂时从环境变量中删除,只是删除系统Path中的存在便可,不是卸载软件。缘由是后面编译会有冲突)bash
其中:source目录对应你的opencv/sources目录,binary目录就是以前新建的那个mingw-build存放目录多线程
此处须要复制opencv->build->bin下的两个文件:opencv_ffmpegxxx.dll、opencv_ffmpegxxx_64.dll到opencv/sources/3rdparty/ffmpeg/目录下ide
注意:碰见红色以后再次点击Configure(等因而要点两次),等到全部列表变白,没有红色一片的时候才表示成功
mingw32-make -j2 # 以2线程进行编译,这个数字根据本身硬件合理选择
mingw32-make install
添加环境变量
至此,OpenCV编译和相关设置已经完成,只须要正常调用便可使用。下面给一个简单摄像头调用示例:
CMakeLists.txt
cmake_minimum_required(VERSION 3.0) project(detect) set(CMAKE_CXX_STANDARD 11) add_executable(detect main.cpp) FIND_PACKAGE(OpenCV REQUIRED) IF (OpenCV_FOUND) INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIRS}) TARGET_LINK_LIBRARIES(detect ${OpenCV_LIBS}) ELSE (OpenCV_FOUND) MESSAGE(FATAL_ERROR "OpenCV library not found") ENDIF (OpenCV_FOUND)
main.cpp
#include <opencv2/highgui.hpp> #include <opencv2/imgproc.hpp> #include <iostream> int main() { cv::VideoCapture capture(0); if (!capture.isOpened()) { std::cout << "open camera error!" << std::endl; return -1; } cv::Mat frame; while (1) { capture >> frame; if (frame.empty()) { std::cout << "capture empty frame" << std::endl; continue; } cv::Mat shrink_frame; cv::resize(frame, shrink_frame, cv::Size(frame.cols / 2, frame.rows / 2), 0, 0, 3); cv::imshow("detect", shrink_frame); int key = cv::waitKey(1); if (key == 'q') { break; } } return 0; }
多线程编译错误信息不明确
表现:
若是使用了多线程编译,致使错误,可是错误信息不明确,如:
Makefile:161: recipe for target 'all' failed mingw32-make: *** [all] Error 2
解决:
使用单线程make,以查看详细的错误提示,再根据具体状况解决
mingw32-make
RC 错误
表现:
... windres.exe: unknown option -- W ...
或者:
FORMAT is one of rc, res, or coff, and is deduced from the file name extension if not specified. A single file name is an input file. No input-file is stdin, default rc. No output-file is stdout, default rc.
解决:
在 cmake-gui 编译配置中:不勾选 ENABLE PRECOMPILED HEADERS,而后从新Configure-Generate-mingw32-make
sprintf instead use StringCbPrintfA or StringCchPrintfA 错误
表现:
...opencv/sources/modules/videoio/src/cap_dshow.cpp... ... 'sprintf_instead_use_StringCbPrintfA_or_StringCchPrintfA' was not declared in this scope ...
解决:
修改E:OpenCV_3.3.1opencvsourcesmodulesvideoiosrccap_dshow.cpp文件,在#include "DShow.h"这行的上面加一行#define NO_DSHOW_STRSAFE,如:
#define NO_DSHOW_STRSAFE #include "DShow.h"
而后从新Configure-Generate-mingw32-make
identifier ‘nullptr’ is a keyword in C++11 错误
表现:
D:\opencv-3.4.1\opencv-3.4.1\3rdparty\protobuf\src\google\protobuf\stubs\io_win32.cc:94:3: warning: identifier 'nullptr' is a keyword in C++11 [-Wc++0x-compat] return s == nullptr || *s == 0; D:\opencv-3.4.1\opencv-3.4.1\3rdparty\protobuf\src\google\protobuf\stubs\io_win32.cc: In function 'bool google::protobuf::internal::win32::{anonymous}::null_or_empty(const char_type*)': D:\opencv-3.4.1\opencv-3.4.1\3rdparty\protobuf\src\google\protobuf\stubs\io_win32.cc:94:15: error: 'nullptr' was not declared in this scope return s == nullptr || *s == 0; ^ 3rdparty\protobuf\CMakeFiles\libprotobuf.dir\build.make:412: recipe for target '3rdparty/protobuf/CMakeFiles/libprotobuf.dir/src/google/protobuf/stubs/io_win32.cc.obj' failed mingw32-make[2]: *** [3rdparty/protobuf/CMakeFiles/libprotobuf.dir/src/google/protobuf/stubs/io_win32.cc.obj] Error 1 CMakeFiles\Makefile2:710: recipe for target '3rdparty/protobuf/CMakeFiles/libprotobuf.dir/all' failed mingw32-make[1]: *** [3rdparty/protobuf/CMakeFiles/libprotobuf.dir/all] Error 2 Makefile:161: recipe for target 'all' failed mingw32-make: *** [all] Error 2
解决:
在 cmake-gui 编译配置中:
勾选 ENABLE_CXX11
而后从新Configure-Generate-mingw32-make
dnn build error (Assembler messages)
表现:
C:\Users\ADMINI~1\AppData\Local\Temp\ccHIQxbw.s: Assembler messages: C:\Users\ADMINI~1\AppData\Local\Temp\ccHIQxbw.s:21800: Error: invalid register for .seh_savexmm ... modules\dnn\CMakeFiles\opencv_dnn.dir\build.make:1741: recipe for target 'modules/dnn/CMakeFiles/opencv_dnn.dir/layers/layers_common.avx512_skx.cpp.obj' failed mingw32-make[2]: *** [modules/dnn/CMakeFiles/opencv_dnn.dir/layers/layers_common.avx512_skx.cpp.obj] Error 1 CMakeFiles\Makefile2:4124: recipe for target 'modules/dnn/CMakeFiles/opencv_dnn.dir/all' failed mingw32-make[1]: *** [modules/dnn/CMakeFiles/opencv_dnn.dir/all] Error 2 Makefile:159: recipe for target 'all' failed mingw32-make: *** [all] Error 2
解决:
在 cmake-gui 编译配置中:
DCPU_DISPATCH 选空白
而后从新Configure-Generate-mingw32-make
(ps:这个错误在最新的OpenCV master分支已修复)
ref:
dnn build error via Mingw64