简体   繁体   English

Python - tkinter; 类型错误:预期的 Ptr<cv::umat> 对于参数“src”</cv::umat>

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

I'm trying to build a graphical interface for image processing.我正在尝试为图像处理构建图形界面。 I have problem uploading function.我在上传 function 时遇到问题。 When I try to check if the file / image exists and modify it, I get this error and I don't know how to fix it.当我尝试检查文件/图像是否存在并对其进行修改时,我收到此错误并且我不知道如何修复它。

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

This is my code:这是我的代码:

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()

Thank you!谢谢!

You need to pass an image to cv2.cvtColor , you are currently passing a string - file .您需要将图像传递给cv2.cvtColor ,您当前正在传递一个字符串file

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

In line no 20: you are using CvtColor to change the image to grayscale.在第 20 行:您正在使用 CvtColor 将图像更改为灰度。
gray = cv2.cvtColor(file, cv2.COLOR_RGB2GRAY) you need to pass the file pointer in place of file. gray = cv2.cvtColor(file, cv2.COLOR_RGB2GRAY)您需要传递文件指针来代替文件。 That is why you are getting the error TypeError: Expected Ptr<cv::UMat> for argument 'src'这就是为什么您收到错误TypeError: Expected Ptr<cv::UMat> for argument 'src'

You need to:你需要:
First read the file using img=cv2.imread(file) , and then use首先使用img=cv2.imread(file)读取文件,然后使用
CvtColor on the img data using gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) .使用gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)对 img 数据进行 CvtColor。

So delete the content in line 20 and add所以删除第20行的内容并添加

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

Edit:编辑:
One important thing on line 6 you are importing askopenfile which returns a binary encoded path of the image.第 6 行的一件重要事情是导入 askopenfile,它返回图像的二进制编码路径。 This is wrong implementation.这是错误的实现。
Instead you should import askopenfilename;相反,您应该导入 askopenfilename; this returns the path of the image file.这将返回图像文件的路径。
I am sharing the entire updated code here:我在这里分享整个更新的代码:

 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()
This should work let me know. 这应该工作让我知道。
Hope this solves your purpose 希望这可以解决您的目的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:预期的 Ptr<cv::umat> 对于参数'src'?</cv::umat> - TypeError: Expected Ptr<cv::UMat> for argument 'src'? 预期 Ptr<cv::UMat> 对于参数&#39;src&#39; - Expected Ptr<cv::UMat> for argument 'src' 类型错误:预期的 Ptr<cv::umat> 对于参数 'm'</cv::umat> - TypeError: Expected Ptr<cv::UMat> for argument 'm' 类型错误:预期的 Ptr<cv::umat> 对于参数“%s”</cv::umat> - TypeError: Expected Ptr<cv::UMat> for argument '%s' 类型错误:预期的 Ptr<cv::umat> 对于参数“img”</cv::umat> - TypeError: Expected Ptr<cv::UMat> for argument 'img' 收到错误 TypeError: Expected Ptr<cv::umat> 处理图像时的参数“src”</cv::umat> - Getting the error TypeError: Expected Ptr<cv::UMat> for argument 'src' while processing images 错误:我无法将我的图像从 bgr 转换为 rgb:TypeError: Expected Ptr<cv::umat> 对于参数“src”</cv::umat> - error: im not able to convert my images from bgr to rgb: TypeError: Expected Ptr<cv::UMat> for argument 'src' 参数“src”的预期 cv::UMat - Expected cv::UMat for argument 'src' Flask:类型错误:预期的 Ptr<cv::umat> 对于参数“%s”</cv::umat> - Flask: TypeError: Expected Ptr<cv::UMat> for argument '%s' 类型错误:预期 Ptr<cv::umat> 在 python 中使用 mss 库作为参数“mat”</cv::umat> - TypeError: Expected Ptr<cv::UMat> for argument 'mat' using mss library in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM