Assertion failed (size.width0 && size.height0)
I tried your code and for me it works (it visualizes the current webcam input)!
I ran it on Visual Studio 2012 Ultimate with OpenCV 2.4.7.
...
The error occurs because the image is empty, so try this:
while (true) {
cap >> image;
if(!image.empty()){
imshow("window", image);
}
// delay 33ms
waitKey(33);
}
Maybe the first image you receive from your webcam is empty. In this case imshow will not throw an error. So hopefully the next input images are not empty.
OpenCV Error: Assertion failed (size.width0 && size.height0) 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.width0 && size.height0 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.width0 && size.height0 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")
Related Topics
Designated Initializers in C++20
C++ Int with Preceding 0 Changes Entire Value
Why Using the Const Keyword Before and After Method or Function Name
At What Point Is It Worth Using a Database
Obtain a Std::Ostream Either from Std::Cout or Std::Ofstream(File)
Program Behaving Strangely on Online Ides
Will a "Variablename;" C++ Statement Be a No-Op at All Times
Pointer Interconvertibility VS Having the Same Address
Rewinding Std::Cout to Go Back to the Beginning of a Line
C++ N Nested Vectors at Runtime
Why Do We Require Requires Requires
In C++, What Does & Mean After a Function's Return Type
How to Link a Plain File into My Executable
Constexpr VS. Static Const: Which One to Prefer
How to Open a Gstreamer Pipeline from Opencv with Videowriter