1. Playing with Images
1.1 Installing the OpenCV Library for Qt
The Qt SDK is probably the best tool to build cross-platform OpenCV applications. In addition, it offers nice UI tools and a complete API. Finally, it comes with Qt Creator, a friendly IDE for developing C++ projects. This recipe will show you, in details, how to install Qt, compile the OpenCV library for it and finally run your first OpenCV application.At the time of writing this recipe, the latest Qt version was 4.7.3 (Qt Creator 2.2.1). Current version of OpenCV was 2.3.1 (August 2011). We will also use CMake which was at version 2.8.5. The installation has been done on Windows 7. Please refer to the OpenCV Cookbook for more details.
-
The first step is obviously to
download Qt. Simply run the installer with all default option. Once installed, you can run Qt Creator.
-
Lets start by creating a simple Hello World application on the Windows console, just to make sure everyting is working properly. Click on
Create Project...
and chooseQt Console Application
.
We will call our projectmyHelloWorld
; place it in the location of your choice and clickNext
.
We create a Desktop application.
A default project is then created.
This one does nothing except launching the concole. Click on the green arrow and you will see it. -
Let’s now write a true “Hello World” application. Simply copy and paste the following C++ code over the preceding one.
#include <iostream> int main() { std::cout << "Hello my world!"; char var; std::cin >> var; return 1; }
And click again on the green arrow to run the new code.
If you obtain the result above, then everything is working well. Note that Qt automatically created a directory calledmyHelloWorld-build-desktop
where it puts all the compiled and executable files. This way, the source code and the binary files does not get mixed together. This is very useful, if you use a version control software such as Subversion in which you submit only the source directory. -
Let's now proceed to OpenCV installation. To download the OpenCV library, just go to the OpenCV official website at opencv.willowgarage.com.
You'll find there the current release version in a downloadable zip file or in a Windows install. In the case of version 2.3.1, a superpack installer is available:
Run it and extract it to the directory of your choice:
Once this is done, you now have all OpenCV source files in the specified directory. -
The next step is to compile the library for the compiler you want to use; here it will be the basic
mingw/g++
compiler that Qt installed by default. Just before we do this, let's include the folder that contains themake
command in ourPath
environment variable. Indeed, compiling the library will be done using thismake
utility command that Qt installed together with the compilers themselves. They are located inC:\QtSDK\mingw\bin
. Goto to your Control Panel (from theStart
menu) and to theSystem
menu.
In theAdvanced system settings
menu, you select theAdvanced
tab.
Click onEnvironment Variables...
In theUser variables
box, look for thePATH
variable. If it is there, click onEdit...
, if not click onNew...
to create it. This variable contains all the folder Windows will look in when you type a command. By setting it in the user variables, this definition is available to you only. If you want it to be valid for all users of your system then define it in theSystem variables
box. -
To build the library from the source files, OpenCV uses CMake, a cross-platform and open source tool designed to build library packages.
We need to install CMake. Go to cmake.org and download the Windows Win32 Installer.
-
Once CMake installed, you can start the gui-based application (
cmake-gui
)
In CMake, specify the directory containing the source code and the one that will contain the builds.
Click onConfigure
. This will create the output directory.
You then specify the compilers that will generate the project. In our case, they are the compilers of MinGW installed by default by Qt.
These are gcc and g++.
CMake now displays the different build options.
Select the build type, hereRelease
. If you wish, at the end, you can repeat the same process with theDebug
mode.
Since we want to use Qt, we also select theWITH_QT
option
Once your options selected, you click onConfigure
again.
And you click onGenerate
to complete the installation. -
Now that you have completed the installation, you are ready to compile the OpenCV library. Start the Windows
cmd console and go to the directory where you installed your builds. Type mingw32-make
Building everything will take time...
Once this built completed, you typemingw32-make install
This last step will install the library and the include files in theinstall
directory. Note that for clarity, you can rename this directory asrelease
since you ask CMake to build a Release install
We are done with the installation! Congratulations! -
Before we build our first OpenCV project, we need to add a few more folders to the
Path
environment variable. First, you need to tell your system where to find the OpenCV dlls. From our installation process, they are inC:\OpenCV-2.3.1\install\bin
.
The Qt dlls are also required; you should find them atC:\QtSDK\QtCreator\bin
-
Our last step is to build a simple OpenCV project to make sure everything is working properly. Start Qt and create a new Qt Console Application project called here
myFirstOpenCVProject
. Goto to theprojects
menu and select theRelease
build configuration.
Our test program will simply open and display an image:#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> int main() { // read an image cv::Mat image= cv::imread("img.jpg"); // create image window named "My Image" cv::namedWindow("My Image"); // show the image on window cv::imshow("My Image", image); // wait key for 5000 ms cv::waitKey(5000); return 1; }
The project file must specify the OpenCV headers and libraries locations:QT += core QT -= gui TARGET = myFirstOpenCVProject CONFIG += console CONFIG -= app_bundle TEMPLATE = app SOURCES += main.cpp INCLUDEPATH += C:\\OpenCV-2.3.1\\install\\include LIBS += -LC:\\OpenCV-2.3.1\\install\\lib \ -lopencv_core231.dll \ -lopencv_highgui231.dll \ -lopencv_imgproc231.dll \ -lopencv_features2d231.dll \ -lopencv_calib3d231.dll
You basically just have to add the last two definitions to the existing project:
Make sure you have an image calledimg.jpg
in yourmyFirstOpenCVProject-build-desktop
directory that is the default directory when you run your project from Qt.
and you should see the image displayed.
Wow! But this is just the beginning, you can do much more with OpenCV... Good luck!