Qt creator 5.0.1 with OpenCv 2.3.4 on windows [clo

2019-02-11 06:28发布

问题:

I have looked for a good guide for days but I can't find one for the latest releases of Qt and OpenCv. I heard that they are both very good so I would like to make a project with these two. I am a beginner with Qt and OpenCv, so can you tell me step by step?

回答1:

I've been struggling with this for quite a while, and got it working this morning. I figured I'd give back to this wonderful community (since it has helped me on numerous occations) by posting my findings. Enjoy!

I've spent a lot of time trying to get qt5 and opencv 2.4.3 to work (so long actually that they released opencv 2.4.4 before i got it to work.) I've been looking at not a few tutorials, but the best one by far has been this one, so major cred and a lot of thanks:

http://www.laganiere.name/opencvCookbook/chap1s1_2.shtml

I can not stress how much this tutorial helped me, and I'm writing this guide as a complement. Mostly I'm providing updated paths to the compilers and libraries and describe how to handle the errors I got along the way. Use the cookbook tutorial for pictures and more in-detail instructions.

But as I hinted, things were not painless and went according to instructions, so I figured I'd share what I did to solve the problems and hopefully this will help someone.

NOTE: I'm not completely sure what I'm doing half of the time, when in doubt I just tend to follow my gut, so if someone with more knowledge and experience spots something that's inaccurate, please tell me and I will correct it. This is simply what I did, and it has worked out for me, I'm now building openCV programs in qt creator 5 using C++.

I used the following paths:

Qt: C:\software\Qt

OpenCV unzip directory: c:\software\opencv-win

OpenCV installed directory: c:\software\opencvMinGW

Downloads:

Ok, so for starters, download the latest qt library and creator with MinGW (in my case 5.0.1)

Then download openCV, which in my case was 2.4.4

Download CMake (at this moment version 2.8.10.2)

Installations:

Install Qt (This will take quite a while).

Run the openCV exe which will extract all the files you need. As stated earlier, I used C:\software\opencv-win as the location to where to extract them.

Install CMake. Make sure to select "Add CMake to system path for all users" (or similar).

Add C:\software\qt\5.0.1\mingw47_32\bin (or equivalent) to path in System variables (How to do this is described in the guide I linked in the beginning)

RESTART COMPUTER.

Compiling OpenCV:

Start Cmake-gui. Under "Where is the source code:" You browse to where you unzipped opencv (in my case C:\software\opencv-win), and in "Where to build the binaries", you enter where you want the finished libraries (I used c:\software\opencvMinGW), then click generate and create directory. At the popup window, you choose "MinGW makefiles" and "Specify native compilers". At C, for me the path became "C:/software/qt/Tools/MinGW/bin/gcc.exe", and for C++ "C:/software/qt/Tools/MinGW/bin/g++.exe", you enter accordingly where you installed Qt.

Press configure.

This is where things got funky for me. I got an error saying:

"CMake Error: CMake was unable to find a build program corresponding to "MinGW Makefiles". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool. CMake Error: CMake was unable to find a build program corresponding to "MinGW Makefiles". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool. CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly. Missing variable is: CMAKE_CXX_COMPILER_ENV_VAR CMake Error: Could not find cmake module file:C:/software/opencvMinGWblah/CMakeFiles/2.8.10.2/CMakeCXXCompiler.cmake CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly. Missing variable is: CMAKE_C_COMPILER_ENV_VAR CMake Error: Could not find cmake module file:C:/software/opencvMinGWblah/CMakeFiles/2.8.10.2/CMakeCCompiler.cmake Configuring incomplete, errors occurred!"

I suspected this had to do with the make-program. Duh. But how to fix it?

I resolved it by checking the "Advanced" button, finding the entry CMAKE_MAKE_PROGRAM and specifying the path "C:/software/qt/Tools/MinGW/bin/mingw32-make.exe"

All is well that ends well. Or is it?

Check the "Grouped" button, open the WITH category, check "WITH_QT". Press configure. Another chunk of errors:

CMake Warning at C:/software/CMake 2.8/share/cmake-2.8/Modules/FindQt4.cmake:615 (message): C:/software/qt/5.0.1/mingw47_32/bin/qmake.exe reported QT_INSTALL_LIBS as "C:/software/qt/5.0.1/mingw47_32/lib" but QtCore could not be found there. Qt is NOT installed correctly for the target build environment. Call Stack (most recent call first):
cmake/OpenCVFindLibsGUI.cmake:8 (find_package) CMakeLists.txt:377 (include)

So, as one might suspect from the error message, the program can't find the libraries. I remedied this as follows. Expand the categori QT, find the value QT_QTCORE_LIBRARY_DEBUG, enter

"C:/software/qt/5.0.1/mingw47_32/bin/Qt5Cored.dll" and just below it you'll find QT_QTCORE_LIBRARY_RELEASE, enter "C:/software/qt/5.0.1/mingw47_32/bin/Qt5Core.dll" NOW, you can press configure (hopefully) without warnings and after that you can press Generate and everything will be dandy and you can follow the cookbook tutorial.

Btw, these are the additional path variables I added to System Variables. I dunno, maybe a couple of them are unneccesary but I'm not about to start messing with them when everything finally works.

C:\software\qt\Tools\MinGW\bin;
C:\software\Qt\Tools\MinGW;
C:\software\opencvMinGW\install\bin;

And don't forget that you need to change the DLL names to 244 instead of 231, like this:

LIBS += -LC:\\software\\opencvMinGW\\install\\lib \
    -lopencv_core244.dll \
    -lopencv_highgui244.dll \
    -lopencv_imgproc244.dll \
    -lopencv_features2d244.dll \
    -lopencv_calib3d244.dll

I really hope this helps someone!

EDIT: Above link doesn't work anymore. This link can be helpful (https://wiki.qt.io/How_to_setup_Qt_and_openCV_on_Windows)