-
Notifications
You must be signed in to change notification settings - Fork 3
/
34_RealTime_Resim_Cizme.py
162 lines (114 loc) · 5.17 KB
/
34_RealTime_Resim_Cizme.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
""" -*- coding: utf-8 -*-
@author: omerkocadayi
https://github.com/omerkocadayi
https://www.linkedin.com/in/omerkocadayi/ """
# Mavi bir nesne ile frame üzerine çizimler yapabilirsiniz.
import cv2
import numpy as np
from collections import deque
cap = cv2.VideoCapture(0)
lower_blue= np.array([100,60,60])
upper_blue= np.array([140,255,255])
blue_points = [deque(maxlen=512)]
green_points = [deque(maxlen=512)]
red_points = [deque(maxlen=512)]
yellow_points = [deque(maxlen=512)]
blue_index=0
green_index=0
red_index=0
yellow_index=0
colors = [(255,0,0),(0,255,0),(0,0,255),(0,255,255)]
color_index = 0
paintWindow = np.zeros((471,636,3))+255
paintWindow = cv2.rectangle(paintWindow,(40,1),(140,65),(0,0,0),2)
paintWindow = cv2.rectangle(paintWindow,(160,1),(255,65),colors[0],-1)
paintWindow = cv2.rectangle(paintWindow,(275,1),(370,65),colors[1],-1)
paintWindow = cv2.rectangle(paintWindow,(390,1),(485,65),colors[2],-1)
paintWindow = cv2.rectangle(paintWindow,(505,1),(600,65),colors[3],-1)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(paintWindow,"CLEAR ALL",(49,33),font,0.5,(0,0,0),2,cv2.LINE_AA)
cv2.putText(paintWindow,"BLUE",(185,33),font,0.5,(255,255,255),2,cv2.LINE_AA)
cv2.putText(paintWindow,"GREEN",(298,33),font,0.5,(255,255,255),2,cv2.LINE_AA)
cv2.putText(paintWindow,"RED",(420,33),font,0.5,(255,255,255),2,cv2.LINE_AA)
cv2.putText(paintWindow,"YELLOW",(520,33),font,0.5,(255,255,255),2,cv2.LINE_AA)
cv2.namedWindow("Paint")
while 1:
ret,frame = cap.read()
frame = cv2.flip(frame,1)
hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
frame = cv2.rectangle(frame,(40,1),(140,65),(0,0,0),2)
frame = cv2.rectangle(frame,(160,1),(255,65),colors[0],-1)
frame = cv2.rectangle(frame,(275,1),(370,65),colors[1],-1)
frame = cv2.rectangle(frame,(390,1),(485,65),colors[2],-1)
frame = cv2.rectangle(frame,(505,1),(600,65),colors[3],-1)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(frame,"CLEAR ALL",(49,33),font,0.5,(0,0,0),2,cv2.LINE_AA)
cv2.putText(frame,"BLUE",(185,33),font,0.5,(255,255,255),2,cv2.LINE_AA)
cv2.putText(frame,"GREEN",(298,33),font,0.5,(255,255,255),2,cv2.LINE_AA)
cv2.putText(frame,"RED",(420,33),font,0.5,(255,255,255),2,cv2.LINE_AA)
cv2.putText(frame,"YELLOW",(520,33),font,0.5,(255,255,255),2,cv2.LINE_AA)
if ret is False:
break
mask = cv2.inRange(hsv,lower_blue,upper_blue)
mask = cv2.erode(mask,(5,5),iterations =2)
mask = cv2.morphologyEx(mask,cv2.MORPH_OPEN,(5,5))
mask = cv2.dilate(mask,(5,5),iterations = 1)
contours,_ =cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
center = None
if len(contours) > 0:
max_contours = sorted(contours, key = cv2.contourArea, reverse=True)[0]
((x,y),radius) = cv2.minEnclosingCircle(max_contours)
cv2.circle(frame,(int(x),int(y)),int(radius),(255,255,0),3)
M = cv2.moments(max_contours)
center = (int(M["m10"]/M["m00"]),int(M["m01"]/M["m00"]))
if center[1] <= 65:
if 40<=center[0]<=140:
blue_points = [deque(maxlen=512)]
green_points = [deque(maxlen=512)]
red_points = [deque(maxlen=512)]
yellow_points = [deque(maxlen=512)]
blue_index=0
green_index=0
red_index=0
yellow_index=0
paintWindow[67:,:,:]=255
elif 160<=center[0]<=255:
color_index = 0
elif 275<=center[0]<=370:
color_index = 1
elif 390<=center[0]<=485:
color_index = 2
elif 505<=center[0]<=600:
color_index = 3
else:
if color_index == 0:
blue_points[blue_index].appendleft(center)
elif color_index == 1:
green_points[green_index].appendleft(center)
elif color_index == 2:
red_points[red_index].appendleft(center)
elif color_index == 3:
yellow_points[yellow_index].appendleft(center)
else:
blue_points.append(deque(maxlen=512))
blue_index+=1
green_points.append(deque(maxlen=512))
green_index+=1
red_points.append(deque(maxlen=512))
red_index+=1
yellow_points.append(deque(maxlen=512))
yellow_index+=1
points = [blue_points,green_points,red_points,yellow_points]
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("Frame",frame)
cv2.imshow("Paint",paintWindow)
if cv2.waitKey(3) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()