If you are reading this post it means you have already developed your program/software/application in OpenCV command line interface. And now you want to develop its GUI. Qt is famous for developing GUI applications using C++ forms which are no more supported in Microsoft Visual Studio. Above all Qt is cross-platform you need a very small modification and your program will have the ability to run on linux or android or any other platform. If you haven’t install Qt yet. Goto their website https://www.qt.io/download/ and download the open-source version.
Before installing make sure you have already installed Microsoft Visual Studio and OpenCV already set –up and running. If you haven’t done this yet please see this article.
Install Qt Creator Community and it will take few minutes depending on your Internet Speed and Performance of your computer.
After installing Qt, run the program and select Create New Project. A window will open and from there you select Qt Widget project as shown below:
Name your project whatever you want. Let’s say display image. I will also guide you how to show image or video inside Qt application using OpenCV. See the link at the end of this article.
After naming your application it will ask you to select your Kit. The Kit contains set of compilers. In Windows operating system Microsoft Visual C++ and MinGw compiler for C language are more popular. If you are using MinGW compiler than you can follow this tutorial but here I have used Microsoft Visual C++ Compiler. If you have installed Qt after installing Microsoft Visual Studio than Qt will automatically detects the appropriate compiler, In this case its MSVC2012.
Select Next and it will ask you to enter details. Leave as it is if you are not sure what to do. Or if you are expert programmer than you know what these classes names present.
After finishing creating your project. You will have 3 folders and one file with .pro extension. This .pro file is the configuration file like the project uses dynamic libraries and the paths to OpenCV libraries etc.
Place the following code at the end of this pro file.
win32 {
INCLUDEPATH += "C:\\opencv\\build\\include" \
CONFIG(debug,debug|release) {
LIBS += -L"C:\\opencv\\build\\x86\\vc11\\lib" \
-lopencv_core2410d \
-lopencv_highgui2410d \
-lopencv_imgproc2410d \
-lopencv_features2d2410d \
-lopencv_contrib2410d \
-lopencv_flann2410d \
-lopencv_gpu2410d \
-lopencv_legacy2410d \
-lopencv_ml2410d \
-lopencv_nonfree2410d \
-lopencv_objdetect2410d \
-lopencv_photo2410d \
-lopencv_stitching2410d \
-lopencv_superres2410d \
-lopencv_ts2410d \
-lopencv_video2410d \
-lopencv_videostab2410d \
-lopencv_calib3d2410d \
}
CONFIG(release,debug|release) {
DEFINES += QT_NO_WARNING_OUTPUT QT_NO_DEBUG_OUTPUT
LIBS += -L"C:\\opencv\\build\\x86\\vc11\\lib" \
-lopencv_core2410 \
-lopencv_highgui2410 \
-lopencv_imgproc2410 \
-lopencv_features2d2410 \
-lopencv_contrib2410 \
-lopencv_flann2410 \
-lopencv_gpu2410 \
-lopencv_legacy2410 \
-lopencv_ml2410 \
-lopencv_nonfree2410 \
-lopencv_objdetect2410 \
-lopencv_photo2410 \
-lopencv_stitching2410 \
-lopencv_superres2410 \
-lopencv_ts2410 \
-lopencv_video2410 \
-lopencv_videostab2410 \
-lopencv_calib3d2410 \
}
}
The above code tells the .pro file the path to OpenCV libraries. Please change path if you have installed OpenCV somewhere else.
And that’s it you are done. You have finally connected OpenCV with Qt creator. Now you can import OpenCV libraries in Qt in mainwindow.cpp and start developing. To display image using OpenCV and Qt please see this article.
If you have any problems or suggestions please drop a comment below: