randy sandy randy sandy - 21 days ago 9
C++ Question

How to use OpenCV with highGUI on QtCreator?

I am trying to display a basic image that is loaded off disk using the highgui module within the cv2 library.
I am able to do this when making a Qt Widgets application but the Qt window becomes redundant; however when using a console application I obtain this error:

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /build/opencv-ISmtkH/opencv-2.4.9.1+dfsg/modules/highgui/src/window.cpp, line 269
terminate called after throwing an instance of 'cv::Exception'
what(): /build/opencv-ISmtkH/opencv-2.4.9.1+dfsg/modules/highgui/src/window.cpp:269: error: (-215) size.width>0 && size.height>0 in function imshow


Here is my code:

int main ()
{
cv::Mat inputImg = cv::imread("/home/pi/scrot1.png");
cv::imshow("Display Image", inputImg);
}


I have written a script in python that can be run via terminal and it calls the high gui module. I would like to write this in C++ but I cannot seem to be able to load a basic image!

EDIT:

The path was actually invalid. However, even with the correct path I cannot see the image with HighGui.

Answer

The error means that the image is not loaded correctly. You can check this using:

if(image.empty()) {
   // not loaded correctly
}

You need to be sure that:

  1. the path is valid
  2. the image is not corrupted
  3. you have the privileges to access that folder

To actually display the image, you need to use cv::waitKey() after the imshow.