Skip to content

Commit

Permalink
Merge pull request #1615 from alicevision/fix/undistortion
Browse files Browse the repository at this point in the history
fix sfm with undistortion
  • Loading branch information
cbentejac authored Dec 6, 2023
2 parents 89d0c26 + 19b5e41 commit 1cf4b12
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
7 changes: 6 additions & 1 deletion src/aliceVision/sfm/bundle/BundleAdjustmentCeres.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ ceres::CostFunction* createCostFunctionFromIntrinsics(const IntrinsicBase* intri
if (undistortion)
{
obsUndistorted.x = undistortion->undistort(observation.x);

if (intrinsicDistortionPtr->getDistortion() != nullptr)
{
throw std::runtime_error("Distortion should not be there when undistortion exists");
}
}
}

Expand All @@ -224,7 +229,7 @@ ceres::CostFunction* createCostFunctionFromIntrinsics(const IntrinsicBase* intri
return new ceres::AutoDiffCostFunction<ResidualErrorFunctor_Pinhole3DEClassicLD, 2, 9, 6, 3>(
new ResidualErrorFunctor_Pinhole3DEClassicLD(w, h, obsUndistorted));
case EINTRINSIC::PINHOLE_CAMERA_3DEANAMORPHIC4:
return new ceres::AutoDiffCostFunction<ResidualErrorFunctor_Pinhole, 2, 18, 6, 3>(new ResidualErrorFunctor_Pinhole(w, h, obsUndistorted));
return new ceres::AutoDiffCostFunction<ResidualErrorFunctor_Pinhole, 2, 4, 6, 3>(new ResidualErrorFunctor_Pinhole(w, h, obsUndistorted));
case EINTRINSIC::PINHOLE_CAMERA_BROWN:
return new ceres::AutoDiffCostFunction<ResidualErrorFunctor_PinholeBrownT2, 2, 9, 6, 3>(
new ResidualErrorFunctor_PinholeBrownT2(w, h, obsUndistorted));
Expand Down
4 changes: 4 additions & 0 deletions src/aliceVision/sfmDataIO/AlembicImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -621,11 +621,15 @@ bool readCamera(const Version& abcVersion,
{
distortion->setParameters(distortionParams);
}


std::shared_ptr<camera::Undistortion> undistortion = intrinsicCasted->getUndistortion();
if (undistortion)
{
undistortion->setParameters(undistortionParams);
undistortion->setOffset(undistortionOffset);
//If undistortion exists, distortion does not
intrinsicCasted->setDistortionObject(nullptr);
}
}

Expand Down
35 changes: 19 additions & 16 deletions src/aliceVision/sfmDataIO/jsonIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,22 +342,6 @@ void loadIntrinsic(const Version& version, IndexT& intrinsicId, std::shared_ptr<

intrinsicWithDistoEnabled->setDistortionInitializationMode(distortionInitializationMode);

std::shared_ptr<camera::Distortion> distortionObject = intrinsicWithDistoEnabled->getDistortion();
if (distortionObject)
{
std::vector<double> distortionParams;
for (bpt::ptree::value_type& paramNode : intrinsicTree.get_child("distortionParams"))
{
distortionParams.emplace_back(paramNode.second.get_value<double>());
}

// ensure that we have the right number of params
if (distortionParams.size() == distortionObject->getParameters().size())
{
distortionObject->setParameters(distortionParams);
}
}

std::shared_ptr<camera::Undistortion> undistortionObject = intrinsicWithDistoEnabled->getUndistortion();
if (undistortionObject)
{
Expand All @@ -375,6 +359,25 @@ void loadIntrinsic(const Version& version, IndexT& intrinsicId, std::shared_ptr<
loadMatrix("undistortionOffset", offset, intrinsicTree);
undistortionObject->setOffset(offset);
}

//If undistortion exists, distortion does not
intrinsicWithDistoEnabled->setDistortionObject(nullptr);
}

std::shared_ptr<camera::Distortion> distortionObject = intrinsicWithDistoEnabled->getDistortion();
if (distortionObject)
{
std::vector<double> distortionParams;
for (bpt::ptree::value_type& paramNode : intrinsicTree.get_child("distortionParams"))
{
distortionParams.emplace_back(paramNode.second.get_value<double>());
}

// ensure that we have the right number of params
if (distortionParams.size() == distortionObject->getParameters().size())
{
distortionObject->setParameters(distortionParams);
}
}
}

Expand Down

0 comments on commit 1cf4b12

Please sign in to comment.