Skip to content

Commit

Permalink
Merge branch '24-fix-reopimiser'
Browse files Browse the repository at this point in the history
  • Loading branch information
MattClarkson committed Jul 29, 2020
2 parents 25d13ad + 50c5abd commit de22fee
Show file tree
Hide file tree
Showing 60 changed files with 366 additions and 134 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ nbsphinx
scipy
opencv-contrib-python
scikit-surgerycore
scikit-surgeryimage
scikit-surgeryimage>=0.7.4
scikit-surgeryopencvcpp
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
'scipy',
'opencv-contrib-python',
'scikit-surgerycore',
'scikit-surgeryimage',
'scikit-surgeryimage>=0.7.4',
'scikit-surgeryopencvcpp',
],

Expand Down
36 changes: 17 additions & 19 deletions sksurgerycalibration/video/video_calibration_cost_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def stereo_2d_error(x_0,
l2r_tvec
):
"""
Private method to RMSE cost function, in stereo, where x_0 contains
Private method to return vector of residuals, in stereo, where x_0 contains
the left camera extrinsics.
"""
rvecs = []
Expand All @@ -38,24 +38,22 @@ def stereo_2d_error(x_0,
rvecs.append(rvec)
tvecs.append(tvec)

tmp_sse, tmp_num = vm.compute_stereo_2d_err(l2r_rmat,
l2r_tvec,
common_object_points,
common_left_image_points,
left_intrinsics,
left_distortion,
common_object_points,
common_right_image_points,
right_intrinsics,
right_distortion,
rvecs,
tvecs
)

mse = tmp_sse / tmp_num
rmse = np.sqrt(mse)

return rmse
residual = vm.compute_stereo_2d_err(l2r_rmat,
l2r_tvec,
common_object_points,
common_left_image_points,
left_intrinsics,
left_distortion,
common_object_points,
common_right_image_points,
right_intrinsics,
right_distortion,
rvecs,
tvecs,
return_residuals=True
)

return residual


def stereo_2d_and_3d_error(x_0,
Expand Down
17 changes: 2 additions & 15 deletions sksurgerycalibration/video/video_calibration_driver_stereo.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,7 @@ def iterative_calibration(self,
reference_ids,
reference_image_points,
reference_image_size,
flags: int = cv2.CALIB_USE_INTRINSIC_GUESS,
override_left_intrinsics=None,
override_left_distortion=None,
override_right_intrinsics=None,
override_right_distortion=None,
override_l2r_rmat=None,
override_l2r_tvec=None
flags: int = cv2.CALIB_USE_INTRINSIC_GUESS
):
"""
Does iterative calibration, like Datta 2009.
Expand Down Expand Up @@ -203,14 +197,7 @@ def iterative_calibration(self,
reference_image_size)

proj_err, recon_err, param_copy = \
self.calibrate(flags,
override_left_intrinsics,
override_left_distortion,
override_right_intrinsics,
override_right_distortion,
override_l2r_rmat,
override_l2r_tvec
)
self.calibrate(flags)

LOGGER.info("Iterative calibration: %s: proj_err=%s, recon_err=%s.",
str(i), str(proj_err), str(recon_err))
Expand Down
31 changes: 20 additions & 11 deletions sksurgerycalibration/video/video_calibration_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ def compute_stereo_2d_err(l2r_rmat,
right_camera_matrix,
right_distortion,
left_rvecs,
left_tvecs):
left_tvecs,
return_residuals=False):
"""
Function to compute stereo SSE re-projection error, over multiple views.
Function to compute stereo re-projection error, over multiple views.
:param l2r_rmat: [3x3] ndarray, rotation for l2r transform
:param l2r_tvec: [3x1] ndarray, translation for l2r transform
Expand All @@ -42,12 +43,15 @@ def compute_stereo_2d_err(l2r_rmat,
:param right_distortion: [1x5] ndarray
:param left_rvecs: Vector of [3x1] ndarray, Rodrigues rotations, left camera
:param left_tvecs: Vector of [3x1] ndarray, translations, left camera
:return: SSE re-reprojection error, number_samples
:param return_residuals: if True returns vector of residuals for LM,
otherwise, returns SSE.
:return: re-reprojection error, number_samples
"""
left_to_right = mu.construct_rigid_transformation(l2r_rmat, l2r_tvec)

lse = 0
rse = 0
residuals = []
number_of_samples = 0
number_of_frames = len(left_object_points)

Expand All @@ -72,18 +76,23 @@ def compute_stereo_2d_err(l2r_rmat,
right_camera_matrix,
right_distortion)

diff_left = left_image_points[i] - projected_left

lse = lse + np.sum(np.square(diff_left))

diff_right = right_image_points[i] - projected_right
rse = rse + np.sum(np.square(diff_right))
number_of_samples = number_of_samples \
+ len(left_image_points[i]) \
+ len(right_image_points[i])

LOGGER.debug("Stereo RMS reprojection: left sse=%s, right sse=%s, num=%s",
str(lse), str(rse), str(number_of_samples))
diff_left = left_image_points[i] - projected_left
diff_right = right_image_points[i] - projected_right

if return_residuals:
residuals.append(diff_left.reshape((-1)))
residuals.append(diff_right.reshape((-1)))
else:
lse = lse + np.sum(np.square(diff_left))
rse = rse + np.sum(np.square(diff_right))

if return_residuals:
return np.hstack(residuals)

return lse + rse, number_of_samples


Expand Down

0 comments on commit de22fee

Please sign in to comment.