Opencv Error: Assertion Failed (Size.Width>0 && Size.Height>0) Simple Code

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:

  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 <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, the img 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 use cap.isOpened() function, which returns True for successful initialization and False 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



Leave a reply



Submit