-1

I am starting out with OpenCV in Python, and I have the following simple code from a Youtube tutorial:

import numpy as np
import cv2

# read the image
img = cv2.imread('lion.jpg')

# show the image
# cv2.imshow has 2 parameters: the first is the name of the window in which we are showing the image
# and the second is the matrix referring to the image we want to show
cv2.imshow('image', img)

# We want the program to exit when the user presses a key
cv2.waitKey(0)
# Once the user presses the key, we want the program to shut down
cv2.destroyAllWindows()

However, I get the following error:

cv2.error: C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:331: error: (-215) size.width>0 && size.height>0 in function cv::imshow

The error occurred on the line where I call cv2.imshow().

Is this because there is no such image lion.jpg already included in the cv2 module? Because I tried using a random picture from my images folder instead and it threw the same error.

John Smith
  • 393
  • 1
  • 6
  • 17
  • 1
    You're trying to show an empty image, since you didn't check whether `imread` succeeded. This sort of question is asked here on pretty much daily basis. – Dan Mašek Feb 12 '18 at 21:45

1 Answers1

1

This error can occur with opencv for different reason.

  • The image is of zero size (which is not your case, but it might happen if you do something silly like img = img[50:40, :])
  • The image does not exist. In this case, make sure the path to your image is complete : lion.jpg should be in the folder from which you execute the python code. Otherwise you should write down the path to the image.

And to my knowledge, there is no such lion.jpg image included in opencv, but I may have overlooked it.

Joseph Budin
  • 1,299
  • 1
  • 11
  • 28