'''
@Author: your name
@Date: 2020-03-02 17:20:30
@LastEditTime: 2020-03-03 21:40:00
@LastEditors: Please set LastEditors
@Description: 主要为了展示opencv的inrange函数
通过img打开或者关闭摄像头,之后通过控制几个滑动条改变参数
@FilePath: \Software VSCode_Python\02_Queue.py
'''
import threading
import time
from queue import Queue
import tkinter
import cv2 as cv
import numpy as np
global channel_Scale,switch_img
channel_Scale = list()
def root_thread():
global switch_img
global channel_Scale
switch_img = "OFF"
def img_control():
global switch_img
if switch_img == "OFF":
switch_img = "ON"
thread_cv = threading.Thread(target=cv_thread,name="opencv")
thread_cv.start()
else:
switch_img = "OFF"
def HSV_scale_black():
try:
global channel_Scale
channel_Scale[0].set(0)
channel_Scale[1].set(180)
channel_Scale[2].set(0)
channel_Scale[3].set(255)
channel_Scale[4].set(0)
channel_Scale[5].set(46)
except:
print("HSV_scale_black")
def HSV_scale_grey():
try:
global channel_Scale
channel_Scale[0].set(0)
channel_Scale[1].set(180)
channel_Scale[2].set(0)
channel_Scale[3].set(43)
channel_Scale[4].set(46)
channel_Scale[5].set(220)
except:
print("HSV_scale_grey")
def HSV_scale_white():
try:
global channel_Scale
channel_Scale[0].set(0)
channel_Scale[1].set(180)
channel_Scale[2].set(0)
channel_Scale[3].set(30)
channel_Scale[4].set(221)
channel_Scale[5].set(255)
except:
print("HSV_scale_white")
def HSV_scale_red1():
try:
global channel_Scale
channel_Scale[0].set(0)
channel_Scale[1].set(10)
channel_Scale[2].set(43)
channel_Scale[3].set(255)
channel_Scale[4].set(46)
channel_Scale[5].set(255)
except:
print("HSV_scale_red1")
def HSV_scale_red2():
try:
global channel_Scale
channel_Scale[0].set(156)
channel_Scale[1].set(180)
channel_Scale[2].set(43)
channel_Scale[3].set(255)
channel_Scale[4].set(46)
channel_Scale[5].set(255)
except:
print("HSV_scale_red2")
def HSV_scale_yellow():
try:
global channel_Scale
channel_Scale[0].set(26)
channel_Scale[1].set(34)
channel_Scale[2].set(43)
channel_Scale[3].set(255)
channel_Scale[4].set(46)
channel_Scale[5].set(255)
except:
print("HSV_scale_yellow")
def HSV_scale_green():
try:
global channel_Scale
channel_Scale[0].set(35)
channel_Scale[1].set(77)
channel_Scale[2].set(43)
channel_Scale[3].set(255)
channel_Scale[4].set(46)
channel_Scale[5].set(255)
except:
print("HSV_scale_green")
def HSV_scale_orange():
try:
global channel_Scale
channel_Scale[0].set(11)
channel_Scale[1].set(25)
channel_Scale[2].set(43)
channel_Scale[3].set(255)
channel_Scale[4].set(46)
channel_Scale[5].set(255)
except:
print("HSV_scale_orange")
root = tkinter.Tk()
#窗口使用变量初始化开始
screen_Width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
width = screen_Width//2
height = screen_height//2+50
root_win_par = '%dx%d+%d+%d'% (width, height, (screen_Width-width)/2, (screen_height-height)/2)
root.geometry(root_win_par)
tkinter.Button(root,text="img",command=img_control).place(x=10,y=10,width=50,height=25)
button_color_text=("black","grey","white","red_1","red_2","orange","yellow","green")
button_color_command=(HSV_scale_black,HSV_scale_grey,HSV_scale_white,HSV_scale_red1,HSV_scale_red2,HSV_scale_orange,HSV_scale_yellow,HSV_scale_green)
for i in range(len(button_color_text)):
tkinter.Button(root,text=button_color_text[i],command=button_color_command[i]).place(x=70+55*i,y=10,width=50,height=25)
scale_text=("H lowerb","H upperb","S lowerb","S upperb","V lowerb","V upperb")
for i in range(6):
channel_Scale.append(tkinter.IntVar())
tkinter.Scale(root,variable=channel_Scale[i],from_ = 0,to = 255,orient=tkinter.HORIZONTAL,width = 10).place(x=75,y=50+i*50,width=400,height=40)
tkinter.Label(root,text=scale_text[i]).place(x=10,y=50+i*50,width=55,height=40)
root.mainloop()
def cv_thread():
global switch_img
global channel_Scale
def interval_acquisition():
global channel_Scale
lowerb=list()
upperb=list()
for i in channel_Scale[::2]:
lowerb.append(i.get())
for i in channel_Scale[1::2]:
upperb.append(i.get())
return np.array(lowerb) ,np.array(upperb)
video = cv.VideoCapture(0)
while(video.isOpened()):
ret , img=video.read()
if ret == True:
img = cv.cvtColor(img,cv.COLOR_BGR2HSV)
img = cv.flip(img,1)
lowerb,upperb=interval_acquisition()
img = cv.inRange(img,lowerb,upperb)
cv.imshow("video",img)
cv.waitKey(10)
if switch_img == "OFF":
break
video.release()
cv.destroyAllWindows()
if __name__ == "__main__":
thread_root = threading.Thread(target=root_thread,name="win")
thread_root.start()