[英]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 程序,但出现此错误:
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:当您使用
imshow
加载图像时,这通常是由以下原因引起的:
imread("C:\\path\\to\\image.png")
should be: imread("C:\\\\path\\\\to\\\\image.png")
, or imread("C:/path/to/image.png")
);imread("C:\\path\\to\\image.png")
应该是: imread("C:\\\\path\\\\to\\\\image.png")
或imread("C:/path/to/image.png")
); 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();
记得添加
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.我有完全相同的问题,只有在 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.当我在 Ubuntu 中使用 Qt Creator 时,我也遇到了同样的错误。 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")
如果您使用的是 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. 1-在默认位于左侧的搜索解决方案资源管理器中右键单击您的项目名称。
2-Click on Open folder in file explorer 2-单击文件资源管理器中的打开文件夹
3-Paste your image to that folder 3-将您的图像粘贴到该文件夹中
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.