Thomas
Thomas

Reputation: 1212

OpenCV(4.2.0) error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'

I've been getting this error. I have copied the code from This website and making changes so that the code actually works. The code:

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", help="Image Path")
args = vars(ap.parse_args())
img_path = args['image']

#Reading image with opencv
img = cv2.imread(img_path)

#Reading csv file with pandas and giving names to each column
index=["color","color_name","hex","R","G","B"]

csv = pd.read_csv('colors.csv', names=index, header=None)
cv2.namedWindow('image')
cv2.setMouseCallback('image', draw_function)

while(1): 
    #This is error line
    cv2.imshow("image",img)
    [...]

The full Traceback:

Traceback (most recent call last):
  File "C:\Users\someone\Documents\python\____The Useless Installer____\PY\color_detection.py", line 39, in <module>
    cv2.imshow("image",img)
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'

I've looked here and here but I don't understand how to fix this and I don't think it relates to my question that much either. If this is obvious I am very sorry. I'm a complete newbie at this. Thanks

Upvotes: 0

Views: 6896

Answers (1)

Cătălina S&#238;rbu
Cătălina S&#238;rbu

Reputation: 1283

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


Upvotes: 3

Related Questions