Skip to content Skip to sidebar Skip to footer

Assertion Failure : Size.width>0 && Size.height>0 In Function Imshow

i am using opencv2 and python on raspberry pi. and i am new with python and opencv. i tried to read a jpeg image and display image it shows the following error: /home/pi/opencv-2.

Solution 1:

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).

Solution 2:

it's the path which is causing the problem, i had the same problem but when i gave the full path of the image it was working perfectly.

Solution 3:

While using Raspbian in Rpi 3 I had the same problem when trying to read qrcodes. The error is because cv2 was not able to read the image. If using png image install pypng module.

sudo pip install pypng

Solution 4:

Use r in the code where you specified the file address. For Example:

import cv2
img = cv2.imread(r'D:\Study\Git\OpenCV\resources\lena.png')
cv2.imshow('output', img)
cv2.waitKey(0)

r stands for "raw" and will cause backslashes in the string to be interpreted as actual backslashes rather than special characters.

Solution 5:

In my case, I had forgotten to change the working directory of my terminal to that of my code+testImage. Hence, it failed to find the image there.

Finally, this is what worked for me:

I saved the image and Python file on Desktop. I changed my cmd directory to it,

cd Desktop

And then checked for my file:

ls

And this was my code that worked:

import cv2
import numpy as np

im = cv2.imread('unnamed.jpg')
#Display the image
cv2.imshow('im',im)
cv2.waitKey(2000) #Milliseconds

Post a Comment for "Assertion Failure : Size.width>0 && Size.height>0 In Function Imshow"