-
Notifications
You must be signed in to change notification settings - Fork 40
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
Eigen-based PnP Cost Function and Unit Test #54
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d75fa92
Added Eigen-based camera point projection method
marip8 9708f3f
Updated PNP optimization to use Eigen objects
marip8 cb30029
Added unit test for 2D PnP optimization
marip8 c551427
Moved PnP optimization out of experimental folder
marip8 c6d2a62
Improved clarity of camera projection function
marip8 ec9ab4e
Fixed bug in transformation math
marip8 47ad8d6
Centered camera over target
marip8 d17c6ee
Updated to use an auto-diff local parameterization
marip8 ae14cce
Merge branch 'master' into feature/pnp_utest
marip8 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
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
76 changes: 76 additions & 0 deletions
76
rct_optimizations/include/rct_optimizations/local_parameterization.h
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#pragma once | ||
|
||
#include <ceres/rotation.h> | ||
#include <Eigen/Dense> | ||
|
||
// Ceres Solver - A fast non-linear least squares minimizer | ||
// Copyright 2015 Google Inc. All rights reserved. | ||
// http://ceres-solver.org/ | ||
// | ||
// Redistribution and use in source and binary forms, with or without | ||
// modification, are permitted provided that the following conditions are met: | ||
// | ||
// * Redistributions of source code must retain the above copyright notice, | ||
// this list of conditions and the following disclaimer. | ||
// * Redistributions in binary form must reproduce the above copyright notice, | ||
// this list of conditions and the following disclaimer in the documentation | ||
// and/or other materials provided with the distribution. | ||
// * Neither the name of Google Inc. nor the names of its contributors may be | ||
// used to endorse or promote products derived from this software without | ||
// specific prior written permission. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
// POSSIBILITY OF SUCH DAMAGE. | ||
// | ||
// Author: sameeragarwal@google.com (Sameer Agarwal) | ||
|
||
namespace rct_optimizations | ||
{ | ||
/** | ||
* @brief Local parameterization of an Eigen quaternion | ||
* | ||
* As of version 1.12.0, Ceres does not provide implementations of auto-diff local parameterizations out of the box | ||
* However, Ceres uses them in unit its own unit tests. This struct is adapted directly from the following Ceres unit test: | ||
* | ||
* https://ceres-solver.googlesource.com/ceres-solver/+/master/internal/ceres/local_parameterization_test.cc#345 | ||
* blob: 636cac29a78648e823758e154681583cd81d5b25 | ||
* | ||
*/ | ||
struct EigenQuaternionPlus | ||
{ | ||
template<typename T> | ||
bool operator()(const T *x, const T *delta, T *x_plus_delta) const | ||
{ | ||
const T norm_delta = sqrt(delta[0] * delta[0] + delta[1] * delta[1] + delta[2] * delta[2]); | ||
Eigen::Quaternion<T> q_delta; | ||
if (norm_delta > T(0.0)) | ||
{ | ||
const T sin_delta_by_delta = sin(norm_delta) / norm_delta; | ||
q_delta.coeffs() << sin_delta_by_delta * delta[0], sin_delta_by_delta * delta[1], | ||
sin_delta_by_delta * delta[2], cos(norm_delta); | ||
} | ||
else | ||
{ | ||
// We do not just use q_delta = [0,0,0,1] here because that is a | ||
// constant and when used for automatic differentiation will | ||
// lead to a zero derivative. Instead we take a first order | ||
// approximation and evaluate it at zero. | ||
q_delta.coeffs() << delta[0], delta[1], delta[2], T(1.0); | ||
} | ||
Eigen::Map<Eigen::Quaternion<T>> x_plus_delta_ref(x_plus_delta); | ||
Eigen::Map<const Eigen::Quaternion<T>> x_ref(x); | ||
x_plus_delta_ref = q_delta * x_ref; | ||
return true; | ||
} | ||
}; | ||
|
||
} // namespace rct_optimizations |
File renamed without changes.
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
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 |
---|---|---|
@@ -0,0 +1,110 @@ | ||
#include <gtest/gtest.h> | ||
#include <rct_optimizations/pnp.h> | ||
#include <rct_optimizations_tests/observation_creator.h> | ||
#include <rct_optimizations_tests/utilities.h> | ||
|
||
using namespace rct_optimizations; | ||
|
||
TEST(PNP_2D, PerfectInitialConditions) | ||
{ | ||
test::Camera camera = test::makeKinectCamera(); | ||
|
||
unsigned target_rows = 5; | ||
unsigned target_cols = 7; | ||
double spacing = 0.025; | ||
test::Target target(target_rows, target_cols, spacing); | ||
|
||
Eigen::Isometry3d camera_to_target(Eigen::Isometry3d::Identity()); | ||
double x = static_cast<double>(target_rows - 1) * spacing / 2.0; | ||
double y = static_cast<double>(target_cols - 1) * spacing / 2.0; | ||
camera_to_target.translate(Eigen::Vector3d(x, y, 1.0)); | ||
camera_to_target.rotate(Eigen::AngleAxisd(M_PI, Eigen::Vector3d::UnitX())); | ||
|
||
PnPProblem problem; | ||
problem.camera_to_target_guess = camera_to_target; | ||
problem.intr = camera.intr; | ||
EXPECT_NO_THROW(problem.correspondences = | ||
test::getCorrespondences(camera_to_target, Eigen::Isometry3d::Identity(), camera, target, true)); | ||
|
||
PnPResult result = optimize(problem); | ||
EXPECT_TRUE(result.converged); | ||
EXPECT_TRUE(result.camera_to_target.isApprox(camera_to_target)); | ||
EXPECT_LT(result.initial_cost_per_obs, 1.0e-15); | ||
EXPECT_LT(result.final_cost_per_obs, 1.0e-15); | ||
} | ||
|
||
TEST(PNP_2D, PerturbedInitialCondition) | ||
{ | ||
test::Camera camera = test::makeKinectCamera(); | ||
|
||
unsigned target_rows = 5; | ||
unsigned target_cols = 7; | ||
double spacing = 0.025; | ||
test::Target target(target_rows, target_cols, spacing); | ||
|
||
Eigen::Isometry3d camera_to_target(Eigen::Isometry3d::Identity()); | ||
double x = static_cast<double>(target_rows) * spacing / 2.0; | ||
double y = static_cast<double>(target_cols) * spacing / 2.0; | ||
camera_to_target.translate(Eigen::Vector3d(x, y, 1.0)); | ||
camera_to_target.rotate(Eigen::AngleAxisd(M_PI, Eigen::Vector3d::UnitX())); | ||
|
||
PnPProblem problem; | ||
problem.camera_to_target_guess = test::perturbPose(camera_to_target, 0.05, 0.05); | ||
problem.intr = camera.intr; | ||
problem.correspondences = test::getCorrespondences(camera_to_target, | ||
Eigen::Isometry3d::Identity(), | ||
camera, | ||
target, | ||
true); | ||
|
||
PnPResult result = optimize(problem); | ||
EXPECT_TRUE(result.converged); | ||
EXPECT_TRUE(result.camera_to_target.isApprox(camera_to_target, 1.0e-8)); | ||
EXPECT_LT(result.final_cost_per_obs, 1.0e-15); | ||
} | ||
|
||
TEST(PNP_2D, BadIntrinsicParameters) | ||
{ | ||
test::Camera camera = test::makeKinectCamera(); | ||
|
||
unsigned target_rows = 5; | ||
unsigned target_cols = 7; | ||
double spacing = 0.025; | ||
test::Target target(target_rows, target_cols, spacing); | ||
|
||
Eigen::Isometry3d camera_to_target(Eigen::Isometry3d::Identity()); | ||
double x = static_cast<double>(target_rows) * spacing / 2.0; | ||
double y = static_cast<double>(target_cols) * spacing / 2.0; | ||
camera_to_target.translate(Eigen::Vector3d(x, y, 1.0)); | ||
camera_to_target.rotate(Eigen::AngleAxisd(M_PI, Eigen::Vector3d::UnitX())); | ||
|
||
PnPProblem problem; | ||
problem.camera_to_target_guess = camera_to_target; | ||
problem.correspondences = test::getCorrespondences(camera_to_target, | ||
Eigen::Isometry3d::Identity(), | ||
camera, | ||
target, | ||
true); | ||
|
||
// Tweak the camera intrinsics such that we are optimizing with different values (+/- 1%) | ||
// than were used to generate the observations | ||
problem.intr = camera.intr; | ||
problem.intr.fx() *= 1.01; | ||
problem.intr.fy() *= 0.99; | ||
problem.intr.cx() *= 1.01; | ||
problem.intr.cy() *= 0.99; | ||
|
||
PnPResult result = optimize(problem); | ||
|
||
// The optimization should still converge by moving the camera further away from the target, | ||
// but the residual error and error in the resulting transform should be much higher | ||
EXPECT_TRUE(result.converged); | ||
EXPECT_FALSE(result.camera_to_target.isApprox(camera_to_target, 1.0e-3)); | ||
EXPECT_GT(result.final_cost_per_obs, 1.0e-3); | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} |
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.
Is it possible to map
target_t
directly to anEigen::Translation
instead of needing to go through anEigen::Matrix<T,3,1>
as an intermediary?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 thought. I'm not sure if the pointer maps the same way, but I can test it out
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 appears to be a bug with the way that Eigen maps translations from pointers into objects, so it doesn't compile. I think we'll have to leave it as a vector for now. Ultimately I would like to be able to pass the entire matrix to a local parameterization instead of two separate objects, but I haven't worked that out yet.