Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

I started to learn OpenCV today and faced with 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:)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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'

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading