-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_processing_manual.py
241 lines (220 loc) · 9.05 KB
/
image_processing_manual.py
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env python
#%%
import os
import cv2
import csv
import numpy as np
import matplotlib.pyplot as plt
#%%
# Shows image in pop up without crashing jupyter
def showim(img):
res = cv2.resize(img, None, fx=0.75, fy=0.75, interpolation = cv2.INTER_CUBIC)
cv2.imshow('image',res)
k = cv2.waitKey(0) & 0xFF
if k == 27: # wait for ESC key to exit
cv2.destroyAllWindows()
def plotim(idx,calib_vis=False,testpt=None):
img = cv2.cvtColor(cv2.imread(img_dir + str(imgs[idx]) + '.jpg'), cv2.COLOR_BGR2RGB)
fig = plt.figure(figsize=(14, 12))
ax = fig.add_subplot(autoscale_on=False)
ax.set_aspect('equal')
ax.minorticks_on()
ax.grid(which='both')
ax.set_xlim(0,img.shape[1])
ax.set_ylim(0,img.shape[0])
ax.imshow(img)
ax.scatter(EE_start_px[0],EE_start_px[1],s=5,c='red',zorder=2.5)
if calib_vis:
marked_UV = P@marked_XYZ
marked_UV = marked_UV/marked_UV[2]
ax.scatter(marked_UV[0,:],marked_UV[1,:],s=5,c='yellow',zorder=2.5)
# Robot base links
base_links = [email protected]([[0,0,0,1],[0,0,0.333,1]]).T #,[0,-0.15,0.333,1],[0,0.15,0.333,1] # Joint2 axis if Joint=0
base_links = base_links/base_links[2,:].reshape(1,-1)
ax.plot(base_links[0,:],base_links[1,:],lw=3,c='slategrey')
if testpt is not None:
ax.scatter(testpt[0],testpt[1],s=5,c='yellow',zorder=2.5)
print(imgs[idx])
# Marked points for calibration visualation
marked_XYZ = np.array([
[0.154,0.149,0.0,1.0], # FR3 TERI base
[0.154,-0.150,0.0,1.0], # FR3 TERI base
[-0.238,0.149,0.0,1.0], # FR3 TERI base
[-0.238,-0.149,0.0,1.0], # FR3 TERI base
[0.154,0.149,-0.03,1.0], # FR3 TERI base
[0.154,-0.150,-0.03,1.0], # FR3 TERI base
[-0.238,0.149,-0.03,1.0], # FR3 TERI base
[-0.238,-0.149,-0.03,1.0], # FR3 TERI base
[0.055,0.0,0.14,1.0], # FR3 link0 arrow
[0.0715,0.0,0.00135,1.0], # FR3 link0 front edge
]).T
# Pixel to 3D conversions
def UV_to_XZplane(u,v,Y=0):
rhs1 = np.hstack([P[:,:3],np.array([[-u,-v,-1]]).T])
rhs1 = np.vstack([rhs1, np.array([0,1,0,0])]) # Intersect y=Y plane
rhs2 = np.reshape(np.hstack([-P[:,3],[Y]]),(4,1))
sol = np.linalg.inv(rhs1)@rhs2
return sol[:3]
#%%
# Paths
dataset_name = 'orange_full_range_centered'
data_date = '0830-orientation_expt'
data_dir = os.getcwd() + '/paramID_data/' + data_date + '/' + dataset_name
print('Dataset: ' + dataset_name)
print('Date: ' + data_date)
print('Path: ' + data_dir)
#%%
# Camera intrinsic and extrinsic transforms
with np.load(data_dir + '/../TFs_adj.npz') as tfs:
P = tfs['P']
E_base = tfs['E_base']
E_cam = tfs['E_cam']
K_cam = tfs['K_cam']
#%%
# Get img list and display first with grid
img_dir = data_dir + '/images/'
imgs = np.loadtxt(data_dir + '/sequence_experiment.csv', dtype=np.ulonglong, delimiter=',', skiprows=1, usecols=0)
# Plot initial EE location to check camera calibration
EE_start_XYZ = np.loadtxt(data_dir + '/sequence_experiment.csv', delimiter=',', skiprows=1, max_rows=1, usecols=range(1,4))
EE_start_px = [email protected]([EE_start_XYZ,1])
EE_start_px = EE_start_px/EE_start_px[2]
plotim(0,True)
#%%
# Set Y positions of markers
base_Y = EE_start_XYZ[1] - 0.015
mid_Y = EE_start_XYZ[1] - 0.0075
end_Y = EE_start_XYZ[1] - 0.0125
print("Assuming base at Y=" + str(base_Y))
print("Assuming mid at Y=" + str(mid_Y))
print("Assuming end at Y=" + str(end_Y))
print("DID YOU SET THE MARKER POSITIONS AT THE RIGHT RADII????")
#%%
import matplotlib
matplotlib.use('TkAgg')
ts = np.loadtxt(data_dir + '/sequence_experiment.csv', dtype=np.ulonglong, delimiter=',', skiprows=1, usecols=0)
data = np.loadtxt(data_dir + '/sequence_experiment.csv', delimiter=',', skiprows=1)
X_EE = data[:,1]
Y_EE = data[:,2]
Z_EE = data[:,3]
Phi = data[:,4]
Goal_X = data[:,5]
Goal_Z = data[:,6]
# Goal_Phi = data[:,7] # For endpoint orientation experiment
Endpt_Sol_X = data[:,7] # For endpoint grid experiment
Endpt_Sol_Z = data[:,8] # For endpoint grid experiment
X_base_meas = []
Z_base_meas = []
X_mid_meas = []
Z_mid_meas = []
X_end_meas = []
Z_end_meas = []
U_base = []
V_base = []
U_mid = []
V_mid = []
U_end = []
V_end = []
U_ang_start = []
V_ang_start = []
U_ang_end = []
V_ang_end = []
X_ang_start = []
Z_ang_start = []
X_ang_end = []
Z_ang_end = []
Base_angle = []
for n in range(len(ts)):
# Display current image
fig, ax = plt.subplots(figsize=(12, 9))
img = cv2.cvtColor(cv2.imread(img_dir + str(imgs[n]) + '.jpg'), cv2.COLOR_BGR2RGB)
ax.imshow(img)
# Plot current EE location
EEpx = [email protected]([X_EE[n],Y_EE[n],Z_EE[n],1]).T
EEpx = EEpx/EEpx[2]
ax.scatter(EEpx[0],EEpx[1],s=5,c='red',zorder=2.5)
# Plot current goal location
goalpx = [email protected]([Goal_X[n],Y_EE[n],Goal_Z[n],1]).T
goalpx = goalpx/goalpx[2]
ax.scatter(goalpx[0],goalpx[1],marker='x',c='limegreen',zorder=2.5)
# Plot current model endpt location
modelpx = [email protected]([Endpt_Sol_X[n],Y_EE[n],np.max([Endpt_Sol_Z[n],0.0]),1]).T # clamp Z to 0 to use as reference if below bench
modelpx = modelpx/modelpx[2]
ax.scatter(modelpx[0],modelpx[1],s=5,c='yellow',zorder=2.5)
# Plot Z=0 in XZ plane
z0px_L = [email protected]([-1.0,Y_EE[n],0,1]).T
z0px_L = z0px_L/z0px_L[2]
z0px_R = [email protected]([1.0,Y_EE[n],0,1]).T
z0px_R = z0px_R/z0px_R[2]
plt.plot([z0px_L[0],z0px_R[0]],[z0px_L[1],z0px_R[1]],c='slategrey')
# plt.axis('off')
plt.xlim(-100,2020)
plt.ylim(1080,-100)
plt.tight_layout()
# Get user input for marker locations
UVs = plt.ginput(n=-1, timeout=0)
plt.close()
# Convert chosen pts to XZ plane
base_UV = [int(UVs[0][0]), int(UVs[0][1])]
mid_UV = [int(UVs[1][0]), int(UVs[1][1])]
end_UV = [int(UVs[2][0]), int(UVs[2][1])]
base_XZ = UV_to_XZplane(base_UV[0], base_UV[1], base_Y)
mid_XZ = UV_to_XZplane(mid_UV[0], mid_UV[1], mid_Y)
end_XZ = UV_to_XZplane(end_UV[0], end_UV[1], end_Y)
# Determine base angle if applicable # TODO - it's actually the tip angle (alpha). But I already saved all the data as base angle.
if len(UVs) > 3:
base_ang_start_UV = [int(UVs[3][0]), int(UVs[3][1])]
base_ang_end_UV = [int(UVs[4][0]), int(UVs[4][1])]
base_ang_start_XZ = UV_to_XZplane(base_ang_start_UV[0], base_ang_start_UV[1], end_Y)
base_ang_end_XZ = UV_to_XZplane(base_ang_end_UV[0], base_ang_end_UV[1], end_Y)
base_ang = np.arctan2(-(base_ang_end_XZ[0]-base_ang_start_XZ[0]),-(base_ang_end_XZ[2]-base_ang_start_XZ[2])) # atan2(-delX,-delZ) because of robot axis shenanigans
else:
base_ang_start_UV = np.array([[0.0],[0.0]])
base_ang_end_UV = np.array([[0.0],[0.0]])
base_ang_start_XZ = np.array([[0.0],[0.0],[0.0]])
base_ang_end_XZ = np.array([[0.0],[0.0],[0.0]])
base_ang = 0.0
X_base_meas.append(base_XZ[0,0])
Z_base_meas.append(base_XZ[2,0])
X_mid_meas.append(mid_XZ[0,0])
Z_mid_meas.append(mid_XZ[2,0])
X_end_meas.append(end_XZ[0,0])
Z_end_meas.append(end_XZ[2,0])
U_base.append(base_UV[0])
V_base.append(base_UV[1])
U_mid.append(mid_UV[0])
V_mid.append(mid_UV[1])
U_end.append(end_UV[0])
V_end.append(end_UV[1])
U_ang_start.append(base_ang_start_UV[0])
V_ang_start.append(base_ang_start_UV[1])
U_ang_end.append(base_ang_end_UV[0])
V_ang_end.append(base_ang_end_UV[1])
X_ang_start.append(base_ang_start_XZ[0,0])
Z_ang_start.append(base_ang_start_XZ[2,0])
X_ang_end.append(base_ang_end_XZ[0,0])
Z_ang_end.append(base_ang_end_XZ[2,0])
Base_angle.append(base_ang)
with open(data_dir + '/sequence_results.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=',')
writer.writerow(['ts', 'X_EE', 'Y_EE', 'Z_EE', 'Phi',
'X_base_meas', 'Z_base_meas', 'X_mid_meas', 'Z_mid_meas', 'X_end_meas', 'Z_end_meas',
'U_base', 'V_base', 'U_mid', 'V_mid', 'U_end', 'V_end',
'Base_angle',
'X_ang_start', 'Z_ang_start', 'X_ang_end', 'Z_ang_end',
'U_ang_start', 'V_ang_start', 'U_ang_end', 'V_ang_end',
'Goal_X', 'Goal_Z',
# 'Goal_Phi',
'Endpt_Sol_X', 'Endpt_Sol_Z'
])
for n in range(len(ts)):
writer.writerow([imgs[n], X_EE[n], Y_EE[n], Z_EE[n], Phi[n],
X_base_meas[n], Z_base_meas[n], X_mid_meas[n], Z_mid_meas[n], X_end_meas[n], Z_end_meas[n],
U_base[n], V_base[n], U_mid[n], V_mid[n], U_end[n], V_end[n],
float(Base_angle[n]),
X_ang_start[n], Z_ang_start[n], X_ang_end[n], Z_ang_end[n],
U_ang_start[n], V_ang_start[n], U_ang_end[n], V_ang_end[n],
Goal_X[n], Goal_Z[n],
# Goal_Phi[n],
Endpt_Sol_X[n], Endpt_Sol_Z[n]
])
# %%