Wednesday, 21 November 2012

Getting Started with QT and OpenCV 2.4.2

I recently started developing with OpenCV 2.4.2 and Qt on a Linux machine (Ubuntu 12.0.4), so I decided to write a few tutorials. Here, we are going to see how to set-up a development environment particularly Qt-creator to work with OpenCV.
First, you need to install OpenCV as this has been dealt with severally I don't bother with this. If you have not yet installed this have a look at this tutorial or just search Google for a tutorial that suits you.

Next step you would need to install Qt-creator. This can be done either via the Ubuntu Software Centre or by directly downloading the .bin installer from here.

Now we can begin, launch QT creator and then Go to File > New File or Project. Select a QT Console Application (Use a QT GUI Application if you OpenCV installation is configured to use QT and  not GTK ) give the project a name e.g. “FirstQtProject”




You could leave the other settings in the wizard as they are or change them to suit your own projects. With the default settings, two files would be created: 'FirstQtProject.pro' ( this will vary depending on the Project Name you have chosen) and 'main.cpp'. To run OpenCV programs in QT creator we need to let the IDE know where to find the OpenCV libraries.

Open the .pro file associated with the project and append the following lines at the end of the file. This should be included in every OpenCV Project you create but remember to change the include Path "/usr/local/include/opencv2" to the location of your OpenCV include directory on your PC.

#Change this to your include directory.
INCLUDEPATH += "/usr/local/include/opencv2" 
LIBS += `pkg-config --cflags --libs opencv`
If you don't have Pkg-config installed replace the last line with the following and replace “/usr/local/lib” with the location of the openCV libraries on your PC.
# Confirm the location of you opencv libraries and change appropriately.
LIBS += usr/local/lib \
-lopencv_core \
-lopencv_highgui \
-lopencv_imgproc \
-lopencv_flann \
-lopencv_legacy \
-lopencv_ml \
-lopencv_features2d \
-lopencv_calib3d
Now go to 'main.cpp' file and type in the following lines of code and press Ctrl+R to run it.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
int main(void)
{
    // Load an image from the disk and store in variable.
    cv::Mat image1 = cv::imread("/home/stephen/Pictures/download.jpg");
    // Create a image display window called Figure1. 
    cv::namedWindow("Figure1");
    // Display image in Figure1.  
    cv::imshow("Figure1", image1);
    // Wait until user closes the window or presses Esc Key. 
    cv::waitKey(0);
    
    return 0;
}
The result should be an empty console window called "qtcreator_process_sub” and a window called “Figure1” containing the loaded image.

Please let me know if this was helpful and if there is tutorial you would like me to write mention it in the comments. Happy Coding!

8 comments:

  1. Thanks...It works.. :)
    Although, you could add:
    If Qt-creator has been installed using apt-get (or the software center), go to Qt Creator settings and change the terminal to usr/bin/xterm -e

    ReplyDelete
  2. Hi!I've installed QT and OpenCV but when I build your example appear a Segmentation Fault and I dont understand it. Would you know what happen?

    ReplyDelete
    Replies
    1. Hi, Check that the image you are loading actually exists.

      Delete
  3. Thank you. This is the first project I can run on my computer! It is quite encouraging!

    ReplyDelete
  4. Sorry Sir,
    I am getting this error.

    sudo ./FirstQtProject /home/jithin/images_logo_lg.gif
    ./FirstQtProject: error while loading shared libraries: libopencv_core.so.2.4: cannot open shared object file: No such file or directory
    //////////////////////////////////////////////////////////////////////////////////////
    My *.pro file
    QT += core

    QT -= gui

    TARGET = FirstQtProject
    CONFIG += console
    CONFIG -= app_bundle

    TEMPLATE = app


    SOURCES += main.cpp

    INCLUDEPATH += "/usr/local/include/opencv2"
    LIBS += `pkg-config --cflags --libs opencv`
    //////////////////////////////////////////////////////////////////////////////////////////////
    Files in
    cd /usr/local/include/opencv2
    [jithin@localhost opencv2]$ ls
    calib3d features2d highgui ml opencv.hpp stitching video
    contrib flann imgproc nonfree opencv_modules.hpp superres videostab
    core gpu legacy objdetect photo ts
    ////////////////////////////////////////////////////////////////////////////////////////////////////
    Files in
    cd /usr/local/lib
    [jithin@localhost lib]$ ls
    libopencv_calib3d.so libopencv_highgui.so.2.4.6 libopencv_stitching.so.2.4
    libopencv_calib3d.so.2.4 libopencv_imgproc.so libopencv_stitching.so.2.4.6
    libopencv_calib3d.so.2.4.6 libopencv_imgproc.so.2.4 libopencv_superres.so
    libopencv_contrib.so libopencv_imgproc.so.2.4.6 libopencv_superres.so.2.4
    libopencv_contrib.so.2.4 libopencv_legacy.so libopencv_superres.so.2.4.6
    libopencv_contrib.so.2.4.6 libopencv_legacy.so.2.4 libopencv_ts.so
    libopencv_core.so libopencv_legacy.so.2.4.6 libopencv_ts.so.2.4
    libopencv_core.so.2.4 libopencv_ml.so libopencv_ts.so.2.4.6
    libopencv_core.so.2.4.6 libopencv_ml.so.2.4 libopencv_video.so
    libopencv_features2d.so libopencv_ml.so.2.4.6 libopencv_video.so.2.4
    libopencv_features2d.so.2.4 libopencv_nonfree.so libopencv_video.so.2.4.6
    libopencv_features2d.so.2.4.6 libopencv_nonfree.so.2.4 libopencv_videostab.so
    libopencv_flann.so libopencv_nonfree.so.2.4.6 libopencv_videostab.so.2.4
    libopencv_flann.so.2.4 libopencv_objdetect.so libopencv_videostab.so.2.4.6
    libopencv_flann.so.2.4.6 libopencv_objdetect.so.2.4 libosmscoutmapqt.a
    libopencv_gpu.so libopencv_objdetect.so.2.4.6 libosmscoutmapqt.la
    libopencv_gpu.so.2.4 libopencv_photo.so libosmscoutmapqt.so
    libopencv_gpu.so.2.4.6 libopencv_photo.so.2.4 libosmscoutmapqt.so.0
    libopencv_highgui.so libopencv_photo.so.2.4.6 libosmscoutmapqt.so.0.0.0
    libopencv_highgui.so.2.4 libopencv_stitching.so pkgconfig
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    ReplyDelete
  5. Thanks Sir ,
    My Problem Solved by

    http://stackoverflow.com/questions/12335848/opencv-program-compile-error-libopencv-core-so-2-4-cannot-open-shared-object-f


    Now i m in to OpenCv..
    Need Some more help on the go..
    Thank You Sir... Thank You very much

    ReplyDelete
  6. this code is running but the image viewer window is not opening so .... kindly help me why is this happening ......

    ReplyDelete
  7. Hey. I urgently need your help.
    I am given a project which I need to complete in 30 days. 7 days have passed and I am unable to find anything. Can you please help me?
    Tis is my task given by my professor:
    Your job is to make a video display console in QT using QT creator. The Soft should take video from a BNC cable (via any suitable converter) on ubuntu, qt platform.

    ReplyDelete