Integrating OpenCV 2.4.6 with Qt 5.1

  04 Oct 2013

In the previous post I’ve describe how to build opencv on a linux system. Let’s see how we can integrate Opencv with Qt. I’ll use a simple console application which will read and display an RGB image, to show the steps.

  1. Create a new Qt console application.
  2. Add the following lines to the .pro file
     INCLUDEPATH += /us/local/include/opencv
     LIBS += -L/usr/local/lib\

    Here INCLUDEPATH points the compiler to fetch opencv header files. LIBS points to the opencv libraries which our program make use of. We have to add all the required libraries depending on the functions that we use on our program. The built libs can be found under /release/lib, which means we can put that as the libs reference location on the other hand we can use /usr/local/include/opencv ; you might remember we added an install prefix option before executing make install command. It pre-pends CMAKE_INSTALL_PREFIX on to all install directories. Default value for this on linux is usr/local . Now all the things are ready to use opencv in our application. Let’s write a simple application to test it.

#include <QCoreApplication>
#include <cv.h>
#include <highgui.h>

using namespace std;
using namespace cv;

int main(int argc, char *argv[])
    QCoreApplication a(argc, argv);
    Mat image;
    image = imread("<path to image file>");   // Read the file

       if(image.empty())                              // Check for invalid input
           cout <<  "Could not open or find the image" << std::endl ;
           return -1;

       namedWindow( "Display window", CV_WINDOW_AUTOSIZE );//Create a window for display.

       imshow( "Display window", image );   // Show our image inside it.
    return a.exec();
comments powered by Disqus