-
Notifications
You must be signed in to change notification settings - Fork 767
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix triangulatePoint3 for calibrations with distortion #1128
Merged
dellaert
merged 3 commits into
borglab:develop
from
thomassm:fix/triangulatePoint3-Cal3DS2
Mar 15, 2022
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -227,6 +227,99 @@ std::vector<Matrix34, Eigen::aligned_allocator<Matrix34>> projectionMatricesFrom | |
return projection_matrices; | ||
} | ||
|
||
/** Create a pinhole calibration from a different Cal3 object, removing distortion. | ||
* | ||
* @tparam CALIBRATION Original calibration object. | ||
* @param cal Input calibration object. | ||
* @return Cal3_S2 with only the pinhole elements of cal. | ||
*/ | ||
template <class CALIBRATION> | ||
Cal3_S2 createPinholeCalibration(const CALIBRATION& cal) { | ||
const auto& K = cal.K(); | ||
return Cal3_S2(K(0,0), K(1,1), K(0,1), K(0,2), K(1,2)); | ||
} | ||
|
||
/** Internal undistortMeasurement to be used by undistortMeasurement and undistortMeasurements */ | ||
template <class CALIBRATION, class MEASUREMENT> | ||
MEASUREMENT undistortMeasurementInternal(const CALIBRATION& cal, | ||
const MEASUREMENT& measurement, | ||
boost::optional<Cal3_S2> pinholeCal = boost::none) { | ||
if (!pinholeCal) { | ||
pinholeCal = createPinholeCalibration(cal); | ||
} | ||
return pinholeCal->uncalibrate(cal.calibrate(measurement)); | ||
} | ||
|
||
/** Remove distortion for measurements so as if the measurements came from a pinhole camera. | ||
* | ||
* Removes distortion but maintains the K matrix of the initial cal. Operates by calibrating using | ||
* full calibration and uncalibrating with only the pinhole component of the calibration. | ||
* @tparam CALIBRATION Calibration type to use. | ||
* @param cal Calibration with which measurements were taken. | ||
* @param measurements Vector of measurements to undistort. | ||
* @return measurements with the effect of the distortion of sharedCal removed. | ||
*/ | ||
template <class CALIBRATION> | ||
Point2Vector undistortMeasurements(const CALIBRATION& cal, | ||
const Point2Vector& measurements) { | ||
Cal3_S2 pinholeCalibration = createPinholeCalibration(cal); | ||
Point2Vector undistortedMeasurements; | ||
// Calibrate with cal and uncalibrate with pinhole version of cal so that measurements are undistorted. | ||
std::transform(measurements.begin(), measurements.end(), std::back_inserter(undistortedMeasurements), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could we just use
|
||
[&cal, &pinholeCalibration](const Point2& measurement) { | ||
return undistortMeasurementInternal<CALIBRATION>(cal, measurement, pinholeCalibration); | ||
}); | ||
return undistortedMeasurements; | ||
} | ||
|
||
/** Specialization for Cal3_S2 as it doesn't need to be undistorted. */ | ||
template <> | ||
inline Point2Vector undistortMeasurements(const Cal3_S2& cal, | ||
const Point2Vector& measurements) { | ||
return measurements; | ||
} | ||
|
||
|
||
/** Remove distortion for measurements so as if the measurements came from a pinhole camera. | ||
* | ||
* Removes distortion but maintains the K matrix of the initial calibrations. Operates by calibrating using | ||
* full calibration and uncalibrating with only the pinhole component of the calibration. | ||
* @tparam CAMERA Camera type to use. | ||
* @param cameras Cameras corresponding to each measurement. | ||
* @param measurements Vector of measurements to undistort. | ||
* @return measurements with the effect of the distortion of the camera removed. | ||
*/ | ||
template <class CAMERA> | ||
typename CAMERA::MeasurementVector undistortMeasurements(const CameraSet<CAMERA>& cameras, | ||
const typename CAMERA::MeasurementVector& measurements) { | ||
|
||
const size_t num_meas = cameras.size(); | ||
assert(num_meas == measurements.size()); | ||
typename CAMERA::MeasurementVector undistortedMeasurements(num_meas); | ||
for (size_t ii = 0; ii < num_meas; ++ii) { | ||
// Calibrate with cal and uncalibrate with pinhole version of cal so that measurements are undistorted. | ||
undistortedMeasurements[ii] = | ||
undistortMeasurementInternal<typename CAMERA::CalibrationType>(cameras[ii].calibration(), measurements[ii]); | ||
} | ||
return undistortedMeasurements; | ||
} | ||
|
||
/** Specialize for Cal3_S2 to do nothing. */ | ||
template <class CAMERA = PinholeCamera<Cal3_S2>> | ||
inline PinholeCamera<Cal3_S2>::MeasurementVector undistortMeasurements( | ||
const CameraSet<PinholeCamera<Cal3_S2>>& cameras, | ||
const PinholeCamera<Cal3_S2>::MeasurementVector& measurements) { | ||
return measurements; | ||
} | ||
|
||
/** Specialize for SphericalCamera to do nothing. */ | ||
template <class CAMERA = SphericalCamera> | ||
inline SphericalCamera::MeasurementVector undistortMeasurements( | ||
const CameraSet<SphericalCamera>& cameras, | ||
const SphericalCamera::MeasurementVector& measurements) { | ||
return measurements; | ||
} | ||
|
||
/** | ||
* Function to triangulate 3D landmark point from an arbitrary number | ||
* of poses (at least 2) using the DLT. The function checks that the | ||
|
@@ -253,8 +346,11 @@ Point3 triangulatePoint3(const std::vector<Pose3>& poses, | |
// construct projection matrices from poses & calibration | ||
auto projection_matrices = projectionMatricesFromPoses(poses, sharedCal); | ||
|
||
// Undistort the measurements, leaving only the pinhole elements in effect. | ||
auto undistortedMeasurements = undistortMeasurements<CALIBRATION>(*sharedCal, measurements); | ||
|
||
// Triangulate linearly | ||
Point3 point = triangulateDLT(projection_matrices, measurements, rank_tol); | ||
Point3 point = triangulateDLT(projection_matrices, undistortedMeasurements, rank_tol); | ||
|
||
// Then refine using non-linear optimization | ||
if (optimize) | ||
|
@@ -300,7 +396,11 @@ Point3 triangulatePoint3( | |
|
||
// construct projection matrices from poses & calibration | ||
auto projection_matrices = projectionMatricesFromCameras(cameras); | ||
Point3 point = triangulateDLT(projection_matrices, measurements, rank_tol); | ||
|
||
// Undistort the measurements, leaving only the pinhole elements in effect. | ||
auto undistortedMeasurements = undistortMeasurements<CAMERA>(cameras, measurements); | ||
|
||
Point3 point = triangulateDLT(projection_matrices, undistortedMeasurements, rank_tol); | ||
|
||
// The n refine using non-linear optimization | ||
if (optimize) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch, fixed the numbering. I ran
clang-format --style=Google
for the new code, do you have a different formatter though? It made a lot of changes to the original code that I didn't include.