使用滑块编辑颜色值

0 投票
1 回答
1550 浏览
提问于 2025-04-18 08:12

我的目标是用不同的滑块来调整这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()

1 个回答

1

你这里有几个问题。第一个是你在这些代码行中覆盖了你的 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 控件可能会导致一些奇怪的行为,所以作为解决办法,我使用了来自 multiprocessing 模块的一个同步 Array,它是线程安全的]

我建议你去了解一下 Python中的线程。不过,为了让你入门,下面的代码示例展示了如何在一个线程中运行 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()

撰写回答