-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathshapeCount
316 lines (248 loc) · 12.2 KB
/
shapeCount
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import numpy as np
import cv2
import time
show = False
global triangle
global rectangle
global circle
def findBiggestShape():
# Capturing video through webcam
webcam = cv2.VideoCapture(0)
ret = webcam.set(3, 640)
ret = webcam.set(4, 480)
frame_rate = 10
prev = 0
# Start a while loop
while(1):
# Reading the video from the
# webcam in image frames
time_elapsed = time.time() - prev
_, imageFrame = webcam.read()
if time_elapsed > 1./frame_rate:
prev = time.time()
# Convert the imageFrame in
# BGR(RGB color space) to
# HSV(hue-saturation-value)
# color space
hsvFrame = cv2.cvtColor(imageFrame, cv2.COLOR_BGR2HSV)
# Set range for red color and
# define mask
# red_lower = np.array([136, 87, 111], np.uint8)
# red_upper = np.array([180, 255, 255], np.uint8)
# red_mask = cv2.inRange(hsvFrame, red_lower, red_upper)
# lower mask (0-10)
lower_red = np.array([0, 50, 50])
upper_red = np.array([10, 255, 255])
mask0 = cv2.inRange(hsvFrame, lower_red, upper_red)
# upper mask (170-180)
lower_red = np.array([170, 50, 50])
upper_red = np.array([180, 255, 255])
mask1 = cv2.inRange(hsvFrame, lower_red, upper_red)
# join my masks
red_mask = mask0+mask1
imageCanny_red = cv2.Canny(red_mask, 200, 250)
# Set range for green color and
# define mask
# green_low = np.array([50, 52, 72], np.uint8)
# green_upper = np.array([70, 255, 255], np.uint8)
# green_mask = cv2.inRange(hsvFrame, green_low, green_upper)
green_low = np.array([40, 40, 40], np.uint8)
green_upper = np.array([70, 255, 255], np.uint8)
green_mask = cv2.inRange(hsvFrame, green_low, green_upper)
imageCanny_green = cv2.Canny(green_mask, 50, 150)
# Set range for blue color and
# define mask
blue_lower = np.array([90, 80, 2], np.uint8)
blue_upper = np.array([125, 255, 255], np.uint8)
blue_mask = cv2.inRange(hsvFrame, blue_lower, blue_upper)
imageCanny_blue = cv2.Canny(blue_mask, 50, 150)
# Morphological Transform, Dilation
# for each color and bitwise_and operator
# between imageFrame and mask determines
# to detect only that particular color
kernal = np.ones((5, 5), "uint8")
# For red color
red_mask = cv2.dilate(red_mask, kernal)
res_red = cv2.bitwise_and(imageFrame, imageFrame,
mask=red_mask)
# For yellow color
green_mask = cv2.dilate(green_mask, kernal)
res_yellow = cv2.bitwise_and(imageFrame, imageFrame,
mask=green_mask)
# For blue color
blue_mask = cv2.dilate(blue_mask, kernal)
res_blue = cv2.bitwise_and(imageFrame, imageFrame,
mask=blue_mask)
# Creating contour to track red color
contours, hierarchy = cv2.findContours(imageCanny_red,
cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
i = 0
rectangle = 0
for pic, contour in enumerate(contours):
area = cv2.contourArea(contour)
if(area > 300):
# x, y, w, h = cv2.boundingRect(contour)
# imageFrame = cv2.rectangle(imageFrame, (x, y),
# (x + w, y + h),
# (0, 0, 255), 2)
if i == 0:
i = 1
continue
# cv2.approxPloyDP() function to approximate the shape
approx = cv2.approxPolyDP(
contour, 0.045 * cv2.arcLength(contour, True), True)
hull = cv2.convexHull(approx)
# print(len(approx))
# using drawContours() function
cv2.drawContours(imageFrame, [contour], 0, (0, 0, 255), 5)
# finding center point of shape
M = cv2.moments(contour)
if M['m00'] != 0.0:
x = int(M['m10']/M['m00'])
y = int(M['m01']/M['m00'])
# putting shape name at center of each shape
if len(approx) == 3:
cv2.putText(imageFrame, 'Triangle', (x, y),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
elif len(approx) == 4:
cv2.putText(imageFrame, 'Quadrilateral', (x, y),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
rectangle = rectangle + 1
else:
cv2.putText(imageFrame, 'circle', (x, y),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
# cv2.putText(imageFrame, "Red Colour", (x, y),
# cv2.FONT_HERSHEY_SIMPLEX, 1.0,
# (0, 0, 255))
# Creating contour to track green color
contours, hierarchy = cv2.findContours(imageCanny_green,
cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
i = 0
triangle = 0
for pic, contour in enumerate(contours):
area = cv2.contourArea(contour)
if(area > 300):
# x, y, w, h = cv2.boundingRect(contour)
# imageFrame = cv2.rectangle(imageFrame, (x, y),
# (x + w, y + h),
# (0, 255, 255), 2)
if i == 0:
i = 1
continue
# cv2.approxPloyDP() function to approximate the shape
approx = cv2.approxPolyDP(
contour, 0.045 * cv2.arcLength(contour, True), True)
hull = cv2.convexHull(approx)
# using drawContours() function
cv2.drawContours(imageFrame, [contour], 0, (0, 0, 255), 5)
# finding center point of shape
M = cv2.moments(contour)
if M['m00'] != 0.0:
x = int(M['m10']/M['m00'])
y = int(M['m01']/M['m00'])
# putting shape name at center of each shape
if len(approx) == 3:
cv2.putText(imageFrame, 'Triangle', (x, y),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
triangle = triangle + 1
elif len(approx) == 4:
cv2.putText(imageFrame, 'Quadrilateral', (x, y),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
else:
cv2.putText(imageFrame, 'circle', (x, y),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
# cv2.putText(imageFrame, "Yellow Colour", (x, y),
# cv2.FONT_HERSHEY_SIMPLEX,
# 1.0, (0, 255, 255))
# Creating contour to track blue color
contours, hierarchy = cv2.findContours(imageCanny_blue,
cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
i = 0
circle = 0
for pic, contour in enumerate(contours):
area = cv2.contourArea(contour)
if(area > 300):
# x, y, w, h = cv2.boundingRect(contour)
# imageFrame = cv2.rectangle(imageFrame, (x, y),
# (x + w, y + h),
# (255, 0, 0), 2)
if i == 0:
i = 1
continue
# cv2.approxPloyDP() function to approximate the shape
approx = cv2.approxPolyDP(
contour, 0.045 * cv2.arcLength(contour, True), True)
hull = cv2.convexHull(approx)
# using drawContours() function
cv2.drawContours(imageFrame, [contour], 0, (0, 0, 255), 5)
# finding center point of shape
M = cv2.moments(contour)
if M['m00'] != 0.0:
x = int(M['m10']/M['m00'])
y = int(M['m01']/M['m00'])
# putting shape name at center of each shape
if len(approx) == 3:
cv2.putText(imageFrame, 'Triangle', (x, y),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
elif len(approx) == 4:
cv2.putText(imageFrame, 'Quadrilateral', (x, y),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
else:
cv2.putText(imageFrame, 'circle', (x, y),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
circle = circle + 1
# cv2.putText(imageFrame, "Blue Colour", (x, y),
# cv2.FONT_HERSHEY_SIMPLEX,
# 1.0, (255, 0, 0))
# Program Termination
cv2.imshow("Multiple Color Detection in Real-TIme", imageFrame)
cv2.moveWindow('frame', x=0, y=0) # 原地
cv2.imshow('mask', imageCanny_red)
cv2.moveWindow('mask', x=imageFrame.shape[1], y=0) # 右边
# print(rectangle)
# print(triangle)
# print(circle)
prime = None
if rectangle > triangle and rectangle > circle:
# print("rectangle")
prime = "rectangle"
if triangle > rectangle and triangle > circle:
# print("triangle")
prime = "triangle"
if circle > triangle and circle > rectangle:
# print("circle")
prime = "circle"
if circle > triangle and circle == rectangle:
# print("circle and rectangle")
prime = "circle and rectangle"
if circle > rectangle and circle == triangle:
# print("circle and triangle")
prime = "circle and triangle"
if triangle > circle and triangle == rectangle:
# print("triangle and rectangle")
prime = "triangle and rectangle"
if triangle > rectangle and triangle == circle:
# print("triangle and circle")
prime = "triangle and circle"
if rectangle > circle and rectangle == triangle:
# print("rectangle and triangle")
prime = "rectangle and triangle"
if rectangle > triangle and rectangle == circle:
# print("rectangle and circle")
prime = "rectangle and circle"
if rectangle == triangle and rectangle == circle:
# print("circle and rectangle and triangle")
prime = "circle and rectangle and triangle"
# cv2.imshow('res', res_red)
# cv2.moveWindow('res', y=imageFrame.shape[0], x=0) # 下边
if cv2.waitKey(10) & 0xFF == ord('q'):
webcam.release()
cv2.destroyAllWindows()
return prime
break
if __name__ == "__main__":
findBiggestShape()
print(findBiggestShape())