【发布时间】:2021-11-16 19:12:15
【问题描述】:
我有一个图像,我想在 tkinter 中使用它,并使用 OpenCv 中的选择 ROI 函数和鼠标事件。我不知道如何解决这个问题,但我尽我所能,这是我的代码。 我的问题是我有错误说我在 (lbl tkimgis) 的变量没有定义。有没有其他方法可以实现这一目标或我可以做出改变的任何建议? 谢谢您的帮助。祝你有美好的一天!
from tkinter import *
import numpy as np
import cv2
import sys
import PIL.Image as imge
import PIL.ImageTk as imtk
ev = None
def click(event, u16):
global subregion, tkimgis, ev
if ev == None:
ev = event
return None
bbox = cv2.selectROI("Image", u16, fromCenter=False)
cv2.destroyWindow("Image")
print("region:", bbox)
####Here u16 is my image array from another jupyter notebook cell
tkimgis = imtk.PhotoImage(image=imge.fromarray(u16))
lbl.config(image=tkimgis)
(x,y,w,h) = bbox
subregion = na[y:y+h, x:x+w]
ev = event
root = Tk()
lbl = Label(root, image=tkimgis)
lbl.place(x=0, y=0)
root.bind('<Button-1>', lambda event: click(event))
root.mainloop()
【问题讨论】:
-
我认为您发布的代码中存在缩进问题。
-
您好,抱歉,我已经编辑了它
标签: python image opencv tkinter event-handling