You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
2.0 KiB
68 lines
2.0 KiB
4 years ago
|
import cv2
|
||
|
import numpy as np
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
def nothing(*arg):
|
||
|
pass
|
||
|
|
||
|
h1_old = 0
|
||
|
s1_old = 0
|
||
|
v1_old = 0
|
||
|
h2_old = 0
|
||
|
s2_old = 0
|
||
|
v2_old = 0
|
||
|
|
||
|
cv2.namedWindow( "result" ) # создаем главное окно
|
||
|
cv2.namedWindow( "settings" ) # создаем окно настроек
|
||
|
|
||
|
cam = cv2.VideoCapture(0)
|
||
|
# создаем 6 бегунков для настройки начального и конечного цвета фильтра
|
||
|
cv2.createTrackbar('h1', 'settings', 0, 255, nothing)
|
||
|
cv2.createTrackbar('s1', 'settings', 0, 255, nothing)
|
||
|
cv2.createTrackbar('v1', 'settings', 0, 255, nothing)
|
||
|
cv2.createTrackbar('h2', 'settings', 255, 255, nothing)
|
||
|
cv2.createTrackbar('s2', 'settings', 255, 255, nothing)
|
||
|
cv2.createTrackbar('v2', 'settings', 255, 255, nothing)
|
||
|
crange = [0,0,0, 0,0,0]
|
||
|
|
||
|
while True:
|
||
|
flag, img = cam.read()
|
||
4 years ago
|
# Перевод изображения в HSV
|
||
|
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
|
||
4 years ago
|
|
||
|
# считываем значения бегунков
|
||
|
h1 = cv2.getTrackbarPos('h1', 'settings')
|
||
|
s1 = cv2.getTrackbarPos('s1', 'settings')
|
||
|
v1 = cv2.getTrackbarPos('v1', 'settings')
|
||
|
h2 = cv2.getTrackbarPos('h2', 'settings')
|
||
|
s2 = cv2.getTrackbarPos('s2', 'settings')
|
||
|
v2 = cv2.getTrackbarPos('v2', 'settings')
|
||
|
|
||
|
# формируем начальный и конечный цвет фильтра
|
||
|
h_min = np.array((h1, s1, v1), np.uint8)
|
||
|
h_max = np.array((h2, s2, v2), np.uint8)
|
||
|
|
||
|
# накладываем фильтр на кадр в модели HSV
|
||
|
thresh = cv2.inRange(hsv, h_min, h_max)
|
||
|
|
||
|
cv2.imshow('result', thresh)
|
||
|
|
||
|
ch = cv2.waitKey(5)
|
||
|
|
||
|
if h1 != h1_old or s1 != s1_old or v1 != v1_old \
|
||
|
or h2 != h2_old or s2 != s2_old or v2 != v2_old :
|
||
|
print ("resault: (%d,%d,%d),(%d,%d,%d)"%(h1,s1,v1,h2,s2,v2))
|
||
|
|
||
|
h1_old = h1
|
||
|
s1_old = s1
|
||
|
v1_old = v1
|
||
|
h2_old = h2
|
||
|
s2_old = s2
|
||
|
v2_old = v2
|
||
|
|
||
|
if ch == 27:
|
||
|
break
|
||
|
|
||
|
cam.release()
|
||
4 years ago
|
cv2.destroyAllWindows()
|