Skip to content

Commit 7c3df69

Browse files
committed
added shape detection python script
1 parent 65d6b8c commit 7c3df69

File tree

4 files changed

+202
-0
lines changed

4 files changed

+202
-0
lines changed
19.2 KB
Loading
34.5 KB
Loading
37.5 KB
Loading

Python/shape-detection/untitled.py

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
import cv2
2+
import numpy as np
3+
import os
4+
shapes = {}
5+
#calculates the area of the shape
6+
7+
def area(cnt):
8+
area = cv2.contourArea(cnt)
9+
return area
10+
11+
#calculates the centroids of the shape
12+
13+
def centroids(cnt):
14+
M = cv2.moments(cnt)
15+
cx = int(M['m10']/M['m00'])
16+
cy = int(M['m01']/M['m00'])
17+
#print(cx,cy)
18+
return cx,cy
19+
20+
21+
if __name__ == '__main__':
22+
global shapes
23+
img = cv2.imread("img.png",0)
24+
image = cv2.imread("img.png")
25+
h_img,w_img=img.shape
26+
shapes = {} #creates an empty dictionary
27+
28+
ret,thresh = cv2.threshold(img , 240, 255, cv2.THRESH_BINARY)#calculates threshold of the gray image
29+
contours,hierarchy= cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
30+
31+
32+
33+
for cnt in contours:#loops through each shape
34+
approx = cv2.approxPolyDP(cnt, 0.01*cv2.arcLength(cnt, True), True)#it approximates a contour shape
35+
cv2.drawContours(image , [approx], 0, (0), 5)
36+
x = approx.ravel()[0]
37+
y = approx.ravel()[1]
38+
39+
if len(approx) == 3:
40+
shapes["Triangle"]=[]#initialising the key "Triangle" with an empty list
41+
42+
ar=area(cnt)#calling area function
43+
cx,cy=centroids(cnt)#calling centroids function
44+
color = image[cy,cx]#gives pixels of point(cx,cy)
45+
blue,green,red=color#unpacking of tuple
46+
47+
m=max(blue,green,red)
48+
if color[0]==m:
49+
col="blue"
50+
elif color[1]==m:
51+
col= "green"
52+
else:
53+
col="red"
54+
shapes["Triangle"].append(col)#appending the list with property color
55+
shapes["Triangle"].append(ar)#appending the list with property area
56+
57+
shapes["Triangle"].append(cx)#appending the list with property cx
58+
59+
shapes["Triangle"].append(cy)#appending the list with property cy
60+
61+
62+
elif len(approx) == 4 :
63+
(x, y, w, h) = cv2.boundingRect(approx)# to highlight the region of interest
64+
arr = w / float(h)#calculates aspect ratio
65+
areaa=area(cnt)
66+
if arr >= 0.95 and arr <= 1.05 and w<(w_img-10) :#if aspect ratio is approximately equal to one then it is a square
67+
#w<1000 because the w of the whole image is 1024
68+
69+
shapes["Square"]=[]
70+
ar=area(cnt)
71+
cx,cy=centroids(cnt)
72+
color = image[cy,cx]
73+
blue,green,red=color
74+
75+
m=max(blue,green,red)
76+
if color[0]==m:
77+
col="blue"
78+
elif color[1]==m:
79+
col= "green"
80+
else:
81+
col="red"
82+
shapes["Square"].append(col)
83+
shapes["Square"].append(ar)
84+
shapes["Square"].append(cx)
85+
shapes["Square"].append(cy)
86+
elif(w<(w_img-10) and areaa>=0.99*w*h):
87+
shapes["Rectangle"]=[]
88+
ar=area(cnt)
89+
cx,cy=centroids(cnt)
90+
color = image[cy,cx]
91+
blue,green,red=color
92+
93+
m=max(blue,green,red)
94+
if color[0]==m:
95+
col="blue"
96+
elif color[1]==m:
97+
col= "green"
98+
else:
99+
col="red"
100+
shapes["Rectangle"].append(col)
101+
shapes["Rectangle"].append(ar)
102+
shapes["Rectangle"].append(cx)
103+
shapes["Rectangle"].append(cy)
104+
105+
elif(w<(w_img-10)):
106+
shapes["Rhombus"]=[]
107+
ar=area(cnt)
108+
cx,cy=centroids(cnt)
109+
color = image[cy,cx]
110+
blue,green,red=color
111+
112+
m=max(blue,green,red)
113+
if color[0]==m:
114+
col="blue"
115+
elif color[1]==m:
116+
col= "green"
117+
else:
118+
col="red"
119+
shapes["Rhombus"].append(col)
120+
shapes["Rhombus"].append(ar)
121+
shapes["Rhombus"].append(cx)
122+
shapes["Rhombus"].append(cy)
123+
124+
125+
elif len(approx) == 5:
126+
shapes["Pentagon"]=[]
127+
ar=area(cnt)
128+
cx,cy=centroids(cnt)
129+
color = image[cy,cx]
130+
blue,green,red=color
131+
132+
m=max(blue,green,red)
133+
if color[0]==m:
134+
col="blue"
135+
elif color[1]==m:
136+
col= "green"
137+
else:
138+
col="red"
139+
shapes["Pentagon"].append(col)
140+
shapes["Pentagon"].append(ar)
141+
shapes["Pentagon"].append(cx)
142+
shapes["Pentagon"].append(cy)
143+
elif len(approx) == 6:
144+
shapes["Hexagon"]=[]
145+
ar=area(cnt)
146+
cx,cy=centroids(cnt)
147+
color = image[cy,cx]
148+
blue,green,red=color
149+
150+
m=max(blue,green,red)
151+
if color[0]==m:
152+
col="blue"
153+
elif color[1]==m:
154+
col= "green"
155+
else:
156+
col="red"
157+
shapes["Hexagon"].append(col)
158+
shapes["Hexagon"].append(ar)
159+
shapes["Hexagon"].append(cx)
160+
shapes["Hexagon"].append(cy)
161+
elif 6 < len(approx) < 15:
162+
shapes["Ellipse"]=[]
163+
ar=area(cnt)
164+
cx,cy=centroids(cnt)
165+
color = image[cy,cx]
166+
blue,green,red=color
167+
168+
m=max(blue,green,red)
169+
if color[0]==m:
170+
col="blue"
171+
elif color[1]==m:
172+
col= "green"
173+
else:
174+
col="red"
175+
shapes["Ellpise"].append(col)
176+
shapes["Ellpise"].append(ar)
177+
shapes["Ellpise"].append(cx)
178+
shapes["Ellpise"].append(cy)
179+
else:
180+
#if contour has no vertex then it is a circle
181+
shapes["Circle"]=[]
182+
ar=area(cnt)
183+
cx,cy=centroids(cnt)
184+
color = image[cy,cx]
185+
blue,green,red=color
186+
187+
m=max(blue,green,red)
188+
if color[0]==m:
189+
col="blue"
190+
elif color[1]==m:
191+
col= "green"
192+
else:
193+
col="red"
194+
shapes["Circle"].append(col)
195+
shapes["Circle"].append(ar)
196+
shapes["Circle"].append(cx)
197+
shapes["Circle"].append(cy)
198+
199+
200+
201+
202+
print(shapes)

0 commit comments

Comments
 (0)