Repository navigation
Expand file tree
/
Copy pathseparate_array.py
More file actions
186 lines (158 loc) · 8.91 KB
/
Copy pathseparate_array.py
File metadata and controls
186 lines (158 loc) · 8.91 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
186
import cv2 as cv
import numpy as np
import math
class separate_array:
def __init__(self,array):
self.array = array
self.posMaxR = []
self.posMinR = []
self.arrCircles = []
self.arrHoles = []
self.coordinatesC = []
self.coordinatesCA = []
def separate(self):
self.maxRadius = max(self.array[:,2])
self.minRadius = max(self.array[:,2])-0.3
self.posMaxR = np.where(self.array[:,2] >= self.maxRadius)
self.posMinR = np.where(self.array[:,2] <= self.minRadius)
self.arrCircles = []
self.arrHoles = []
for self.i1 in self.posMaxR[0]:
self.arrCircles = np.concatenate((self.arrCircles, self.array[self.i1])) #Vector output
for self.i2 in self.posMinR[0]:
self.arrHoles = np.concatenate((self.arrHoles, self.array[self.i2]))
#Convert to a np array
self.arrCircles = np.array(self.arrCircles).reshape(self.posMaxR[0].shape[0],4)
self.arrHoles = np.array(self.arrHoles).reshape(self.posMinR[0].shape[0],4)
return self.arrCircles,self.arrHoles
def find_coordinates(self,array1,array2, cimg):
#Find the index of the reference circles in the array
self.posRef0 = np.where(array1[:,3] == 0)
self.posRef1 = np.where(array1[:,3] == 1)
self.posRef2 = np.where(array1[:,3] == 2)
#Find the coordinates
if len(self.posRef0[0]) == 1:
self.coord0x = array1[self.posRef0[0][0]][0]
self.coord0y = array1[self.posRef0[0][0]][1]
if len(self.posRef1[0]) == 1:
self.coord1x = array1[self.posRef1[0][0]][0]
self.coord1y = array1[self.posRef1[0][0]][1]
#cv.line(cimg,(int(self.coord0x),int(self.coord0y)),(int(self.coord1x),int(self.coord1y)),(255,255,0),2)
if len(self.posRef2[0]) == 1:
self.coord2x = array1[self.posRef2[0][0]][0]
self.coord2y = array1[self.posRef2[0][0]][1]
#cv.line(cimg,(int(self.coord0x),int(self.coord0y)),(int(self.coord2x),int(self.coord2y)),(255,255,0),2)
#Find the Hole coordinates
self.posHole = []
for self.i3 in np.arange(len(array2)+1):
self.position = np.where(array2[:,3] == self.i3)[0]
if len(self.position) == 0:
self.position = np.array([-1])
self.posHole = np.concatenate((self.posHole,self.position))
if self.posHole[0] >= 0 and self.posHole[1] >= 0 and self.posHole[2] >= 0 and self.posHole[3]>=0:
self.x0 = array2[int(self.posHole[0])][0]; self.y0 = array2[int(self.posHole[0])][1]
self.x1 = array2[int(self.posHole[1])][0]; self.y1 = array2[int(self.posHole[1])][1]
self.x2 = array2[int(self.posHole[2])][0]; self.y2 = array2[int(self.posHole[2])][1]
self.x3 = array2[int(self.posHole[3])][0]; self.y3 = array2[int(self.posHole[3])][1]
self.thetaR = math.atan((self.y1-self.y0)/(self.x0-self.x1))
self.thetaG = math.degrees(self.thetaR)
#Hole in quadrant 3 not found
elif self.posHole[0] >= 0:
self.x0 = array2[int(self.posHole[0])][0]; self.y0 = array2[int(self.posHole[0])][1]
if self.posHole[1] >= 0 and self.posHole[2] >= 0:
self.x1 = array2[int(self.posHole[1])][0]; self.y1 = array2[int(self.posHole[1])][1]
self.x2 = array2[int(self.posHole[2])][0]; self.y2 = array2[int(self.posHole[2])][1]
self.ca = self.x0-self.x1
self.co = self.y1-self.y0
self.thetaR = math.atan((self.co)/(self.ca))
self.thetaG = math.degrees(self.thetaR)
self.h = math.sqrt((self.ca**2)+(self.co**2))
if self.thetaG > 0:
self.x3 = self.x1+(self.h*math.sin(self.thetaR))
print('positivo')
else:
self.x3 = self.x1+(self.h*math.sin(self.thetaR))
print('negativo')
self.y3 = self.y1+self.h
#Holes in quadrant 1 and 3 not found
elif self.posHole[2] >= 0:
self.x2 = array2[int(self.posHole[2])][0]; self.y2 = array2[int(self.posHole[2])][1]
self.ca = self.y2 - self.y0
self.co = self.x2 - self.x0
self.thetaR = math.atan((self.co)/(self.ca))
self.thetaG = math.degrees(self.thetaR)
self.h = math.sqrt((self.ca**2)+(self.co**2))
self.x1 = self.x0 - self.h
if self.thetaG > 0:
self.y1 = self.y0 + (self.h*math.sin(self.thetaR))
self.x3 = self.x1 + (self.h*math.sin(self.thetaR))
else:
self.y1 = self.y0 + (self.h*math.sin(self.thetaR))
self.x3 = self.x1 + (self.h*math.sin(self.thetaR))
self.y3 = self.y1 + self.h
elif self.posHole[1] >= 0:
self.x1 = array2[int(self.posHole[1])][0]; self.y1 = array2[int(self.posHole[1])][1]
self.ca = self.x0-self.x1
self.co = self.y1-self.y0
self.thetaR = math.atan((self.co)/(self.ca))
self.thetaG = math.degrees(self.thetaR)
self.h = math.sqrt((self.ca**2)+(self.co**2))
if self.thetaG > 0:
self.x3 = self.x1+(self.h*math.sin(self.thetaR))
self.x2 = self.x3 + self.h
print('positivo')
else:
self.x3 = self.x1+(self.h*math.sin(self.thetaR))
self.x2 = self.x3 + self.h
print('negativo')
self.y3 = self.y1+self.h
self.y2 = self.y3 - (self.h*math.sin(self.thetaR))
elif self.posHole[1] >= 0:
self.x1 = array2[int(self.posHole[1])][0]; self.y1 = array2[int(self.posHole[1])][1]
if self.posHole[3] >= 0 and self.posHole[2] >= 0 :
self.x3 = array2[int(self.posHole[3])][0]; y3 = array2[int(self.posHole[3])][1]
self.x2 = array2[int(self.posHole[2])][0]; y2 = array2[int(self.posHole[2])][1]
self.ca = self.y3 - self.y1
self.co = self.x3 - self.x1
self.thetaR = math.atan((self.co)/(self.ca))
self.thetaG = math.degrees(self.thetaR)
self.h = math.sqrt((self.ca**2)+(self.co**2))
if self.thetaG > 0:
self.y0 = self.y1-(self.h*math.sin(self.thetaR))
else:
self.y0 = self.y1+(self.h*math.sin(self.thetaR))
self.x0 = self.x1 + self.h
elif self.posHole[3] >= 0:
self.x3 = array2[int(self.posHole[3])][0]; self.y3 = array2[int(self.posHole[3])][1]
self.ca = self.y3 - self.y1
self.co = self.x3 - self.x1
self.thetaR = math.atan((self.co)/(self.ca))
self.thetaG = math.degrees(self.thetaR)
self.h = math.sqrt((self.ca**2)+(self.co**2))
if self.thetaG > 0:
self.y0 = self.y1 + (self.h*math.sin(self.thetaR))
self.y2 = self.y3 + (self.h*math.sin(self.thetaR))
else:
self.y0 = self.y1 + (self.h*math.sin(self.thetaR))
self.y2 = self.y3 + (self.h*math.sin(self.thetaR))
self.x0 = self.x1 + self.h
self.x2 = self.x3 + self.h
#set scale
self.d01px = abs(self.coord0x - self.coord1x)
self.d02px = abs(self.coord0y - self.coord2y)
self.media = (self.d01px+self.d02px)/2
#conversion from px to mm
self.ratio = (180/self.media)#d01px
#conversion verification from circles reference
self.d02mm = self.d02px*self.ratio
#print('distance from C0y to C2y:', d02mm)
#conversion verification from holes
self.d02pxh = self.y2-self.y0
self.comp = (self.d02pxh * self.ratio)-130
self.d01mmh = (abs(self.x0-self.x1) * self.ratio)-self.comp
cv.line(cimg,(int(self.coord0x),int(self.coord0y)),(int(self.coord2x),int(self.coord2y)),(255,255,0),2)
cv.line(cimg,(int(self.coord0x),int(self.coord0y)),(int(self.coord1x),int(self.coord1y)),(255,255,0),2)
cv.line(cimg,(int(self.x0),int(self.y0)),(int(self.x1),int(self.y1)),(255,0,0),2)
cv.line(cimg,(int(self.x1),int(self.y1)),(int(self.x3),int(self.y3)),(255,0,0),2)
cv.line(cimg,(int(self.x3),int(self.y3)),(int(self.x2),int(self.y2)),(255,0,0),2)
cv.line(cimg,(int(self.x2),int(self.y2)),(int(self.x0),int(self.y0)),(255,0,0),2)