Skip to content

Commit 1cf4b12

Browse files
authored
Merge pull request #1615 from alicevision/fix/undistortion
fix sfm with undistortion
2 parents 89d0c26 + 19b5e41 commit 1cf4b12

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

src/aliceVision/sfm/bundle/BundleAdjustmentCeres.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ ceres::CostFunction* createCostFunctionFromIntrinsics(const IntrinsicBase* intri
204204
if (undistortion)
205205
{
206206
obsUndistorted.x = undistortion->undistort(observation.x);
207+
208+
if (intrinsicDistortionPtr->getDistortion() != nullptr)
209+
{
210+
throw std::runtime_error("Distortion should not be there when undistortion exists");
211+
}
207212
}
208213
}
209214

@@ -224,7 +229,7 @@ ceres::CostFunction* createCostFunctionFromIntrinsics(const IntrinsicBase* intri
224229
return new ceres::AutoDiffCostFunction<ResidualErrorFunctor_Pinhole3DEClassicLD, 2, 9, 6, 3>(
225230
new ResidualErrorFunctor_Pinhole3DEClassicLD(w, h, obsUndistorted));
226231
case EINTRINSIC::PINHOLE_CAMERA_3DEANAMORPHIC4:
227-
return new ceres::AutoDiffCostFunction<ResidualErrorFunctor_Pinhole, 2, 18, 6, 3>(new ResidualErrorFunctor_Pinhole(w, h, obsUndistorted));
232+
return new ceres::AutoDiffCostFunction<ResidualErrorFunctor_Pinhole, 2, 4, 6, 3>(new ResidualErrorFunctor_Pinhole(w, h, obsUndistorted));
228233
case EINTRINSIC::PINHOLE_CAMERA_BROWN:
229234
return new ceres::AutoDiffCostFunction<ResidualErrorFunctor_PinholeBrownT2, 2, 9, 6, 3>(
230235
new ResidualErrorFunctor_PinholeBrownT2(w, h, obsUndistorted));

src/aliceVision/sfmDataIO/AlembicImporter.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -621,11 +621,15 @@ bool readCamera(const Version& abcVersion,
621621
{
622622
distortion->setParameters(distortionParams);
623623
}
624+
625+
624626
std::shared_ptr<camera::Undistortion> undistortion = intrinsicCasted->getUndistortion();
625627
if (undistortion)
626628
{
627629
undistortion->setParameters(undistortionParams);
628630
undistortion->setOffset(undistortionOffset);
631+
//If undistortion exists, distortion does not
632+
intrinsicCasted->setDistortionObject(nullptr);
629633
}
630634
}
631635

src/aliceVision/sfmDataIO/jsonIO.cpp

+19-16
Original file line numberDiff line numberDiff line change
@@ -342,22 +342,6 @@ void loadIntrinsic(const Version& version, IndexT& intrinsicId, std::shared_ptr<
342342

343343
intrinsicWithDistoEnabled->setDistortionInitializationMode(distortionInitializationMode);
344344

345-
std::shared_ptr<camera::Distortion> distortionObject = intrinsicWithDistoEnabled->getDistortion();
346-
if (distortionObject)
347-
{
348-
std::vector<double> distortionParams;
349-
for (bpt::ptree::value_type& paramNode : intrinsicTree.get_child("distortionParams"))
350-
{
351-
distortionParams.emplace_back(paramNode.second.get_value<double>());
352-
}
353-
354-
// ensure that we have the right number of params
355-
if (distortionParams.size() == distortionObject->getParameters().size())
356-
{
357-
distortionObject->setParameters(distortionParams);
358-
}
359-
}
360-
361345
std::shared_ptr<camera::Undistortion> undistortionObject = intrinsicWithDistoEnabled->getUndistortion();
362346
if (undistortionObject)
363347
{
@@ -375,6 +359,25 @@ void loadIntrinsic(const Version& version, IndexT& intrinsicId, std::shared_ptr<
375359
loadMatrix("undistortionOffset", offset, intrinsicTree);
376360
undistortionObject->setOffset(offset);
377361
}
362+
363+
//If undistortion exists, distortion does not
364+
intrinsicWithDistoEnabled->setDistortionObject(nullptr);
365+
}
366+
367+
std::shared_ptr<camera::Distortion> distortionObject = intrinsicWithDistoEnabled->getDistortion();
368+
if (distortionObject)
369+
{
370+
std::vector<double> distortionParams;
371+
for (bpt::ptree::value_type& paramNode : intrinsicTree.get_child("distortionParams"))
372+
{
373+
distortionParams.emplace_back(paramNode.second.get_value<double>());
374+
}
375+
376+
// ensure that we have the right number of params
377+
if (distortionParams.size() == distortionObject->getParameters().size())
378+
{
379+
distortionObject->setParameters(distortionParams);
380+
}
378381
}
379382
}
380383

0 commit comments

Comments
 (0)