鑑於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