简体   繁体   中英

OpenCV Error: Assertion failed (size.width>0 && size.height>0) simple code

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?

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, eg 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. (eg ".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;
}

通常这意味着您的图像不存在,这是在实际显示之前检查内容是否可在窗口中显示的基本断言,顺便说一句,您需要创建一个窗口以显示该图像 namedWindow("name" ) 然后 imshow ("name", image);

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;
}

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");

仔细检查您的图像路径

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")

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM