Python OpenCV Error: cv2.error :-1: error: in function 'imshow'

Question:

I started to learn OpenCV today and faced a small problem:

cv2.error: OpenCV(4.6.0) :-1: error: (-5:Bad argument) in function 'imshow'

You can use this issue just to warm up before going to solve more complicated questions:)

Thank you!

The code:

import cv2 as cv 

img = R'Photos and videosPhotos and videosDogs photo.jpg'

cv.imread = img

cv.imshow("Dog", img)

cv.waitKey(0)

The Error:

Traceback (most recent call last):
    File "C:Program FilesPython310librunpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
    File "C:Program FilesPython310librunpy.py", line 86, in _run_code
    exec(code, run_globals)
    File "C:UsersAdminPycharmProjectspythonProject1read.py", line 7, in <module>
    cv.imshow("Dog", img)
cv2.error: OpenCV(4.6.0) :-1: error: (-5:Bad argument) in function 'imshow'
> Overload resolution failed:
>  - mat is not a numpy array, neither a scalar
>  - Expected Ptr<cv::cuda::GpuMat> for argument 'mat'
>  - Expected Ptr<cv::UMat> for argument 'mat'
Asked By: David

||

Answers:

cv.imread() is a function which takes in a "path" as an argument, so you need to pass the path to your image to it and it will then return the CV2 Image object, which you can pass that imshow() to display.

import cv2 as cv 

path = R'Photos and videosPhotos and videosDogs photo.jpg'

img = cv.imread(path)

cv.imshow("Dog", img)

cv.waitKey(0)
Answered By: Vorkos
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.