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

匿名 (未验证) 提交于 2019-12-03 03:03:02

问题:

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  #include   using namespace std;  int main() {     cout 

What's the cause of this error?

回答1:

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  #include  using namespace cv;  int main() {     Mat3b img = imread("path_to_image");      if (!img.data)     {         std::cout 


回答2:

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  #include   using namespace std; using namespace cv; int main() {     Mat inputImage = imread("beniz1");     imshow("Display Image", inputImage);     waitKey(5000);      return 0; } 


回答3:

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!