Skip to content

Commit

Permalink
Enable apply calibration on smaller images
Browse files Browse the repository at this point in the history
  • Loading branch information
servantftechnicolor committed Nov 28, 2023
1 parent 38bc4db commit a44af82
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 6 deletions.
10 changes: 10 additions & 0 deletions src/aliceVision/camera/Undistortion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,20 @@ class Undistortion
_center = {width / 2, height / 2};
}

void setDiagonal(double diagonal)
{
//May be used for plates with a different size than lens grid
_diagonal = diagonal;
}

inline Vec2 getOffset() const { return _offset; }

Vec2 getSize() const { return _size; }

inline double getDiagonal() const { return _diagonal; }



const std::vector<double>& getParameters() const { return _undistortionParams; }

void setParameters(const std::vector<double>& params)
Expand Down
1 change: 1 addition & 0 deletions src/aliceVision/sfmDataIO/AlembicExporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ void AlembicExporter::DataImpl::addCamera(const std::string& name,
ODoubleArrayProperty(userProps, "mvg_undistortionParams").set(undistortion->getParameters());
ODoubleProperty(userProps, "mvg_undistortionOffsetX").set(undistortion->getOffset().x());
ODoubleProperty(userProps, "mvg_undistortionOffsetY").set(undistortion->getOffset().y());
ODoubleProperty(userProps, "mvg_undistortionDiagonal").set(undistortion->getDiagonal());
}

camObj.getSchema().set(camSample);
Expand Down
10 changes: 10 additions & 0 deletions src/aliceVision/sfmDataIO/AlembicImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ bool readCamera(const Version& abcVersion,
std::vector<double> distortionParams;
std::vector<double> undistortionParams;
Vec2 undistortionOffset = {0, 0};
double undistortionDiagonal = 0.0;

if (userProps)
{
Expand Down Expand Up @@ -581,6 +582,10 @@ bool readCamera(const Version& abcVersion,
{
undistortionOffset(1) = getAbcProp<Alembic::Abc::IDoubleProperty>(userProps, *propHeader, "mvg_undistortionOffsetY", sampleFrame);
}
if (const Alembic::Abc::PropertyHeader* propHeader = userProps.getPropertyHeader("mvg_undistortionDiagonal"))
{
undistortionDiagonal = getAbcProp<Alembic::Abc::IDoubleProperty>(userProps, *propHeader, "mvg_undistortionDiagonal", sampleFrame);
}
}
}
}
Expand Down Expand Up @@ -626,6 +631,11 @@ bool readCamera(const Version& abcVersion,
{
undistortion->setParameters(undistortionParams);
undistortion->setOffset(undistortionOffset);

if (!(abcVersion < Version(1, 2, 7)))
{
undistortion->setDiagonal(undistortionDiagonal);
}
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/aliceVision/sfmDataIO/jsonIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ void saveIntrinsic(const std::string& name, IndexT intrinsicId, const std::share
const double focalLengthMM = intrinsicScaleOffset->sensorWidth() * intrinsicScaleOffset->getScale().x() / double(intrinsic->w());
const double focalRatio = intrinsicScaleOffset->getScale().x() / intrinsicScaleOffset->getScale().y();
const double pixelAspectRatio = 1.0 / focalRatio;

intrinsicTree.put("initialFocalLength", initialFocalLengthMM);
intrinsicTree.put("focalLength", focalLengthMM);
intrinsicTree.put("pixelRatio", pixelAspectRatio);
Expand All @@ -182,7 +182,6 @@ void saveIntrinsic(const std::string& name, IndexT intrinsicId, const std::share
if (intrinsicScaleOffsetDisto)
{
bpt::ptree distParamsTree;

std::shared_ptr<camera::Distortion> distortionObject = intrinsicScaleOffsetDisto->getDistortion();
if (distortionObject)
{
Expand All @@ -202,8 +201,9 @@ void saveIntrinsic(const std::string& name, IndexT intrinsicId, const std::share

std::shared_ptr<camera::Undistortion> undistortionObject = intrinsicScaleOffsetDisto->getUndistortion();
if (undistortionObject)
{
{
saveMatrix("undistortionOffset", undistortionObject->getOffset(), intrinsicTree);
intrinsicTree.put("undistortionDiagonal", undistortionObject->getDiagonal());

for (double param : undistortionObject->getParameters())
{
Expand Down Expand Up @@ -374,6 +374,11 @@ void loadIntrinsic(const Version& version, IndexT& intrinsicId, std::shared_ptr<
Vec2 offset;
loadMatrix("undistortionOffset", offset, intrinsicTree);
undistortionObject->setOffset(offset);

if (!(version < Version(1, 2, 7)))
{
undistortionObject->setDiagonal(intrinsicTree.get<double>("undistortionDiagonal"));
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/aliceVision/sfmDataIO/sfmDataIO.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#define ALICEVISION_SFMDATAIO_VERSION_MAJOR 1
#define ALICEVISION_SFMDATAIO_VERSION_MINOR 2
#define ALICEVISION_SFMDATAIO_VERSION_REVISION 6
#define ALICEVISION_SFMDATAIO_VERSION_REVISION 7

// AliceVision version as a string; for example "0.9.0".
#define ALICEVISION_SFMDATAIO_VERSION_STRING \
Expand Down
12 changes: 10 additions & 2 deletions src/software/utils/main_applyCalibration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,14 @@ int aliceVision_main(int argc, char **argv)
// Check that aspect ratios are approximately equal
if (std::abs(aspect - calibrationAspect) > 1e-2)
{
ALICEVISION_LOG_ERROR("Intrinsic from input SfMData and calibrated SfMData are incompatible.");
return EXIT_FAILURE;
bool distortionOnly = (isDistortionCalibrated && !isIntrinsicCalibrated);
bool smaller = (width <= calibrationWidth && height <= calibrationHeight);

if (distortionOnly == false || smaller == false)
{
ALICEVISION_LOG_ERROR("Intrinsic from input SfMData and calibrated SfMData are incompatible.");
return EXIT_FAILURE;
}
}

// Copy original intrinsic
Expand Down Expand Up @@ -232,6 +238,8 @@ int aliceVision_main(int argc, char **argv)
calibratedUndistortion->getType());
undistortion->setSize(width, height);
undistortion->setParameters(calibratedUndistortion->getParameters());


newIntrinsic->setUndistortionObject(undistortion);
newIntrinsic->setDistortionInitializationMode(camera::EInitMode::CALIBRATED);
}
Expand Down

0 comments on commit a44af82

Please sign in to comment.