C++ OpenCV 错误:断言失败 (size.width>0 && size.height>0) 简单代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31341845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenCV Error: Assertion failed (size.width>0 && size.height>0) simple code
提问by
I am trying to run this simple OpenCV program, but i got this error:
我正在尝试运行这个简单的 OpenCV 程序,但出现此错误:
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?
这个错误的原因是什么?
采纳答案by Miki
This error means that you are trying to show an empty image. When you load the image with imshow
, this is usually caused by:
此错误意味着您正在尝试显示空图像。当您使用 加载图像时imshow
,这通常是由以下原因引起的:
- 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")
, orimread("C:/path/to/image.png")
); - The image extension is wrong. (e.g. ".jpg" is different from ".jpeg");
- You don't have the rights to access the folder.
- 您的图像路径错误(在 Windows 中转义两次目录分隔符,例如
imread("C:\path\to\image.png")
应该是:imread("C:\\path\\to\\image.png")
, 或imread("C:/path/to/image.png")
); - 图片扩展名错误。(例如“.jpg”不同于“.jpeg”);
- 您无权访问该文件夹。
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")
).
排除其他问题的一个简单解决方法是将图像放在项目目录中,然后简单地传递给imread
文件名 ( imread("image.png")
)。
Remember to add waitKey();
, otherwise you won't see anything.
记得添加waitKey();
,否则你什么也看不到。
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;
}
回答by David Safrastyan
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);
通常这意味着您的图像不存在,这是在实际显示之前检查内容是否可在窗口中显示的基本断言,顺便说一句,您需要创建一个窗口以显示该图像 namedWindow("name" ) 然后 imshow ("name", image);
回答by Fade
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.
我有完全相同的问题,只有在 Raspbian 中。经过数小时的尝试,解决方案非常简单,我不得不省略文件扩展名。
#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;
}
回答by VIVEK KUMAR
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.
当我在 Ubuntu 中使用 Qt Creator 时,我也遇到了同样的错误。图像在项目文件夹中,所以我认为没有必要提供完整路径。
img = imread("baboon.png");
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");
img = imread("home/vivek/QT_ImageProcessing/IP_HomeWork1/baboon.png");
回答by Sanzhar Askaruly
double check your path to an image
仔细检查您的图像路径
回答by Ahmad24
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")
最有可能的是,您没有使用正确的图像路径或其格式。如果您使用的是 Windows: img =cv2.imread("C:/Users/mohin/Pictures/IMG_4514.jpg")