1. 程式人生 > >QT 下使用 opencv with_QT

QT 下使用 opencv with_QT

 window 下使用opencv 且opencv支援QT



軟體環境
Qt-5.9.3(mingw530)
MinGW-5.1.0-32bit(下載的QT裡面有)
CMake-3.11.3
OpenCV-2.4.9

1 編譯opencv WITH_QT 
  官網上下載下來編譯好的opencv,不支援QT
  因此需要自己編譯
 
2 下載的 OpenCV 資料夾會有:
  build (已編譯好的庫,使用 MSVC 的話,直接在)
  sources (原始碼)
  QT使用的是MinGW 版,因此需要自己編譯,
  MinGW 版的opencv(支援QT)編譯流程
 (1)CMake 自行安裝
 (2)MinGW 可以直接使用qt裡面的


  開啟 cmake-gui,設定原始碼和生成路徑:
  Where is the source code: E:/OpenCV_2.4.9/opencv/sources
  Where to build the binaries: E:/OpenCV_2.4.9/opencv-build(名字隨便,不要與已有的build重合就行)
 
  點選 Configure,設定編譯器
  Specify the generator for this project: MinGW Makefiles
  Specify native compilers
  Next
  Compilers C: E:\Qt\Qt5.9.3\Tools\mingw530_32\bin\gcc.exe
  Compilers C++: E:\Qt\Qt5.9.3\Tools\mingw530_32\bin\g++.exe
  Finish
  
  此時出現一系列紅色,
    勾選 WITH_QT
    勾選 WITH_OPENGL
    不勾選 WITH_IPP
 
  再次點選 Configure,出現下列設定選項
  設定 QT_MAKE_EXECUTABLE 為 E:\Qt\Qt5.9.3\5.9.3\mingw53_32\bin\qmake.exe
  設定 Qt5Concurrent_DIR 為 E:\Qt\Qt5.9.3\5.9.3\mingw53_32\lib\cmake\Qt5Concurrent
  設定 Qt5Core_DIR 為 E:\Qt\Qt5.9.3\5.9.3\mingw53_32\lib\cmake\Qt5Core
  設定 Qt5Gui_DIR 為 E:\Qt\Qt5.9.3\5.9.3\mingw53_32\lib\cmake\Qt5Gui 0
  設定 Qt5Test_DIR 為 E:\Qt\Qt5.9.3\5.9.3\mingw53_32\lib\cmake\Qt5Test
  設定 Qt5Widgets_DIR 為 E:\Qt\Qt5.9.3\5.9.3\mingw53_32\lib\cmake\Qt5Widgets
  設定 Qt5OpenGL_DIR 為 E:\Qt\Qt5.9.3\5.9.3\mingw53_32\lib\cmake\Qt5OpenGL

  點選 Generate 生成 Makefile

  **編譯 OpenCV**
  進入到你之前設定的目錄(E:/OpenCV_2.4.9/opencv-build)
  直接執行 E:\Qt\Qt5.9.3\Tools\mingw530_32\bin\mingw32-make
  (最好設定E:\Qt\Qt5.9.3\Tools\mingw530_32\bin 為系統變數)
  如果編譯有問題,自行百度,我是一氣呵成的

  編譯好了進行安裝
  執行 mingw32-make install
  
  最好大功完成
  G:\paper\opencv249\build\install\x64\mingw\bin 下生成*249.dll
  G:\paper\opencv249\build\install\x64\mingw\lib  下生成*249.dll.a

  G:\paper\opencv249\build\install\x64\mingw\bin 設定到系統變數裡面
  
3  QT使用編譯好的opencv
   .po 檔案中設定
    INCLUDEPATH += G:\paper\opencv249\build\install\include
    INCLUDEPATH += G:\paper\opencv249\build\install\include\opencv
    INCLUDEPATH += G:\paper\opencv249\build\install\include\opencv2



LIBS +=G:\paper\opencv249\build\install\x64\mingw\lib\libopencv_core249.dll.a\
      G:\paper\opencv249\build\install\x64\mingw\lib\libopencv_calib3d249.dll.a\
      G:\paper\opencv249\build\install\x64\mingw\lib\libopencv_contrib249.dll.a\
      G:\paper\opencv249\build\install\x64\mingw\lib\libopencv_core249.dll.a\
      G:\paper\opencv249\build\install\x64\mingw\lib\libopencv_features2d249.dll.a\
      G:\paper\opencv249\build\install\x64\mingw\lib\libopencv_flann249.dll.a\
      G:\paper\opencv249\build\install\x64\mingw\lib\libopencv_gpu249.dll.a\
      G:\paper\opencv249\build\install\x64\mingw\lib\libopencv_highgui249.dll.a\
      G:\paper\opencv249\build\install\x64\mingw\lib\libopencv_imgproc249.dll.a\
      G:\paper\opencv249\build\install\x64\mingw\lib\libopencv_legacy249.dll.a\
      G:\paper\opencv249\build\install\x64\mingw\lib\libopencv_ml249.dll.a\
      G:\paper\opencv249\build\install\x64\mingw\lib\libopencv_objdetect249.dll.a\
      G:\paper\opencv249\build\install\x64\mingw\lib\libopencv_video249.dll.a\
      G:\paper\opencv249\build\install\x64\mingw\lib\libopencv_ocl249.dll.a


main檔案:
#include "opencv2/opencv.hpp"
const std::string input_file_name = "input.png";
cv::Mat image = imread(input_file_name);

如果出現
G:\01miwork\qtpro2\bandTest\main.cpp:11: error: undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&,

.po 檔案中設定
DEFINES += _GLIBCXX_USE_CXX11_ABI=0

記得重新構建