可以直接從這裡下載別人構建好的
https://github.com/huihut/OpenCV-MinGW-Build
--------------------------------------------------------------------------
1、下載並安裝QT5.12或更高版本,勾選MingW x64
2、把qt bin目錄加入PATH:
C:\Qt\Qt5.12.1\5.12.1\mingw73_64\bin
C:\Qt\Qt5.12.1\Tools\mingw730_64\bin
3、下載opencv4.0或更高版本,解壓為 D:\software\opencv\sources
4、下載CMake3.14或更高版本,安裝時選擇把bin目錄加入PATH
5、安裝python2.7.16,安裝時選擇把bin目錄加入PATH
6、安裝JDK1.8
7、啟動CMake,輸入原始碼和構建目錄:
原始碼目錄:D:\software\opencv\sources
構建目錄:D:\software\opencv\build
點選左下角的Configuration按鈕,選擇構建模式為MingGW makefiles,選擇 Native Compiler,選擇C和C++編輯器的位置:
C:\Qt\Qt5.12.1\Tools\mingw730_64\bin\gcc.exe
C:\Qt\Qt5.12.1\Tools\mingw730_64\bin\g++.exe
然後等待配置。
8、配置完成後,勾選WITH_OPENGL和WITH_QT兩項,選擇CMAKE_BUILD_TYPE為Release,再次configure。
9、正式開始構建
cd D:\software\opencv\build
mingw32-make -j 8
mingw32-make install
-------------------------------------------------------------------------------------------------
新建一個QT專案,修改.pro檔案,加入:
INCLUDEPATH += D:\software\OpenCV-MinGW-Build-OpenCV-4.0.1-x64\include
LIBS += D:\software\OpenCV-MinGW-Build-OpenCV-4.0.1-x64\x64\mingw\bin\libopencv_core401.dll
LIBS += D:\software\OpenCV-MinGW-Build-OpenCV-4.0.1-x64\x64\mingw\bin\libopencv_highgui401.dll
LIBS += D:\software\OpenCV-MinGW-Build-OpenCV-4.0.1-x64\x64\mingw\bin\libopencv_imgproc401.dll
LIBS += D:\software\OpenCV-MinGW-Build-OpenCV-4.0.1-x64\x64\mingw\bin\libopencv_calib3d401.dll
LIBS += D:\software\OpenCV-MinGW-Build-OpenCV-4.0.1-x64\x64\mingw\bin\libopencv_imgcodecs401.dll
LIBS += D:\software\OpenCV-MinGW-Build-OpenCV-4.0.1-x64\x64\mingw\bin\libopencv_videoio401.dll
修改main.cpp,如下:
#include "widget.h" #include <QApplication> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> using namespace cv; using namespace std; int main(int argc, char *argv[]) { Mat image = imread("C:\\Users\\Administrator\\Desktop\\1.bmp", 0); namedWindow("image"); imshow("image", image); Mat contours; Canny(image, //灰度圖 contours, //輸出輪廓 125, //低閾值 350); //高閾值 //因為正常情況下輪廓是用非零畫素表示 我們反轉黑白值 Mat contoursInv; //反轉後的影像 threshold(contours, contoursInv, 128, //低於該值的畫素 255, //將變成255 THRESH_BINARY_INV); namedWindow("contoursInv"); imshow("contoursInv", contoursInv); waitKey(0); return 0; }