5

I'm trying to set up OpenCV in Qt Creator and I have some problems. I added the OpenCV path in the Qt Creator .pro file

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

And I want to read and show image in this code

void MainWindow::on_pushButton_clicked()
{
     cv::Mat matInput = cv::imread("LP.jpg");
     if( matInput.empty())
     {
          std::cout<<"Can't load image "<<std::endl;
     }
     cv::namedWindow("Show");
     cv::imshow("Show", matInput);
     cv::waitKey();
}

When I run my project it shows the following message:

Starting /home/vasan/Qt/build-OpenCVWithQt-Desktop-Debug/OpenCVWithQt...
The program has unexpectedly finished.
/home/vasan/Qt/build-OpenCVWithQt-Desktop-Debug/OpenCVWithQt exited with code 0

3
  • Where is LP.jpg? In the Resources? Commented Jul 30, 2013 at 5:08
  • So if you run in debug mode, it crashes on the line cv::Mat matInput = cv::imread("LP.jpg");, right ? Commented Jul 30, 2013 at 5:36
  • yes, LP.jpg in my project folder. When I delete opencv code i.e Mat,imread, imshow I can build and run it can show GUI windows. Commented Jul 30, 2013 at 8:11

3 Answers 3

3

I have also been trying to set up OpenCV for Qt Creator for a few days now. I would recommend you trying the procedure found at

https://code.google.com/p/qt-opencv-multithreaded/wiki/Documentation

It is simple to follow, and it just WORKS. When you get to section 1.3, run any code sample you got at hand. I used:

int main( int argc, char** argv )
{
    if( argc != 2)
    {
     cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
     return -1;
    }

    Mat image;
    image = imread(argv[1], CV_LOAD_IMAGE_COLOR);   // Read the file
    image = imread("lena.png", CV_LOAD_IMAGE_COLOR);   // Read the file
    if(! image.data )                              // 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.

    waitKey(0);                                          // Wait for a keystroke in the window
    return 0;
}

Hope that helps!

Sign up to request clarification or add additional context in comments.

Comments

2

It worked for me:

INCLUDEPATH += /usr/local/include/opencv2
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

Comments

0

Your code is correct. I think the problem arise because you are using shadow building. For this reason you should put the image in the shadow building folder (the folder that contains the executable) and not in the project folder (that contains only the code files).

Another tip is to insert a return statement in the if case, so program will not quit when it does not find the image.

 if( matInput.empty())
 {
      qDebug() << "Can't load image";
      return;
 }

Also, be sure to include the following headers

#include <QDebug>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.