-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageProcessing.py
More file actions
191 lines (164 loc) · 6.92 KB
/
ImageProcessing.py
File metadata and controls
191 lines (164 loc) · 6.92 KB
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
import cv2
# This file process the colors from the live webcam
# The image processor will start with the green side facing the camera
# and yellow top, then move to the orange side, the blue, then red
# Then it will capture white with green on top and then
# Yellow with blue on top
class GetColors:
def __init__(self):
# Access the live video through the 0 or 1 channel
self.gSide, self.oSide, self.bSide = [], [], []
self.rSide, self.wSide, self.ySide = [], [], []
# Add the lines and the circles to the image
def addRfrnce (self):
size = self.imgShow.shape
# Gets the y and x coordinates of the center of that image
pxlY, pxlX = size[0] // 2, size[1] // 2
# Gets a reasonable side length for the square
# Gets the length of a single cubicle
sqrLnth = (pxlX + pxlY) // 2; cbclLnth = (sqrLnth // 3)
# Gets the coordinates for the square (for the cube)
pxlYStart, pxlXStart = pxlY - (sqrLnth // 2), pxlX - (sqrLnth // 2)
pxlYEnd, pxlXEnd = pxlY + (sqrLnth // 2), pxlX + (sqrLnth // 2)
# Makes a square, in which the user shows their cube
cv2.rectangle(self.imgShow, (pxlXStart, pxlYStart), (pxlXEnd, pxlYEnd), (0, 0, 0), 2)
# Has the coordinates of where the 9 cubicle are going to be
zeroCble, frstCble = (pxlX - cbclLnth, pxlY - cbclLnth), (pxlX, pxlY - cbclLnth)
scndCble, thrdCble = (pxlX + cbclLnth, pxlY - cbclLnth), (pxlX - cbclLnth, pxlY)
frthCble, ffthCbld = (pxlX, pxlY), (pxlX + cbclLnth, pxlY)
sxthCble, svthCble = (pxlX - cbclLnth, pxlY + cbclLnth), (pxlX, pxlY + cbclLnth)
egthCble = (pxlX + cbclLnth, pxlY + cbclLnth)
self.side = [zeroCble, frstCble, scndCble, thrdCble, frthCble]
self.side.extend([ffthCbld, sxthCble, svthCble, egthCble])
for i in self.side:
img = cv2.circle(self.imgShow, i, 15, (0, 0, 0), 2)
# Returns the image and the coordinates of the 9 circles
return img
# Defines the order of presenting the cube in front of camera here
def addText(self, fSide, uSide):
# Reminder that the y and x axis are switched for this
size = self.imgShow.shape
textF = "Faces the Camera: "+fSide
textU = "Faces Upwards: "+uSide
textKey = "Press 'p' to capture the next Side"
font = cv2.FONT_HERSHEY_SIMPLEX
self.imgShow = cv2.putText(self.imgShow, textF, (0,25), font, 1, (0,0,0),2)
self.imgShow = cv2.putText(self.imgShow, textU, (0, 75), font, 1, (0, 0, 0), 2)
self.imgShow = cv2.putText(self.imgShow, textKey,(0, size[0]-25) , font, 1, (0, 0, 0), 2)
# Takes the image and the coordinates as parameters
def getColors(self,img):
answer = []
# Gets the mean of five point within the circle for each point
# and adds it to the answer list
for i in self.side:
bluePnt=int(img[i[1], i[0]][0])
bluePnt+=(int(img[i[1], i[0]+10][0])+int(img[i[1]+10, i[0]][0]))
bluePnt+=(int(img[i[1], i[0]-10][0]) + int(img[i[1]-10, i[0]][0]))
greenPnt=int(img[i[1], i[0]][1])
greenPnt+=int(img[i[1], i[0]+10][1]) + int(img[i[1]+10, i[0]][1])
greenPnt+=int(img[i[1], i[0]-10][1]) + int(img[i[1]-10, i[0]][1])
redPnt=int(img[i[1], i[0]][2])
redPnt+=int(img[i[1], i[0]+10][2]) + int(img[i[1]+10, i[0]][2])
redPnt+=int(img[i[1], i[0]-10][2]) + int(img[i[1]-10, i[0]][2])
bluePnt, greenPnt, redPnt = bluePnt//5, greenPnt//5, redPnt//5
answer.append([bluePnt, greenPnt, redPnt])
return answer
def getAllColors(self):
self.colorList = []
capture = cv2.VideoCapture(0)
g, o, r, b, w, y = "Green", "Orange", "Red", "Blue", "White", "Yellow"
sidesList = [(g, y), (o, y), (b, y), (r, y), (w, g), (y, b)]
for i in sidesList:
condition = True
# Goes through frames (images) that make up a video
while condition:
# Gets the current frame as img for getting color
success, img = capture.read()
# Get another copy of the frame for showing
success, self.imgShow = capture.read()
# Gets the size of the image
self.addRfrnce()
self.imgShow = cv2.flip(self.imgShow,1)
self.addText(i[0], i[1])
cv2.imshow("Capture", self.imgShow)
# Press (P) to print the color
if cv2.waitKey(1) & 0xFF == ord("p"):
self.colorList.append(self.getColors(img))
condition = False
success, img = capture.read()
capture.release()
cv2.destroyAllWindows()
# If the user decides to capture again
# This function converts numbers into colors
# Note:The image processor will start with the green side facing the camera
# and yellow top, then move to the orange side, the blue, then red
# Then it will capture white with green on top and then
# Yellow with blue on top
def convertColors(self):
colorList = self.colorList
self.answerList = []
self.anchorList = [colorList[0][4], colorList[1][4]]
self.anchorList.extend([colorList[2][4], colorList[3][4]])
self.anchorList.extend([colorList[4][4], colorList[5][4]])
for i in colorList:
for n in i:
minDiff = (255, ())
for s in self.anchorList:
bDiff = abs(n[0]-s[0])
gDiff = abs(n[1]-s[1])
rDiff = abs(n[2]-s[2])
if bDiff+gDiff+rDiff<=minDiff[0]:
minDiff = bDiff+gDiff+rDiff, s
self.answerList.append(minDiff[1])
def convertList(self):
anchorList = self.anchorList
answerList = self.answerList
finalList = []
gAnchor, oAnchor, bAnchor=anchorList[0], anchorList[1], anchorList[2]
rAnchor, wAnchor, yAnchor=anchorList[3], anchorList[4], anchorList[5]
for i in answerList:
if i==gAnchor:
finalList.append("g")
if i==oAnchor:
finalList.append("o")
if i==bAnchor:
finalList.append("b")
if i==rAnchor:
finalList.append("r")
if i==wAnchor:
finalList.append("w")
if i==yAnchor:
finalList.append("y")
gSide,oSide,bSide=finalList[0:9],finalList[9:18],finalList[18:27]
rSide,wSide,ySide=finalList[27:36],finalList[36:45],finalList[45:54]
self.gSide, self.oSide, self.bSide = gSide, oSide, bSide
self.rSide, self.wSide, self.ySide = rSide, wSide, ySide
def getColor(self):
gSide, oSide, bSide = self.gSide, self.oSide, self.bSide
rSide, wSide, ySide = self.rSide, self.wSide, self.ySide
return (gSide, bSide, wSide, ySide, rSide, oSide)
# Can be used for testing
def getSide(self, side):
cSide = side
return (cSide[0]+" "+cSide[1]+" "+cSide[2]+"\n"+
cSide[3]+" "+cSide[4]+" "+cSide[5]+"\n"+
cSide[6]+" "+cSide[7]+" "+cSide[8]+"\n")
def printCube(self):
g,b = self.getSide(self.gSide),self.getSide(self.bSide)
r,o = self.getSide(self.rSide), self.getSide(self.oSide)
y,w = self.ySide, self.wSide
# This will print the blank lines
print (" "*5+"|"+y[0]+" "+y[1]+" "+y[2]+"\n"+
" "*5+"|"+y[3]+" "+y[4]+" "+y[5]+"\n"+
" "*5+"|"+y[6]+" "+y[7]+" "+y[8]+"\n"+
r[:5]+"|"+g[:5]+"|"+o[:5]+"|"+b[:5]+"|"+"\n"+
r[6:11]+"|"+g[6:11]+"|"+o[6:11]+"|"+b[6:11]+"|"+"\n"+
r[12:17]+"|"+g[12:17]+"|"+o[12:17]+"|"+b[12:17]+"|"+"\n"+
" "*5+"|"+w[0]+" "+w[1]+" "+w[2]+"\n"+
" "*5+"|"+w[3]+" "+w[4]+" "+w[5]+"\n"+
" "*5+"|"+w[6]+" "+w[7]+" "+w[8]+"\n")
def finalStep(self):
self.getAllColors()
self.convertColors()
self.convertList()
cubeProcess = GetColors()