18

I am trying to run this simple OpenCV program, but i got this error:

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file .../opencv/modules/highgui/src/window.cpp, line 276

Code:

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;

int main()
{
    cout << "Hello World!" << endl;

    cv::Mat inputImage = cv::imread("/home/beniz1.jpg");
    cv::imshow("Display Image", inputImage);

    return 0;
}

What's the cause of this error?

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36

6 Answers6

25

This error means that you are trying to show an empty image. When you load the image with imshow, this is usually caused by:

  1. The path of your image is wrong (in Windows escape twice directory delimiters, e.g. imread("C:\path\to\image.png") should be: imread("C:\\path\\to\\image.png"), or imread("C:/path/to/image.png"));
  2. The image extension is wrong. (e.g. ".jpg" is different from ".jpeg");
  3. You don't have the rights to access the folder.

A simple workaround to exclude other problems is to put the image in your project dir, and simply pass to imread the filename (imread("image.png")).

Remember to add waitKey();, otherwise you won't see anything.

You can check if an image has been loaded correctly like:

#include <opencv2\opencv.hpp>
#include <iostream>
using namespace cv;

int main()
{
    Mat3b img = imread("path_to_image");

    if (!img.data)
    {
        std::cout << "Image not loaded";
        return -1;
    }

    imshow("img", img);
    waitKey();
    return 0;
}
Miki
  • 40,887
  • 13
  • 123
  • 202
  • It looks like the OP is using linux or possibly cygwin. – drescherjm Jul 10 '15 at 13:49
  • 1
    @drescherjm yes, it's just a general info on how to solve this problem (there are a lot of question on this particular topic). I added the most probable (and example only for Windows), feel free to add the ones on linux – Miki Jul 10 '15 at 13:54
1

Usually it means that your image is not there, it's a basic assertion for checking if the content is displayable in the window before actually displaying it, and by the way you need to create a window in order to show the image namedWindow( "name") then imshow ("name", image);

David Safrastyan
  • 725
  • 7
  • 18
0

I had the exact same problem, only in Raspbian. After hours of trying, the solution was pretty simple, I had to leave out the file extension.

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace std;
using namespace cv;
int main()
{
    Mat inputImage = imread("beniz1");
    imshow("Display Image", inputImage);
    waitKey(5000);

    return 0;
}
Fade
  • 9
  • 1
0

I also got the same error when I was using Qt Creator in Ubuntu. The image was in the project folder so I thought there is no need to give the complete path.

img = imread("baboon.png");

The error which I got was:

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

Error got resolved by giving the complete path:

img = imread("home/vivek/QT_ImageProcessing/IP_HomeWork1/baboon.png");

Nikolai Shevchenko
  • 7,083
  • 8
  • 33
  • 42
  • that happens when you don't know what a **Current Working Directory** is, or what it is for the current process. IDEs love to start programs from directories that are different from the directory containing the source code. – Christoph Rackwitz Dec 23 '22 at 14:44
-1

Most probably, You have not used the correct path to your image or it's format. If you're using windows: img =cv2.imread("C:/Users/mohin/Pictures/IMG_4514.jpg")

-2

Simply add your image to your project directory folder.

How:

1-Right click on your project name at Search Solution Explorer which is on left by default.

2-Click on Open folder in file explorer

3-Paste your image to that folder

then

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace std;
using namespace cv;
int main()
{
    //change "beniz1" to "beniz1.jpg"

    Mat inputImage = imread("beniz1.jpg"); 
    imshow("Display Image", inputImage);
    waitKey(5000);

    return 0;
}
Riemann
  • 37
  • 5