Skip to content

Commit 29ff585

Browse files
committed
Save transM into CSV file. BA process changed
1 parent 7cb195e commit 29ff585

File tree

3 files changed

+156
-86
lines changed

3 files changed

+156
-86
lines changed

parallax/bundle_adjustmnet.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def __init__(self, model, file_path):
2424

2525
def _parse_csv(self):
2626
self.df = pd.read_csv(self.file_path)
27-
self._remove_duplicates()
27+
#self._remove_duplicates()
2828
self._average_3D_points()
2929

3030
self._set_camera_list()
@@ -47,7 +47,6 @@ def _set_observations(self):
4747
# Create a mapping from camera IDs to indices
4848
camera_id_to_index = {str(camera_id): idx for idx, camera_id in enumerate(self.list_cameras)}
4949

50-
5150
# Iterate through the DataFrame to collect observations
5251
for _, row in self.df.iterrows():
5352
cam0, pt0 = str(row['cam0']), row['pt0']
@@ -82,9 +81,13 @@ def _average_3D_points(self):
8281
# Merge the averaged columns back into the original DataFrame
8382
self.df = self.df.merge(grouped, on='ts_local_coords', how='left')
8483

84+
# Create a mapping of ts_local_coords to index in the averaged points
85+
self.df['point_index'] = self.df.groupby('ts_local_coords').ngroup()
86+
8587
# Write the updated DataFrame back to the CSV file
8688
self.df.to_csv(self.file_path, index=False)
8789

90+
8891
def _remove_duplicates(self):
8992
# Drop duplicate rows based on 'ts_local_coords', 'global_x', 'global_y', 'global_z' columns
9093
logger.debug(f"Original rows: {self.df.shape[0]}")
@@ -182,22 +185,31 @@ def optimize(self, print_result=True):
182185
self.opt_points = opt_params[12 * n_cams:].reshape(n_pts, 3)
183186

184187
if print_result:
185-
print(f"\nOptimization completed.")
188+
print(f"\n************** Optimization completed. **************************")
186189
# Compute initial residuals
187190
initial_residuals = self.residuals(initial_params)
188191
initial_residuals_sum = np.sum(initial_residuals**2)
189192
average_residual = initial_residuals_sum / len(self.bal_problem.observations)
190-
print(f"Before BA, Average residual: {average_residual}")
193+
print(f"** Before BA, Average residual of reproj: {np.round(average_residual, 2)} **")
191194

192195
# Compute Optimize residuals
193196
opt_residuals = self.residuals(opt_params)
194197
opt_residuals_sum = np.sum(opt_residuals**2)
195198
average_residual = opt_residuals_sum / len(self.bal_problem.observations)
196-
print(f"After BA, Average residual: {average_residual}")
199+
print(f"** After BA, Average residual of reproj: {np.round(average_residual, 2)} **")
200+
print(f"******************************************************************")
197201

198202
logger.debug(f"Optimized camera parameters: {self.opt_camera_params}")
199203

200204
for i in range(len(self.bal_problem.points)):
201205
logger.debug(f"\nPoint {i}")
202206
logger.debug(f"org : {self.bal_problem.points[i]}")
203-
logger.debug(f"opt : {self.opt_points[i]}")
207+
logger.debug(f"opt : {self.opt_points[i]}")
208+
209+
# Map optimized points to the original DataFrame rows
210+
opt_points_df = pd.DataFrame(self.opt_points, columns=['opt_x', 'opt_y', 'opt_z'])
211+
self.bal_problem.df = self.bal_problem.df.join(opt_points_df, on='point_index', rsuffix='_opt')
212+
213+
# Save the updated DataFrame to the CSV file
214+
self.bal_problem.df.to_csv(self.bal_problem.file_path, index=False)
215+
logger.info(f"Optimized points saved to {self.bal_problem.file_path}")

parallax/coords_transformation.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ def func(self, x, measured_pts, global_pts, reflect_z=False):
6464
def avg_error(self, x, measured_pts, global_pts, reflect_z=False):
6565
"""Calculates the total error for the optimization."""
6666
error_values = self.func(x, measured_pts, global_pts, reflect_z)
67-
ave_error = np.sum(error_values**2)/len(error_values)
68-
return ave_error
67+
mean_squared_error = np.mean(error_values**2)
68+
average_error = np.sqrt(mean_squared_error)
69+
return average_error
6970

7071
def fit_params(self, measured_pts, global_pts):
7172
"""Fits parameters to minimize the error defined in func"""
@@ -87,12 +88,14 @@ def fit_params(self, measured_pts, global_pts):
8788
if avg_error1 < avg_error2:
8889
rez = res1[0]
8990
R = self.combineAngles(rez[2], rez[1], rez[0], reflect_z=False)
91+
avg_err = avg_error1
9092
else:
9193
rez = res2[0]
9294
R = self.combineAngles(rez[2], rez[1], rez[0], reflect_z=True)
95+
avg_err = avg_error1
9396

9497
#origin = rez[3:]
9598
origin = rez[3:6]
9699
scale = rez[6:]
97100
#return origin, R # translation vector and rotation matrix
98-
return origin, R, scale # translation vector, rotation matrix, and scaling factors
101+
return origin, R, scale, avg_err # translation vector, rotation matrix, and scaling factors

0 commit comments

Comments
 (0)