OpenCV Error: Assertion failed (size.width 0 && size.height 0) simple code
This error means that you are trying to show an empty image. When you load the image with imshow
, this is usually caused by:
- 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.
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;
}
Assertion failure : size.width 0 && size.height 0 in function imshow
The image fails to load (probably because you forgot the leading /
in the path). imread
then returns None. Passing None
to imshow
causes it to try to create a window of size 0x0, which fails.
The poor error handling in cv
probably owes to its quite thin wrapper layer on the C++ implementation (where returning NULL on error is a common practice).
OpenCV(4.2.0) error: (-215:Assertion failed) size.width 0 && size.height 0 in function 'cv::imshow'
Basically, this error tells you that you are trying to show an empty / non existent image. Please do check:
- The path: I think the problem comes from
cv2.imread()
. If the path is incorrect, theimg
variable will be empty.
The way you tried to read the image is almost right:
img = cv2.imread(C:\Users\someone\Documents\python\____The Useless Installer____\PY\colorpic)
The way it should be:
- double backslash for escaping the
"\"
character which has a special meaning in programming languages - you do need to enter the format of the picture (
jpeg
,png
, etc..). - you need to pass this argument as a
'string'
or"string"
Therefore try img = cv2.imread("C:\\Users\\someone\\Documents\\python\\____The Useless Installer____\\PY\\colorpic.jpg")
OpenCV Error: Assertion failed (size.width 0 && size.height 0) in imshow
Provide an id to VideoCapture
.
cap = cv2.VideoCapture(0)
Also check the value of ret
, see if it's TRUE
or FALSE
print (ret)
Edit:
To capture a video, you need to create a VideoCapture object. Its argument can be either the device index or the name of a video file. Device index is just the number to specify which camera.
cap = cv2.VideoCapture(0)
To check whether the
cap
has been initialized or not, you can usecap.isOpened()
function, which returnsTrue
for successful initialization andFalse
for failure.
if cap.isOpened() == False:
print ("VideoCapture failed")
cap.read()
returns a bool (True/False). If frame is read correctly, it will be True. So you can check end of the video by checking this return value.
ret, frame = cap.read()
if ret == False:
print("Frame is empty")
Further reading here.
Related Topics
Scope of Variables in If Statements
Why Did Microsoft Abandon Long Double Data Type
Differencebetween a Const Reference and Normal Parameter
Is There Any Case Where a Return of a Rvalue Reference (&&) Is Useful
C++ Convert Vector<Int> to Vector<Double>
How to Set a Timeout on Blocking Sockets in Boost Asio
Why Does C++ Allow an Integer to Be Assigned to a String
Visual Studio 2010's Strange "Warning Lnk4042"
Overriding Public Virtual Functions with Private Functions in C++
About Binding a Const Reference to a Sub-Object of a Temporary
Convert a String to a Date in C++
How to Initialize a Vector in C++
Error with Multiple Definitions of Function
Stdafx + Header File - Order of Inclusion in Mfc Application