我正在尝试创建一个用户界面,用户可以在其中编辑实时流openCV应用程序的阈值。运行此代码我得到一个错误,说明全局变量" slider"没有定义,我真的不明白为什么:
from threading import Thread
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
import imutils
from Tkinter import*
thresh=0
def get_thresh():
global thresh
global slider
thresh=slider.get()
def funkcija1():
window=Tk()
window.title("Test")
window.geometry('800x480')
slider=Scale(window,from_=0, to=255,command=get_thresh())
slider.pack()
window.mainloop()
def funkcija2():
camera=PiCamera()
rawCapture=PiRGBArray(camera)
camera.resolution=(640,480)
camera.framerate=32
rawCapture=PiRGBArray(camera,size=(640,480))
time.sleep(0.1)
for frame in camera.capture_continuous(rawCapture,format="bgr",use_video_port=True):
image=frame.array
qray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
cv2.imshow("crnobela",qray)
threshold=cv2.threshold(qray,thresh,255,cv2.THRESH_BINARY)[1]
cv2.imshow("thresh",threshold)
canny=cv2.Canny(threshold,30,200)
cv2.imshow("canny",canny)
konture=cv2.findContours(canny.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)[1]
cv2.drawContours(image,konture,-1,(0,255,0),3)
cv2.imshow("slika",image)
key=cv2.waitKey(1) & 0xFF
rawCapture.truncate(0)
if key == ord("q"):
cv2.destroyAllWindows()
break
def Main():
t1=Thread(target=funkcija1,args=())
t2=Thread(target=funkcija2,args=())
t1.start()
t2.start()
if __name__ == '__main__':
Main()