Skip to content

Commit 8da46b0

Browse files
authored
Merge pull request #285 from NEEHITGOYAL/shapes
python script for shapes
2 parents f327753 + 9340737 commit 8da46b0

File tree

5 files changed

+213
-0
lines changed

5 files changed

+213
-0
lines changed

Python/shape-detection/README.md

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

0 commit comments

Comments
 (0)