Tuesday, December 11, 2012

Configure OpenCV 2.4 for Qt Creator on Ubuntu

First install Qt creator using Ubuntu Software Center

Open Qt Creator

File --> New file or project
Qt Widget Project --> Qt GUI Application --> Choose

Click Next for all.

Open .pro file and add the following lines

INCLUDEPATH += /usr/local/include/opencv
LIBS += -L/usr/local/lib
LIBS += -lopencv_core
LIBS += -lopencv_imgproc
LIBS += -lopencv_highgui
LIBS += -lopencv_ml
LIBS += -lopencv_video
LIBS += -lopencv_features2d
LIBS += -lopencv_calib3d
LIBS += -lopencv_objdetect
LIBS += -lopencv_contrib
LIBS += -lopencv_legacy
LIBS += -lopencv_flann
LIBS += -lopencv_nonfree


Save it.

Open .ui file.

Drag and drop a PushButton to window.
Click PushButton and chage the follwong properties
  • objectName: btnDisplay
  • text: Display

Right click PushButton --> Go to slot --> clicked() --> Ok

Include the following

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


Add the following code

void MainWindow::on_btnDisplay_clicked()
{
    cv::namedWindow("Disp");
    cv::VideoCapture cap(0);
    cv::Mat frame;
    do{
        cap >> frame;
        imshow("Disp",frame);
    }while(cv::waitKey(30)<0);
    cv::destroyAllWindows();
}


Run it. You will see yourself on the screen, when you click Display button !!!


15 comments:

  1. Man thanks a lot, you saved my life!!

    ReplyDelete
  2. i keep having an error cv.h no such directory can any one help?!please

    ReplyDelete
    Replies
    1. Is you opencv installed directory is same as mine? Have you added opencv to PATH as in my other article?

      Delete
    2. same problem here, how to fix it please ?

      Delete
    3. Try,
      sudo echo “/usr/local/lib” >> /etc/ld.so.conf
      sudo ldconfig

      Delete
  3. help me !
    when i click button, i get an error
    (test1:2811): Gtk-WARNING **: gtk_disable_setlocale() must be called before gtk_init()
    OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /home/phu/opencv-2.4.6.1/modules/highgui/src/window.cpp, line 261
    terminate called after throwing an instance of 'cv::Exception'
    what(): /home/phu/opencv-2.4.6.1/modules/highgui/src/window.cpp:261: error: (-215) size.width>0 && size.height>0 in function imshow

    ReplyDelete
  4. plz help me,when i run this program i got a error is
    "The program has unexpectedly finished.
    /home/ayisha/Qt_APP/showimage/build-disp-Desktop_Qt_5_0_2_GCC_32bit-Debug/disp exited with code 0".plz hel me to solve this.

    ReplyDelete
  5. i m also getting the same error as ayisha

    ReplyDelete
    Replies
    1. does opencv works well without Qt? Do you use the same include path and libs in Qt?

      Does Qt widget app works well without opencv?

      *This example requires web cam installed in the machine. Otherwise try loading a static image and display

      Delete
  6. For me worked with:

    QT_CONFIG -= no-pkg-config
    CONFIG += link_pkgconfig
    PKGCONFIG += opencv

    ReplyDelete
  7. i got these errors
    :-1: error: mainwindow.o: undefined reference to symbol '_ZN2cv6imreadERKNS_6StringEi'

    /usr/local/lib/libopencv_imgcodecs.so.3.0:-1: error: error adding symbols: DSO missing from command line

    any solution???

    ReplyDelete
    Replies
    1. I have the same problem...! with opencv_contrib face recog module in opencv 3.1.0

      Delete
  8. Hi, i am getting the error shown below

    gtk_disable_setlocale() must be called before gtk_init()

    have not been able to find my way around it

    ReplyDelete
  9. you have explained for c programing itseems.

    How to resolve problem in python (ubuntu)?

    (.:4240): Gtk-WARNING **: gtk_disable_setlocale() must be called before gtk_init()
    [xcb] Unknown request in queue while dequeuing
    [xcb] Most likely this is a multi-threaded client and XInitThreads has not been called
    [xcb] Aborting, sorry about that.
    python: ../../src/xcb_io.c:179: dequeue_pending_request: Assertion `!xcb_xlib_unknown_req_in_deq' failed.
    Aborted (core dumped)

    ReplyDelete