Python - tkinter; TypeError: Expected Ptr<cv::UMat> for argument 'src'

Python - tkinter; TypeError: Expected Ptr<cv::UMat> for argument 'src'

我正在尝试构建用于图像处理的图形界面。我的上传功能有问题。 当我尝试检查文件/图像是否存在并修改它时,出现此错误并且我不知道如何修复它。

TypeError: Expected Ptr<cv::UMat> for argument 'src'

这是我的代码:

import cv2

import instructions as instructions
from PIL import Image,ImageTk
from tkinter.filedialog import askopenfile

root = tk.Tk()

logo = Image.open('logo.png')
logo = ImageTk.PhotoImage(logo)
logo_label = tk.Label(image = logo)
logo_label.image = logo
logo_label.grid(column=1,row=0)

def upload():
    browse.set("loading...")
    file = askopenfile(parent=root,mode="rb",title="Choose an image",filetypes =[("JPG file","*.jpg"),("PNG file","*.png"),("JPEG file","*.jpeg")])
    if file:
        gray = cv2.cvtColor(file, cv2.COLOR_RGB2GRAY)
        gray = cv2.medianBlur(gray,5)
        edges = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY, 9, 9)

        color = cv2.bilateralFilter(file,9,250,250)
        cartoon =cv2.bitwise_and(color,color,mask=edges)
        cv2.imshow("Cartoon", cartoon)

intructions = tk.Label(root,text= "Select an image",font = "Raleway")
instructions.grid(columnspan=3,column=0,row=1)



browse = tk.StringVar()
browse_button = tk.Button(root,textvariable = browse,command = lambda:upload(),font = "Raleway",bg="#20bebe",fg ="white",width=15,height =2)
browse.set("Browse")
browse_button.grid(column=1,row=2)

canvas = tk.Canvas(root,width = 600,height = 300)
canvas.grid(columnspan = 3)
root.mainloop()

谢谢!

您需要将图像传递给 cv2.cvtColor,您当前传递的是字符串 - file

    if file:
        src = cv2.imread(file)
        gray = cv2.cvtColor(src, cv2.COLOR_RGB2GRAY)
        gray = cv2.medianBlur(gray,5)

第 20 行:您正在使用 CvtColor 将图像更改为灰度。
gray = cv2.cvtColor(file, cv2.COLOR_RGB2GRAY) 您需要传递文件指针来代替文件。 这就是您收到错误 TypeError: Expected Ptr<cv::UMat> for argument 'src'

的原因

您需要:
首先使用 img=cv2.imread(file) 读取文件,然后使用
使用 gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY).

在 img 数据上使用 CvtColor

所以删去第20行的内容并添加

img=cv2.imread(file)
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

编辑:
第 6 行的一件重要事情是导入 askopenfile,其中 returns 图像的二进制编码路径。这是错误的实现。
相反,你应该 import askopenfilename;这 returns 图像文件的路径。
我在这里分享整个更新的代码:

import cv2
import tkinter as tk

import instructions as instructions
from PIL import Image,ImageTk
from tkinter.filedialog import askopenfilename

root = tk.Tk()

logo = Image.open('logo.jpg')
logo = ImageTk.PhotoImage(logo)
logo_label = tk.Label(image = logo)
logo_label.image = logo
logo_label.grid(column=1,row=0)

def upload():
    browse.set("loading...")
    file = askopenfilename(parent=root,title="Choose an image",filetypes =[("JPG file","*.jpg"),("PNG file","*.png"),("JPEG file","*.jpeg")])
    print(file)
    if file:
        img=cv2.imread(file)
        gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
        gray = cv2.medianBlur(gray,5)
        edges = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY, 9, 9)

        color = cv2.bilateralFilter(img,9,250,250)
        cartoon =cv2.bitwise_and(color,color,mask=edges)
        cv2.imshow("Cartoon", cartoon)

intructions = tk.Label(root,text= "Select an image",font = "Raleway")
instructions.grid(columnspan=3,column=0,row=1)



browse = tk.StringVar()
browse_button = tk.Button(root,textvariable = browse,command = lambda:upload(),font = "Raleway",bg="#20bebe",fg ="white",width=15,height =2)
browse.set("Browse")
browse_button.grid(column=1,row=2)

canvas = tk.Canvas(root,width = 600,height = 300)
canvas.grid(columnspan = 3)
root.mainloop()
这应该可以让我知道。
希望这能解决您的问题