# -*- coding: utf-8 -*-
"""
Created on Mon Mar 22 13:22:57 2021

@author: shsbnu
"""
import time
import cv2

#定义一个函数
def nothing(x):
    pass
#定义一个新窗口
cv2.namedWindow('img')
cv2.createTrackbar('hmin','img',161,255,nothing)
cv2.createTrackbar('smin','img',19,255,nothing)
cv2.createTrackbar('vmin','img',17,255,nothing)
#分别设置调整最大值的滚动条
cv2.createTrackbar('hmax','img',77,255,nothing)
cv2.createTrackbar('smax','img',255,255,nothing)
cv2.createTrackbar('vmax','img',255,255,nothing)

cap=cv2.VideoCapture("autodrive.mp4")

while True:
    res,img = cap.read()
    if not res:
        cap=cv2.VideoCapture("autodrive.mp4")
        continue
        
    cv2.imshow("1",img)
    hmin=cv2.getTrackbarPos('hmin','img')
    smin=cv2.getTrackbarPos('smin','img')
    vmin=cv2.getTrackbarPos('vmin','img')
    
    hmax=cv2.getTrackbarPos('hmax','img')
    smax=cv2.getTrackbarPos('smax','img')
    vmax=cv2.getTrackbarPos('vmax','img')
    
    low_hsv = (hmin,smin,vmin)
    high_hsv = (hmax,smax,vmax)
    
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) 
    newimg = cv2.inRange(hsv,low_hsv,high_hsv)
    cv2.imshow("img1",newimg)
    
    k=cv2.waitKey(50)
    if k==27:
        break
cv2.destroyAllWindows()	
