-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyuv_trackbar.py
54 lines (48 loc) · 1.73 KB
/
yuv_trackbar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import cv2
import numpy as np
def nothing(x):
pass
# Create a black image, a window
img = np.zeros((300,300,3), np.uint8)
cv2.namedWindow('image')
# create trackbars for color changeh
cv2.createTrackbar('Y','image',0,255,nothing)
cv2.createTrackbar('U','image',0,255,nothing)
cv2.createTrackbar('V','image',0,255,nothing)
cap = cv2.VideoCapture(0)
cv2.createTrackbar('Blurring','image',10,30,nothing)
cap = cv2.VideoCapture(1)
while True:
y = cv2.getTrackbarPos('Y','image')
u = cv2.getTrackbarPos('U','image')
v = cv2.getTrackbarPos('V','image')
sigma = cv2.getTrackbarPos('Blurring','image')
sigma = sigma/10
img[:] = [y,u,v]
img= cv2.cvtColor(img, cv2.COLOR_BGR2YUV)
#cv2.imshow('image',img)
_,f = cap.read()
img_yuv = cv2.cvtColor(f, cv2.COLOR_BGR2YUV)
img_yuv = cv2.GaussianBlur(img_yuv, (7, 7), sigma )
#cv2.imshow("g1",img_yuv)
#img_yuv[:,:,2] = cv2.equalizeHist(img_yuv[:,:,2])
#cv2.imshow("g2",img_yuv
#blur = cv2.GaussianBlur(img_yuv,(7,7),1)
#img_yuv = cv2.medianBlur(blur ,5)
mask = cv2.inRange(img_yuv, (np.array([y-45,u-30,v-30])), (np.array([y+45,u+30,v+30])))
cv2.imshow("Masking",mask)
#erode = cv2.erode(mask,None,iterations = 1)
#dilate = cv2.dilate(erode,None,iterations = 1)
ret,contours,hierarchy = cv2.findContours(mask,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
#if len(contours)>0:
#c = max(contours, key=cv2.contourArea)
x, y, w, h = cv2.boundingRect(cnt)
cv2.rectangle(f,(x,y),(x+w,y+h),[255,0,0],2)
#print x, y, w, h
#img_output = cv2.cvtColor(img_yuv, cv2.COLOR_YUV2BGR)
#cv2.imshow('Histogram equalized', f)
if cv2.waitKey(25)&0xff==27:
break
cap.release()
cv2.destroyAllWindows()