Welcome to the Author's Website

1. Playing with Images

1.1.2 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.

  1. The first step is obviously to download Qt. Simply run the installer with all default option. Once installed, you can run Qt Creator.

  2. 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 choose Qt Console Application.

    We will call our project myHelloWorld; place it in the location of your choice and click Next.

    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.

  3. Lets 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 called myHelloWorld-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.

  4. 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.

  5. 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 the make command in our Path environment variable. Indeed, compiling the library will be done using this make utility command that Qt installed together with the compilers themselves. They are located in C:\QtSDK\mingw\bin. Goto to your Control Panel (from the Start menu) and to the System menu.

    In the Advanced system settings menu, you select the Advanced tab.

    Click on Environment Variables...

    In the User variables box, look for the PATH variable. If it is there, click on Edit..., if not click on New... 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 the System variables box.

  6. 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.

  7. 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 on Configure. 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, here Release. If you wish, at the end, you can repeat the same process with the Debug mode.

    Since we want to use Qt, we also select the WITH_QT option

    Once your options selected, you click on Configure again.

    And you click on Generate to complete the installation.

  8. 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 type mingw32-make install

    This last step will install the library and the include files in the install directory. Note that for clarity, you can rename this directory as release since you ask CMake to build a Release install

    We are done with the installation! Congratulations!

  9. 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 in C:\OpenCV-2.3.1\install\bin.

    The Qt dlls are also required; you should find them at C:\QtSDK\QtCreator\bin

  10. 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 the projects menu and select the Release 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 called img.jpg in your myFirstOpenCVProject-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!

Top of the page

 

(c) Robert Laganiere 2011