How To Use OpenCV In Qt


If you ever had been interested in image processing anytime back, chances are high you did head of OpenCV. It’s an open source and highly optimized C++ based computer vision library developed Intel’s Russian research labs. It’s easy to learn and extremely powerful. Now chances are high you also must have heard of Qt framework if you ever wanted to develop a GUI application in C++. It’s an open source, cross-platform C++ framework that’s used globally to develop rich cross-platform applications. It also supports GUI features much easier to learn and use as compared to the Windows only MFC.

For an image processing application in C++ the combination of OpenCV and Qt can prove to be very effective. You can easily use OpenCV from Qt itself (the Qt Creator IDE to be precise). Let us see how can we do that.

This is assuming you already have OpenCV and Qt downloaded. If not, head over to this and this link to get them

Step 1. Install OpenCV by extracting (Linux users) or running the executable (Windows users). Then follow the instructions from OpenCV documentation here to perform the initial setup.

Step 2. Open Qt Creator and create a Qt application. Open the .pro file corresponding to the project. It will be listed on the projects panel as the top most file in project. It will look something like this.


Step 3. Add the following lines at the end of the .pro file –

INCLUDEPATH += C:\\OpenCV\\opencv\\build\\include
LIBS += C:\\OpenCV\\opencv\\build\\x86\\vc12\\lib\\opencv_core249d.lib \
        C:\\OpenCV\\opencv\\build\\x86\\vc12\\lib\\opencv_imgproc249d.lib \
        C:\\OpenCV\\opencv\\build\\x86\\vc12\\lib\\opencv_highgui249d.lib \
        C:\\OpenCV\\opencv\\build\\x86\\vc12\\lib\\opencv_ml249d.lib \
        C:\\OpenCV\\opencv\\build\\x86\\vc12\\lib\\opencv_video249d.lib \
        C:\\OpenCV\\opencv\\build\\x86\\vc12\\lib\\opencv_features2d249d.lib \
        C:\\OpenCV\\opencv\\build\\x86\\vc12\\lib\\opencv_calib3d249d.lib \
        C:\\OpenCV\\opencv\\build\\x86\\vc12\\lib\\opencv_objdetect249d.lib \
        C:\\OpenCV\\opencv\\build\\x86\\vc12\\lib\\opencv_contrib249d.lib \
        C:\\OpenCV\\opencv\\build\\x86\\vc12\\lib\\opencv_legacy249d.lib \
        C:\\OpenCV\\opencv\\build\\x86\\vc12\\lib\\opencv_flann249d.lib \
        C:\\OpenCV\\opencv\\build\\x86\\vc12\\lib\\opencv_features2d249.lib \

Notice the double slash instead of a single slash. I am using Visual Studio 2013 so I mentioned vc12 folder. You can use vc11 or vc10 for Visual Studio version 2011 and 2010 respectively. Also notice the ‘d’ at the end of each file name. It indicates that the libraries are for debug mode. If you want to deploy your application use the files without the trailing ‘d’.

Select Build menu > Clean Project. Then select Build > Run qmake.

That’s it. This is all you need to do to make Qt identify OpenCV. Test it by running this short program.

#include <opencv2/opencv.hpp>

using namespace cv;

int main()
    String imageSrc;
    Mat image;

    std::cout << "Enter path of an image file: ";
    std::cin >> imageSrc;

    //opening image file
    image = imread(imageSrc, IMREAD_COLOR);

        std::cout << "\nUnable to open image.";
        return -1;

    //displaying image
    namedWindow("Test Image");
    imshow("Test Image", image);

    //for preventing instant closing of image window

    return 0;

If the image opens, you have successfully configured OpenCV for Qt. You will need to add those lines to .pro file for every new project you make.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s