我的目标是在查看结果时使用单独的 slider 编辑这 6 个值,以便我可以快速改进我的脚本检测到的内容。
lower_blue = np.array([110,50,50])
upper_blue = np.array([130,255,255]))
import cv2
import numpy as np
from Tkinter import *
master = Tk()
w1 = Scale(master, from_=110, to=255, orient=HORIZONTAL)
w1.pack()
w2 = Scale(master, from_=50, to=255, orient=HORIZONTAL)
w2.pack()
w3 = Scale(master, from_=50, to=255, orient=HORIZONTAL)
w3.pack()
w4 = Scale(master, from_=130, to=255, orient=HORIZONTAL)
w4.pack()
w5 = Scale(master, from_=255, to=255, orient=HORIZONTAL)
w5.pack()
w6 = Scale(master, from_=255, to=255, orient=HORIZONTAL)
w6.pack()
w1 = w1.get()
w2 = w2.get()
w3 = w3.get()
w4 = w4.get()
w5 = w5.get()
w6 = w6.get()
cap = cv2.VideoCapture(0)
while(1):
_, frame = cap.read()
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
lower_blue = np.array([w1,w2,w3])
upper_blue = np.array([w4,w5,w6])
mask = cv2.inRange(hsv, lower_blue, upper_blue)
res = cv2.bitwise_and(frame,frame, mask= mask)
cv2.imshow('frame',frame)
cv2.imshow('mask',mask)
cv2.imshow('res',res)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()
最佳答案
所以你在这里有一些问题。首先是您正在覆盖您的w1,w2,w3...
这些行中的变量:
w1 = w1.get()
w2 = w2.get()
w3 = w3.get()
w4 = w4.get()
w5 = w5.get()
w6 = w6.get()
Scale
的引用小部件,阻止您再次使用它们。while
您拥有的循环将在线程中运行,并保持响应 Scale
小部件实际上也应该在线程中运行。Scale
线程之间的小部件可能会导致一些非常奇怪的行为,因此作为一种解决方法,我使用了同步的 Array
来自 multiprocessing
线程安全的模块]Scale
的示例。一个线程中的小部件,而在另一个线程中循环。import Tkinter as tk
from threading import Thread,Event
from multiprocessing import Array
from ctypes import c_int32
class CaptureController(tk.Frame):
NSLIDERS = 6
def __init__(self,parent):
tk.Frame.__init__(self)
self.parent = parent
# create a synchronised array that other threads will read from
self.ar = Array(c_int32,self.NSLIDERS)
# create NSLIDERS Scale widgets
self.sliders = []
for ii in range(self.NSLIDERS):
# through the command parameter we ensure that the widget updates the sync'd array
s = tk.Scale(self, from_=0, to=255, orient=tk.HORIZONTAL,
command=lambda pos,ii=ii:self.update_slider(ii,pos))
s.pack()
self.sliders.append(s)
# Define a quit button and quit event to help gracefully shut down threads
tk.Button(self,text="Quit",command=self.quit).pack()
self._quit = Event()
self.capture_thread = None
# This function is called when each Scale widget is moved
def update_slider(self,idx,pos):
self.ar[idx] = c_int32(int(pos))
# This function launches a thread to do video capture
def start_capture(self):
self._quit.clear()
# Create and launch a thread that will run the video_capture function
self.capture_thread = Thread(target=video_capture, args=(self.ar,self._quit))
self.capture_thread.daemon = True
self.capture_thread.start()
def quit(self):
self._quit.set()
try:
self.capture_thread.join()
except TypeError:
pass
self.parent.destroy()
# This function simply loops over and over, printing the contents of the array to screen
def video_capture(ar,quit):
# This while loop would be replaced by the while loop in your original code
while not quit.is_set():
print ar[:]
# the slider values are all readily available through the indexes of ar
# i.e. w1 = ar[0]
# w2 = ar[1]
# etc.
if __name__ == "__main__":
root = tk.Tk()
selectors = CaptureController(root)
selectors.pack()
selectors.start_capture()
root.mainloop()
https://stackoverflow.com/questions/23983035/