-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMask Creator.py
More file actions
58 lines (47 loc) · 1.96 KB
/
Mask Creator.py
File metadata and controls
58 lines (47 loc) · 1.96 KB
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
import cv2
import imutils
import numpy as np
cap = cv2.VideoCapture(0)
cv2.namedWindow("Trackbars")
cv2.createTrackbar("L-H", "Trackbars", 0, 180, lambda x:x)
cv2.createTrackbar("L-S", "Trackbars", 0, 255, lambda x:x)
cv2.createTrackbar("L-V", "Trackbars", 0, 255, lambda x:x)
cv2.createTrackbar("U-H", "Trackbars", 0, 180, lambda x:x)
cv2.createTrackbar("U-S", "Trackbars", 0, 255, lambda x:x)
cv2.createTrackbar("U-V", "Trackbars", 0, 255, lambda x:x)
while True:
_, frame = cap.read()
l_h = cv2.getTrackbarPos("L-H", "Trackbars")
l_s = cv2.getTrackbarPos("L-S", "Trackbars")
l_v = cv2.getTrackbarPos("L-V", "Trackbars")
u_h = cv2.getTrackbarPos("U-H", "Trackbars")
u_s = cv2.getTrackbarPos("U-S", "Trackbars")
u_v = cv2.getTrackbarPos("U-V", "Trackbars")
lower_color = np.array([l_h, l_s, l_v])
upper_color = np.array([u_h, u_s, u_v])
blurred = cv2.GaussianBlur(frame, (11, 11), 0)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, lower_color, upper_color)
mask = cv2.erode(mask, None, iterations=2)
mask = cv2.dilate(mask, None, iterations=2)
# Contours detection
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
if len(cnts) > 0:
c = max(cnts, key=cv2.contourArea)
((x, y), radius) = cv2.minEnclosingCircle(c)
if cv2.contourArea(c) > 400:
x, y, h, w = cv2.boundingRect(c)
mid_x = int((x + x + w)/2)
mid_y = int((y + y + h)/2)
cv2.circle(frame, (int(x), int(y)), int(radius), (0, 255, 255), 2)
cv2.line(frame, (int(x), 0), (int(x), 480), (0, 255, 0), 3)
cv2.line(frame, (0, int(y)), (640, int(y)), (0, 255, 0), 3)
cv2.imshow("Frame", frame)
cv2.imshow("Mask", mask)
key = cv2.waitKey(1)
print(key)
if key == 27:
break
cap.release()
cv2.destroyAllWindows()