In this tutorial, I will tell you how I used OpenCV in my Qt application in Windows. (I use Windows 7)
1- Download Qt Creator for Windows. I installed the one with MinGW.
Qt 5.1.0 for Windows 32-bit (MinGW 4.8, OpenGL, 666 MB)
2- Download MinGW. Install.
3- Download CMake. Install.
4- Download OpenCV Source. I installed OpenCV 2.4.6
Extract the files somewhere. I directly extracted to C:\. When files are extracted installer will close by itself.
You should have the souce folder like this:
5- Now creating MinGW make files by cmake-gui. Run Cmake gui from program files.
Select the folder for source files as C:\opencv
Select the folder for build files as C:\opencv-mingw
6- Click Configure Button. It will ask for generators. Select “MinGW Makefiles” and “Specify native compiler” options.
7- Select compilers from Qt creator folder where you installed. Compilers are in Tool folder under mingw48_32/bin
For C select: C:/Qt/Qt5.1.0/Tools/mingw48_32/bin/gcc.exe
For C++ select: C:/Qt/Qt5.1.0/Tools/mingw48_32/bin/g++.exe
8- Now cmake will prepare the configurations. In this part I got several errors in my previous attempts. One if them was about python library. Something like pyhton25.dll not found – i don’t remember. I saw a comment saying “opencv only works with python lower versions than 3. So I erased existing python library. I installed new one python 2.7. Restarted cmake process again. It worked without errors this time.
I got an output like this: Click Expand Code to see all of it.
The CXX compiler identification is GNU 4.8.0
The C compiler identification is GNU 4.8.0
Check for working CXX compiler: C:/Qt/Qt5.1.0/Tools/mingw48_32/bin/g++.exe
Check for working CXX compiler: C:/Qt/Qt5.1.0/Tools/mingw48_32/bin/g++.exe -- works
Detecting CXX compiler ABI info
Detecting CXX compiler ABI info - done
Check for working C compiler: C:/Qt/Qt5.1.0/Tools/mingw48_32/bin/gcc.exe
Check for working C compiler: C:/Qt/Qt5.1.0/Tools/mingw48_32/bin/gcc.exe -- works
Detecting C compiler ABI info
Detecting C compiler ABI info - done
We will choose options we want for OpenCV build. For my build I deselected WITH_CUDA. Other than that I didn’t change anything. Now Click “Generate” button to generate makefiles.
If you got no error in generating makefiles you can close cmake. Now you should have a build directory like this:
9- Now we are at the compiling stage. Open MinGW Shell. Go to your build directory and type make command like this:
This will take some time.. Go kill some titans http://fenglee.com/game/aog/
Hopefully if you can compile without any errors you will get a screen like this:
10- Now make install.
11- Now you should have dll files in your opencv install folder like this: C:\opencv-mingw\install\bin
If you have them that’s great! You have installed OpenCV.
12- Now lets switch to Qt. Open Qt Creator. If you have already have a project just add these lines to your .pro file for adding OpenCV libraries:
INCLUDEPATH += C:\opencv-mingw\install\include
LIBS += -L"C:/opencv-mingw/install/bin"
LIBS += -lopencv_core246 -lopencv_highgui246 -lopencv_imgproc246
What we do here is:
1- define the include path.
2- define the lib path
3- and tell which libraries you are going to use by library names.
library names of opencv : you can use dll names without “lib” and add “-l” in front. for example: libopencv_videostab246.dll => -lopencv_videostab246
Now you are good to go!
If you dont already have a project in Qt with OpenCV, just now I prepared a simple sample project for you people who wants to use OpenCV in Qt applications. I included a nice function to convert Mat images to QImage for displaying them in Qt Gui.
Here you can download it an run it if you followed this post until here.
You can find the code On GitHub
Repository : https://github.com/AlpX/Qt_Creator-OpenCV
qt opencv edge detection
I hope this was useful to someone. I struggled a lot during my trials.
This post was too long, so I will tell about more on Qt, OpenCV, and libraries in another post. Happy coding