1

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 videos\Photos and videos\Dogs photo.jpg'

cv.imread = img

cv.imshow("Dog", img)

cv.waitKey(0)

The Error:

Traceback (most recent call last):
    File "C:\Program Files\Python310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
    File "C:\Program Files\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
    File "C:\Users\Admin\PycharmProjects\pythonProject1\read.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'

1 Answer 1

1

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 videos\Photos and videos\Dogs photo.jpg'

img = cv.imread(path)

cv.imshow("Dog", img)

cv.waitKey(0)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.