randy sandy randy sandy - 1 year ago 275
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-, line 269
terminate called after throwing an instance of 'cv::Exception'
what(): /build/opencv-ISmtkH/opencv- 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!


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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download