-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackupcode.tt
173 lines (132 loc) · 7.04 KB
/
backupcode.tt
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
#The graphic in the app could just redraw
if(key==ord('w')):
self.Canny_Upper+=5
elif(key==ord('s')):
if(self.Canny_Upper-5<=self.Canny_Lower):
MakeSound()
else:
self.Canny_Upper-=5
elif(key==ord('a')):
if(self.Canny_Lower-5==0):
MakeSound()
else:
self.Canny_Lower-=5
elif(key==ord('d')):
if(self.Canny_Upper<=self.Canny_Lower+5):
MakeSound()
else:
self.Canny_Lower+=5
circles = cv2.HoughCircles(Canny,cv2.HOUGH_GRADIENT,1,100,
param1=50,param2=30)#,minRadius=0,maxRadius=0)
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
# draw the outer circle
cv2.circle(Colored,(i[0],i[1]),i[2],(0,255,0),2)
# draw the center of the circle
cv2.circle(Colored,(i[0],i[1]),2,(0,0,255),3)
def MakeSound():
for i in range(1,6):
sys.stdout.write('\r\a{i}'.format(i=i))
sys.stdout.flush()
time.sleep(1)
sys.stdout.write('\n')
print("x0 ",x0," center[0] ",center[0]," Radius ",Radius)
#self.GameFrame[i[1]-i[2]:i[1]+i[2],i[0]-i[2]:i[0]+i[2]].copy()
#Maybe Kmeans isn't needed
#Lag_A=10
#Lag_B=5
#
#InitialPts=[]
#InitialPts.append(self.GameFrame.shape[1]//13,self.GameFrame.shape[0]//13)
#InitialPts.append(self.GameFrame.shape[1] //Lag_B,self.GameFrame.shape[0]//Lag_A)
#InitialPts.append((Lag_B-1)*self.GameFrame.shape[1]//Lag_B,self.GameFrame.shape[0]//Lag_A)
#InitialPts.append(self.GameFrame.shape[1]//5,(Lag_A-1)*self.GameFrame.shape[0]//Lag_A)
#InitialPts.append((Lag_B-1)*self.GameFrame.shape[1]//5,(Lag_A-1)*self.GameFrame.shape[0]//Lag_A)
#InitialPts.append(round((0.70955882352*self.GameFrame.shape[1])//1),round((0.08816120906*self.GameFrame.shape[0])//1))
#Can be replaced by drawing a convex hull around them?
for i in range(len(contours)):
c=contours[i]
extLeft = tuple(c[c[:, :, 0].argmin()][0])
extRight = tuple(c[c[:, :, 0].argmax()][0])
extTop = tuple(c[c[:, :, 1].argmin()][0])
extBot = tuple(c[c[:, :, 1].argmax()][0])
if(i==0):
Left=extLeft
Right=extRight
Top=extTop
Bottom=extBot
else:
if(extLeft[0]<Left[0]):
Left=extLeft
if(extRight[0]>Right[0]):
Right=extRight
if(extTop[1]<Top[1]):
Top=extTop
if(extBot[1]>Bottom[1]):
Bottom=extBot
if(cv2.contourArea(c)>15):
Left = tuple(c[c[:, :, 0].argmin()][0])
Right = tuple(c[c[:, :, 0].argmax()][0])
Top = tuple(c[c[:, :, 1].argmin()][0])
Bottom = tuple(c[c[:, :, 1].argmax()][0])
Width=Bottom[1]-Top[1]
Length=Left[0]-Right[0]
if(Length/Width<=3 and Width/Length<=3):#Introduce area measures as well
_,_,w,h = cv2.boundingRect(c)
Area=w*h
if(cv2.contourArea(c)>250 or cv2.contourArea(c)/Area>0.45):
OverLap = np.zeros((self.Circles[i][3].shape[0],self.Circles[i][3].shape[1],3), np.uint8)
cv2.drawContours(OverLap,[c],-1,[255,255,255],cv2.FILLED);
OverLap=cv2.bitwise_and(OverLap,CircleImg)
contours1, hierarchy = cv2.findContours(self.getCannyImage(OverLap,5,10), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
Area=0
for cnts in contours1:
Area+=cv2.contourArea(cnts)
M = cv2.moments(c)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
Distances=[]
for Sublist in c:
Distances.append(self.getEuclidDist(Sublist[0],self.Circles[i][0]))
Smallest_Distance=sorted(Distances, key=lambda z: z)[0]
if(Smallest_Distance < self.Circles[i][1] and Area/cv2.contourArea(c)>0.25):
Contour_img=self.Circles[i][3][Top[1]:Bottom[1],Left[0]:Right[0]].copy()
Sum=[0,0,0]
No=0
for x in range(Top[1],Bottom[1]):
for j in range(Left[0],Right[0]):
if(cv2.pointPolygonTest(c,(j,x),0)>0):
Sum[0]+=Contour_img[x-Top[1]][j-Left[0]][0]
Sum[1]+=Contour_img[x-Top[1]][j-Left[0]][1]
Sum[2]+=Contour_img[x-Top[1]][j-Left[0]][2]
No+=1
cv2.drawContours(New_Img,[c],-1,[255,255,255],cv2.FILLED);
New_Img=cv2.bitwise_and(New_Img,self.Circles[i][3])
mean = [Sum[0]/No,Sum[1]/No,Sum[2]/No]
Color=self.findClosetColor(mean)
self.segments[i].append([c,Color])
# print("mean ", np.uint32(mean), " Color ",Color)
# cv2.imshow("Contour",Contour_img)
# cv2.imshow("New_Img",New_Img)
# cv2.moveWindow("Contour", 800, 150)
# cv2.waitKey(0)
cv2.imshow("New_Img",CircleImg)
self.Circles[i][3]=New_Img
Canny=self.getCannyImage(self.GameFrame)
contours, hierarchy = cv2.findContours(Canny, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
c = np.vstack(contours[i] for i in range(len(contours)) if( cv2.contourArea(contours[i])>20))
c=cv2.convexHull(c)
Canny=cv2.drawContours(Canny,[c],-1,255,5);#cv2.FILLED);
cv2.imshow("Canny Filled",Canny)
mat = np.argwhere(Canny != 0)
mat = np.array(mat).astype(np.float32) #have to convert type for PCA
m, e = cv2.PCACompute(mat, mean = np.array([]))
#now to draw: let's scale our primary axis by 100,
#and the secondary by 50
center = tuple(m[0])
endpoint1 = tuple(m[0] + e[0]*100)
endpoint2 = tuple(m[0] + e[1]*50)
cv2.circle(self.GameFrame, center, 5, [255,255,255])
cv2.line (self.GameFrame, center, endpoint1, [255,255,255])
cv2.line (self.GameFrame, center, endpoint2, [255,255,255])
print(self.GameFrame.shape)