Home > database >  Why cv2 is throwing bad argument error? How can I run it successfully?
Why cv2 is throwing bad argument error? How can I run it successfully?

Time:08-28

I have declared all variables as global I m using tkinter interface for dipalying image

global img, img1,img2,img3

Here are the two function used for displaying

def hist():
    global img2,img3
    transform = transforms.Grayscale()
    img2 = transform(img2)
    img2 = ImageOps.equalize(img2,mask=None)
    img2 = img2.resize((180, 180))

    img2 = ImageTk.PhotoImage(img2)
    canvas9.create_image(96, 96, image=img2)
    canvas9.place(x=730,y=60)

After running this function I m getting error In this I m copying img2 in img3 and I am facing problem

def rgb():
    global img3,img2
    img3 = img2.copy()
    img3=cv2.cvtColor(img3,cv2.COLOR_BGR2RGB)
    img3 = img3.resize((180, 180))
    img3 = ImageTk.PhotoImage(img3)
    canvas10.create_image(96, 96, image=img3)
    canvas10.place(x=370,y=320)

Error

This is the error I m getting

File "C:\python310\lib\tkinter\__init__.py", line 1921, in __call__
    return self.func(*args)
  File "C:\Users\ABHISHEK\PycharmProjects\cervical_project\gui.py", line 23, in rgb
    img3=cv2.cvtColor(img3,cv2.COLOR_BGR2RGB)
cv2.error: OpenCV(4.5.5) :-1: error: (-5:Bad argument) in function 'cvtColor'
> Overload resolution failed:
>  - src is not a numpy array, neither a scalar
>  - Expected Ptr<cv::UMat> for argument 'src'

CodePudding user response:

You're missing parameter. In line 23:

img3 = img3.resize(img3, (180, 180))

Also this too:

img2 = img2.resize(img2,(180, 180))

CodePudding user response:

You're mixing OpenCV/numpy and PIL indiscriminately.

img3 = img2.copy()
img3 = cv2.cvtColor(img3, cv2.COLOR_BGR2RGB)

So you're actually using img2. What is it?

img2 = ImageTk.PhotoImage(img2)

Tk PhotoImages are not compatible with OpenCV.

Check the type yourself:

print(type(img3)) # right before calling cvtColor
  • Related