-
Notifications
You must be signed in to change notification settings - Fork 1
/
New Color Virtual
160 lines (80 loc) · 3.68 KB
/
New Color Virtual
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import numpy as np
import cv2
from collections import deque
Lower_red = np.array([161, 155, 84])
Upper_red = np.array([179, 255, 255])
bpoints = [deque(maxlen=512)]
gpoints = [deque(maxlen=512)]
rpoints = [deque(maxlen=512)]
ypoints = [deque(maxlen=512)]
bindex = 0,gindex = 0,rindex = 0,yindex = 0
colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (0, 255, 255)]
colorIndex = 0
Window = np.zeros((471,636,3)) + 255
Window = cv2.rectangle(paintWindow, (40,1), (140,65), (0,0,0), 2)
Window = cv2.rectangle(paintWindow, (160,1), (255,65), colors[0], -1)
Window = cv2.rectangle(paintWindow, (275,1), (370,65), colors[1], -1)
Window = cv2.rectangle(paintWindow, (390,1), (485,65), colors[2], -1)
Window = cv2.rectangle(paintWindow, (505,1), (600,65), colors[3], -1)
cv2.putText(Window, "CLEAR ALL", (49, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2, cv2.LINE_AA)
cv2.putText(Window, "BLUE", (185, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(Window, "GREEN", (298, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(Window, "RED", (420, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(Window, "YELLOW", (520, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (150,150,150), 2, cv2.LINE_AA)
cv2.namedWindow('Paint', cv2.WINDOW_AUTOSIZE)
camera = cv2.VideoCapture(0)
while True:
(grabbed, frame) = camera.read()
frame = cv2.flip(frame, 1)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
if not grabbed:
Break
kernel = np.ones((5, 5), np.uint8)
Mask = cv2.inRange(hsv, Lower_red, Upper_red)
Mask = cv2.erode(Mask, kernel, iterations=2)
Mask = cv2.morphologyEx(Mask, cv2.MORPH_OPEN, kernel)
Mask = cv2.dilate(Mask, kernel, iterations=1)
(cnts, _) = cv2.findContours(blueMask.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
center = None
if len(cnts) > 0:
cnt = sorted(cnts, key = cv2.contourArea, reverse = True)[0]
((x, y), radius) = cv2.minEnclosingCircle(cnt)
cv2.circle(frame, (int(x), int(y)), int(radius), (0, 255, 255), 2)
M = cv2.moments(cnt)
center = (int(M['m10'] / M['m00']), int(M['m01'] / M['m00'])
if center[1] <= 65:
if 40 <= center[0] <= 140: # Clear All
bpoints = [deque(maxlen=512)]
gpoints = [deque(maxlen=512)]
rpoints = [deque(maxlen=512)]
ypoints = [deque(maxlen=512)]
bindex = 0, gindex = 0, rindex = 0,yindex = 0
paintWindow[67:,:,:] = 255
elif 160 <= center[0] <= 255:
colorIndex = 0
elif 275 <= center[0] <= 370:
colorIndex = 1
elif 390 <= center[0] <= 485:
colorIndex = 2
elif 505 <= center[0] <= 600:
colorIndex = 3
else :
if colorIndex == 0:
bpoints[bindex].appendleft(center)
elif colorIndex == 1:
gpoints[gindex].appendleft(center)
elif colorIndex == 2:
rpoints[rindex].appendleft(center)
elif colorIndex == 3:
ypoints[yindex].appendleft(center)
points = [bpoints, gpoints, rpoints, ypoints]
for i in range(len(points)):
for j in range(len(points[i])):
for k in range(1, len(points[i][j])):
if points[i][j][k - 1] is None or points[i][j][k] is None:
continue
cv2.line(frame, points[i][j][k - 1], points[i][j][k], colors[i], 2)
cv2.line(paintWindow, points[i][j][k - 1], points[i][j][k], colors[i], 2)
cv2.imshow("Web_cam_video", frame)
cv2.imshow("Paint", paintWindow)