【发布时间】:2021-10-27 23:14:18
【问题描述】:
我一直在尝试使用 tkinter 制作第 29 节中想到的运动检测器应用程序的 GUI,我希望它像,将有 4 个不同的按钮,4 个用于不同的框架,即 Delta 框架,灰度,彩色和阈值框架。 我一直在尝试这个,但是当我这样做时。
按钮起作用,但它显示的帧始终是第一帧,它不会更新。
我的代码:
import tkinter
import cv2, time
from tkinter import *
from tkinter import ttk
top = tkinter.Tk()
first_frame=None
video=cv2.VideoCapture(0)
while True:
check, frame = video.read()
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
gray=cv2.GaussianBlur(gray,(21,21),0)
if first_frame is None:
first_frame=gray
continue
delta_frame=cv2.absdiff(first_frame,gray)
thresh_delta=cv2.threshold(delta_frame, 30, 255, cv2.THRESH_BINARY)[1]
thresh_delta=cv2.dilate(thresh_delta, None, iterations=2)
(cnts,_) = cv2.findContours(thresh_delta.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for countour in cnts:
if cv2.contourArea(countour) < 1000:
continue
(x, y, w, h) = cv2.boundingRect(countour)
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 3)
def Gray():
cv2.imshow("Gray",gray)
cv2.imshow("delta frame", delta_frame)
cv2.imshow("Threshold Delta Frame", thresh_delta)
cv2.imshow("Color Frame", frame)
Button_Gray= tkinter.Button(top, text="Grayscale", command= Gray)
Button_Gray.pack()
top.mainloop()
key=cv2.waitKey(1)
print(gray)
if key==ord("q"):
break
video.realease()
cv2.destroyAllWindows
非常感谢您的快速帮助!
问候 高拉夫·辛格
【问题讨论】:
-
top.mainloop()永远不会返回,它将永远运行。这就是您的问题的原因。此外,您不应该混合使用 Tkinter 和 OpenCV 的 GUI 功能。选择一个 GUI 或另一个。
标签: python-3.x opencv user-interface tkinter