環境:

windows : win10 64bit

opencv: opencv 3.0

cmake: cmake-3.11.1-win32-x86

QT: QT 5.5.0

1、cmake安裝

安裝完Cmkae,並安裝設定為 for all users。

2、新增系統環境變數

新增系統環境變數 

(1)、  D:\Qt\Qt5.5.0\Tools\mingw492_32\bin

(2)、D:\Qt\Qt5.5.0\Tools\mingw492_32\i686-w64-mingw32\lib

(3)、D:\Qt\Qt5.5.0\5.5\mingw492_32\bin  

否則會出現如下錯誤:

CMake Error at C:/Program Files (x86)/CMake 3.11/share/cmake-3.11/Modules/FindQt4.cmake:1382 (message):
  Found unsuitable Qt version "" from NOTFOUND, this code requires Qt 4.x
Call Stack (most recent call first):
  CMakeLists.txt:13 (FIND_PACKAGE)

3、開啟cmake-gui.exe並配置

       1)source code: E:\Work_station\opencv\opencv\sources

          where to build the binaries:  E:\Work_station\opencv\opencv\qt_build

       2)點選 configure 

          選擇 MinGW Makefiles 和 Specify native compilers, 點選next

          C:D:\Qt\Qt5.5.0\Tools\mingw492_32\bin\gcc.exe

          C++:D:\Qt\Qt5.5.0\Tools\mingw492_32\bin\g++.exe

          最後一個空著,點選finish

    3)在搜尋框依次查詢WITH_QT, WITH_TBB, WITH_IPP, WITH_CUDA, CMAKE_BUILD_TYPE,確保

WITH_QT -  選中.

WITH_TBB, WITH_IPP, WITH_CUDA -  不選

          CMAKE_BUILD_TYPE - 點選並輸入Debug              

   4) 點選configure,done後點擊generate,關閉CMake

4、 編譯

          • 進入cmd,切換到  E:\Work_station\opencv\opencv\qt_build目錄

輸入mingw32-make

可能錯誤:

modules\videoio\CMakeFiles\opencv_videoio.dir\build.make:187: recipe for target 'modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_dshow.cpp.obj' failed
mingw32-make[2]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_dshow.cpp.obj] Error 1
CMakeFiles\Makefile2:2867: recipe for target 'modules/videoio/CMakeFiles/opencv_videoio.dir/all' failed
mingw32-make[1]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/all] Error 2
Makefile:161: recipe for target 'all' failed

解決辦法:

cap_dshow.cpp 檔案中#include "precomp.hpp"後新增

#define STRSAFE_NO_DEPRECATE   並 使用#註釋掉OpenCVCompilerOptions.cmake檔案中

的 add_extra_compiler_option(-Werror=non-virtual-dtor) 。

5、新增環境變數

 將D:\Qt\Qt5.5.0\Tools\QtCreator\bin加入系統環境變數

6、拷貝QT工程所需要的檔案

 1)、拷貝原win編譯下的build資料夾下的include資料夾(包含opencv和opencv2)到out_file;

2)、拷貝qt_build資料夾下的lib和bin資料夾到out_file。

(備註: 即out_file資料夾下最終有三個資料夾)

7、QT專案中pro配置(pro檔案末尾新增)

INCLUDEPATH+=E:\Work_station\opencv\opencv\out_file\include\
             E:\Work_station\opencv\opencv\out_file\include\opencv\
              E:\Work_station\opencv\opencv\out_file\include\opencv2

LIBS+=E:\Work_station\opencv\opencv\out_file\lib\libopencv_videostab300d.dll.a\
E:\Work_station\opencv\opencv\out_file\lib\libopencv_stitching300d.dll.a\
E:\Work_station\opencv\opencv\out_file\lib\libopencv_calib3d300d.dll.a\
E:\Work_station\opencv\opencv\out_file\lib\libopencv_features2d300d.dll.a\
E:\Work_station\opencv\opencv\out_file\lib\libopencv_superres300d.dll.a\
E:\Work_station\opencv\opencv\out_file\lib\libopencv_objdetect300d.dll.a\
E:\Work_station\opencv\opencv\out_file\lib\libopencv_shape300d.dll.a\
E:\Work_station\opencv\opencv\out_file\lib\libopencv_imgcodecs300d.dll.a\
E:\Work_station\opencv\opencv\out_file\lib\libopencv_video300d.dll.a\
E:\Work_station\opencv\opencv\out_file\lib\libopencv_core300d.dll.a\
E:\Work_station\opencv\opencv\out_file\lib\libopencv_imgproc300d.dll.a\
E:\Work_station\opencv\opencv\out_file\lib\libopencv_videoio300d.dll.a\
E:\Work_station\opencv\opencv\out_file\lib\libopencv_flann300d.dll.a\
E:\Work_station\opencv\opencv\out_file\lib\libopencv_highgui300d.dll.a\
E:\Work_station\opencv\opencv\out_file\lib\libopencv_photo300d.dll.a\
E:\Work_station\opencv\opencv\out_file\lib\libopencv_ml300d.dll.a

8、測試程式

#include "mainwindow.h"
#include <QApplication>

#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui/highgui.hpp"

#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char *argv[])
{
      Mat src_image;
      src_image = imread("F:\\lena.jpg", IMREAD_COLOR);
      if (src_image.empty())
         return -1;
       imshow("src_image", src_image);
       waitKey(0);
       return 0;
}

錯誤1 :在編寫完步驟8 的程式後,測試時,一直提示 imread函式undefined。但是實際標頭檔案也是包含的。

解決辦法: 新增的LIB不完整。最後補全就好。

錯誤2 :執行程式碼時,提示 qt opencv 程式異常結束

解決辦法: 

1)、首先確保你把你qt編譯器下的所有bin目錄新增到了系統的path環境中; 

2)、把你編譯好的opencv目錄下的bin目錄新增到系統的path環境中; 

3)、這個時候重新編譯你的程式碼,如果不行,那就關機再試一次,絕對可以的。

參考連結:

https://blog.csdn.net/combfish/article/details/21445847

https://blog.csdn.net/qq_21078557/article/details/78962135