cv2.error: Opencv(4.1.0)

Asked

Viewed 397 times

0

Code below:

import cv2

imagemCarregada = cv2.imread("exemplo.jpg", 0)

cv2.imshow("imagem", imagemCarregada)

cv2.waitKey(0)

cv2.destroyAllWindows()

Error presented:

inserir a descrição da imagem aqui

1 answer

2

This error means that you are loading an empty image. As specified in the error, the image must have a width > 0 and a height > 0.

Here, I was able to open an image with exactly the same code.

Possible causes of error:

  • The image does not exist in the directory. Vale checks again and try something like: cv2.imread("C:\\path\\to\\image.png") or cv2.imread("C:/path/to/image.png"));

  • The image extension may be wrong. Remember, .jpg is other than .jpeg

  • You may not have direct access to the image folder.
  • 2

    Complementing what Guilherme said about the image path, it is worth noting that when you use the relative path to refer to a file, python will start searching based on the directory defined in PYTHONPATH, an environment variable with the local directory assigned when calling the interpreter. So by your traceback, the image you upload should be on the Desktop. But by organization, create directories for your projects and put everything possible concerning them there. Calling the python command always from their root.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.