Python 使用滑块编辑颜色值
我的目标是在查看结果时使用单独的滑块编辑这6个值,以便快速优化脚本检测到的内容Python 使用滑块编辑颜色值,python,python-2.7,opencv,numpy,tkinter,Python,Python 2.7,Opencv,Numpy,Tkinter,我的目标是在查看结果时使用单独的滑块编辑这6个值,以便快速优化脚本检测到的内容 lower_blue = np.array([110,50,50]) upper_blue = np.array([130,255,255])) ~Updated Script~新一期“当我从网络摄像头捕获帧时,如何让滑块与值交互?” 所以这里有一些问题。首先,您正在覆盖这些行中的w1、w2、w3…变量: w1 = w1.get() w2 = w2.get() w3 = w3.get() w4 = w4.get()
lower_blue = np.array([110,50,50])
upper_blue = np.array([130,255,255]))
~Updated Script~新一期“当我从网络摄像头捕获帧时,如何让滑块与值交互?”
所以这里有一些问题。首先,您正在覆盖这些行中的
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
小部件可能会导致一些非常奇怪的行为,因此作为一种解决方法,我使用了多处理模块中的同步阵列
,它是线程安全的]
我建议你去仔细阅读。然而,为了让您开始,下面的代码给出了一个示例,说明如何在一个线程中运行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()
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()