-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path48 Contour detection using webcam and trackbars.py
104 lines (73 loc) · 3.15 KB
/
48 Contour detection using webcam and trackbars.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"""
@author: Osama Shakeel
Counting Fingers tutorials using open cv
binding trackbar with video
"""
import cv2
import numpy as np
#Read Camera
cap = cv2.VideoCapture(0,cv2.CAP_DSHOW)
def nothing(x):
pass
#window name
cv2.namedWindow("Color Adjustments",cv2.WINDOW_NORMAL)
cv2.resizeWindow("Color Adjustments", (300, 300))
cv2.createTrackbar("Thresh", "Color Adjustments", 0, 255, nothing)
#COlor Detection Track
cv2.createTrackbar("Lower_H", "Color Adjustments", 0, 255, nothing)
cv2.createTrackbar("Lower_S", "Color Adjustments", 0, 255, nothing)
cv2.createTrackbar("Lower_V", "Color Adjustments", 0, 255, nothing)
cv2.createTrackbar("Upper_H", "Color Adjustments", 255, 255, nothing)
cv2.createTrackbar("Upper_S", "Color Adjustments", 255, 255, nothing)
cv2.createTrackbar("Upper_V", "Color Adjustments", 255, 255, nothing)
while True:
_,frame = cap.read()
frame = cv2.resize(frame,(400,400))
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
#detecting hand
l_h = cv2.getTrackbarPos("Lower_H", "Color Adjustments")
l_s = cv2.getTrackbarPos("Lower_S", "Color Adjustments")
l_v = cv2.getTrackbarPos("Lower_V", "Color Adjustments")
u_h = cv2.getTrackbarPos("Upper_H", "Color Adjustments")
u_s = cv2.getTrackbarPos("Upper_S", "Color Adjustments")
u_v = cv2.getTrackbarPos("Upper_V", "Color Adjustments")
lower_bound = np.array([l_h, l_s, l_v])
upper_bound = np.array([u_h, u_s, u_v])
#Creating Mask
mask = cv2.inRange(hsv, lower_bound, upper_bound)
#filter mask with image
filtr = cv2.bitwise_and(frame, frame, mask=mask)
#mask1 = cv2.bitwise_not(mask) #cv2.THRESH_BINARY
m_g = cv2.getTrackbarPos("Thresh", "Color Adjustments") #getting track bar value
ret,thresh = cv2.threshold(mask,m_g,255,cv2.THRESH_BINARY_INV)
dilata = cv2.dilate(thresh,(1,1),iterations = 6)
#findcontour(img,contour_retrival_mode,method)
cnts,hier = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
#Here cnts is a list of contours. ANd each contour is an array with x, y cordinate
#hier variable called hierarchy and it contain image information.
#print("Number of contour==",cnts,"\ntotal contour==",len(cnts))
#print("Hierarchy==\n",hier)
#Draw the contours
#frame= cv2.drawContours(frame,cnts,-1,(176,10,15),4)
# loop over the contours
for c in cnts:
epsilon = 0.0001*cv2.arcLength(c,True)
data= cv2.approxPolyDP(c,epsilon,True)
hull = cv2.convexHull(data)
cv2.drawContours(frame, [c], -1, (50, 50, 150), 2)
cv2.drawContours(frame, [hull], -1, (0, 255, 0), 2)
"""
hull = cv2.convexHull(data,returnPoints = False)
defect = cv2.convexityDefects(data[0],hull)
print("defect==",defect)
"""
#Display
cv2.imshow("Thresh", thresh)
cv2.imshow("mask==",mask)
cv2.imshow("filter==",filtr)
cv2.imshow("Result", frame)
key = cv2.waitKey(25) &0xFF
if key == 27:
break
cap.release()
cv2.destroyAllWindows()