Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 03fe4d7

Browse files
authored
Add files via upload
1 parent 5a2944c commit 03fe4d7

19 files changed

+151
-0
lines changed

‎OpenCv/Image_analysis.py‎

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# To detect the position, shape and color of an image.
2+
3+
import cv2
4+
import numpy as np
5+
import os
6+
import argparse
7+
import imutils
8+
import math
9+
from os.path import join,isfile
10+
filename = 'outputimage.csv'
11+
12+
#subroutine to write results to a csv
13+
def writecsv(color,shape,(cx,cy)):
14+
global filename
15+
16+
filep = open(filename,'a')
17+
18+
datastr = "," + color + "-" + shape + "-" + str(cx) + "-" + str(cy)
19+
20+
filep.write(datastr)
21+
filep.close()
22+
23+
def detectShape(image, c, cX, cY, color, ll):
24+
peri = cv2.arcLength(c, True)
25+
approx = cv2.approxPolyDP(c, 0.04 * peri, True)
26+
# CHECKING NO. OF VERTICES
27+
if len(approx) == 3:
28+
shape = "TRIANGLE"
29+
30+
elif len(approx) == 4:
31+
32+
(x, y, w, h) = cv2.boundingRect(approx)
33+
ar = w / float(h)
34+
rect_diagonal = math.sqrt(w * w + h * h)
35+
36+
(x, y), radius = cv2.minEnclosingCircle(c)
37+
center = (int(x), int(y))
38+
39+
radius = int(radius)
40+
diameter = 2 * radius
41+
error = diameter / rect_diagonal
42+
if ar >= 0.95 and ar <= 1.05:
43+
shape = 'SQUARE'
44+
45+
elif error >= 0.9 and error <= 1.1:
46+
shape = 'RHOMBUS'
47+
else:
48+
shape = 'TRAPEZIUM'
49+
elif len(approx) == 5:
50+
shape = "PENTAGON"
51+
52+
elif len(approx) == 6:
53+
shape = "HEXAGON"
54+
else:
55+
shape = "CIRCLE"
56+
57+
cv2.putText(image, shape, (cX-25, cY), cv2.FONT_HERSHEY_SIMPLEX,0.5, (255, 255, 255), 1)
58+
writecsv(color, shape, (cX,cY))
59+
ll.append([color+'-'+shape+'-'+str(cX)+'-'+str(cY)])
60+
61+
def main(path):
62+
image = cv2.imread(path)
63+
64+
resized = imutils.resize(image, width=300)
65+
ratio = image.shape[0] / float(resized.shape[0])
66+
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
67+
# CONVERT IN GRAYSCALE THEN BLURRED IT THEN FIND THRESHOLD
68+
gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
69+
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
70+
thresh = cv2.threshold(blurred, 100, 255, cv2.THRESH_BINARY)[1]
71+
72+
cnts = cv2.findContours(thresh, 1, 2)
73+
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
74+
75+
76+
#DETECTING COLOUR
77+
78+
# define range of green color in HSV
79+
lower = np.array([50, 50, 120])
80+
upper = np.array([70, 255, 255])
81+
shapemask = cv2.inRange(hsv, lower, upper)
82+
83+
# define range of blue color in HSV
84+
lower_blue = np.array([110, 50, 50])
85+
upper_blue = np.array([130, 255, 255])
86+
blue_mask = cv2.inRange(hsv, lower_blue, upper_blue)
87+
88+
# define range of red color in HSV
89+
lower_red = np.array([0, 50, 50])
90+
upper_red = np.array([10, 255, 255])
91+
red_mask = cv2.inRange(hsv, lower_red, upper_red)
92+
93+
hsv, cnts, _ = cv2.findContours(shapemask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
94+
hsv, cnts1, _ = cv2.findContours(blue_mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
95+
hsv, cnts2, _ = cv2.findContours(red_mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
96+
97+
ll = [path]
98+
for c in cnts:
99+
M = cv2.moments(c)
100+
cgX = int((M["m10"] / M["m00"]))
101+
cgY = int((M["m01"] / M["m00"]))
102+
cv2.putText(image, '('+str(cgX)+','+str(cgY)+')', (cgX-25, cgY+15), cv2.FONT_HERSHEY_SIMPLEX,0.5, (255, 255, 255), 1)
103+
cv2.putText(image, 'GREEN', (cgX - 20, cgY + 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
104+
detectShape(image, c, cgX, cgY, 'GREEN', ll)
105+
for c in cnts1:
106+
M = cv2.moments(c)
107+
cbX = int((M["m10"] / M["m00"]))
108+
cbY = int((M["m01"] / M["m00"]))
109+
cv2.putText(image, '('+str(cbX)+','+str(cbY)+')', (cbX-25, cbY+15), cv2.FONT_HERSHEY_SIMPLEX,0.5, (255, 255, 255), 1)
110+
cv2.putText(image, 'BLUE', (cbX - 20, cbY + 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
111+
detectShape(image, c, cbX, cbY, 'BLUE', ll)
112+
113+
for c in cnts2:
114+
M = cv2.moments(c)
115+
crX = int((M["m10"] / M["m00"]))
116+
crY = int((M["m01"] / M["m00"]))
117+
font = cv2.FONT_HERSHEY_SIMPLEX
118+
cv2.putText(image, '('+str(crX)+','+str(crY)+')', (crX-25, crY+15), cv2.FONT_HERSHEY_SIMPLEX,0.5, (255, 255, 255), 1)
119+
cv2.putText(image, 'RED', (crX - 25, crY + 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
120+
detectShape(image, c, crX, crY, 'RED', ll)
121+
122+
cv2.imshow("image", image)
123+
cv2.imwrite( path[0:len(path)-4]+"output.png", image );
124+
cv2.waitKey(0)
125+
return ll
126+
if __name__ == "__main__":
127+
mypath = '.'
128+
129+
onlyfiles = [join(mypath, f) for f in os.listdir(mypath) if f.endswith(".png")]
130+
131+
for fp in onlyfiles:
132+
133+
filep = open('outputimage.csv','a')
134+
135+
filep.write(fp)
136+
137+
filep.close()
138+
139+
data = main(fp)
140+
print data
141+
142+
filep = open('outputimage.csv','a')
143+
144+
filep.write('\n')
145+
146+
filep.close()

‎OpenCv/input/Sample1.png‎

19.2 KB
Loading[フレーム]

‎OpenCv/input/circle.png‎

9.67 KB
Loading[フレーム]

‎OpenCv/input/hexagon.png‎

9.11 KB
Loading[フレーム]

‎OpenCv/input/pentagon.png‎

6.99 KB
Loading[フレーム]

‎OpenCv/input/rhombus.png‎

7.63 KB
Loading[フレーム]

‎OpenCv/input/test1.png‎

13.3 KB
Loading[フレーム]

‎OpenCv/input/test2.png‎

13.4 KB
Loading[フレーム]

‎OpenCv/input/test3.png‎

15.8 KB
Loading[フレーム]

‎OpenCv/input/test4.png‎

15.5 KB
Loading[フレーム]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /