From 54c1eae23f48c654afa80ed05ba3c74f821a8393 Mon Sep 17 00:00:00 2001 From: Fabien Servant Date: Tue, 28 Nov 2023 15:56:02 +0100 Subject: [PATCH 1/2] Enable apply calibration on smaller images --- src/aliceVision/camera/Undistortion.hpp | 10 ++++++++++ src/aliceVision/sfmDataIO/AlembicExporter.cpp | 1 + src/aliceVision/sfmDataIO/AlembicImporter.cpp | 11 +++++++++++ src/aliceVision/sfmDataIO/jsonIO.cpp | 11 ++++++++--- src/aliceVision/sfmDataIO/sfmDataIO.hpp | 2 +- src/aliceVision/version.hpp | 4 ++++ src/software/utils/main_applyCalibration.cpp | 12 ++++++++++-- 7 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/aliceVision/camera/Undistortion.hpp b/src/aliceVision/camera/Undistortion.hpp index c28c8d09ac..20c8aaf291 100644 --- a/src/aliceVision/camera/Undistortion.hpp +++ b/src/aliceVision/camera/Undistortion.hpp @@ -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& getParameters() const { return _undistortionParams; } void setParameters(const std::vector& params) diff --git a/src/aliceVision/sfmDataIO/AlembicExporter.cpp b/src/aliceVision/sfmDataIO/AlembicExporter.cpp index fa93fc7a0e..e02e765e0c 100644 --- a/src/aliceVision/sfmDataIO/AlembicExporter.cpp +++ b/src/aliceVision/sfmDataIO/AlembicExporter.cpp @@ -243,6 +243,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); diff --git a/src/aliceVision/sfmDataIO/AlembicImporter.cpp b/src/aliceVision/sfmDataIO/AlembicImporter.cpp index 164e364ed8..41a9fe172c 100644 --- a/src/aliceVision/sfmDataIO/AlembicImporter.cpp +++ b/src/aliceVision/sfmDataIO/AlembicImporter.cpp @@ -419,6 +419,7 @@ bool readCamera(const Version& abcVersion, std::vector distortionParams; std::vector undistortionParams; Vec2 undistortionOffset = {0, 0}; + double undistortionDiagonal = 0.0; if (userProps) { @@ -579,6 +580,10 @@ bool readCamera(const Version& abcVersion, { undistortionOffset(1) = getAbcProp(userProps, *propHeader, "mvg_undistortionOffsetY", sampleFrame); } + if (const Alembic::Abc::PropertyHeader* propHeader = userProps.getPropertyHeader("mvg_undistortionDiagonal")) + { + undistortionDiagonal = getAbcProp(userProps, *propHeader, "mvg_undistortionDiagonal", sampleFrame); + } } } } @@ -625,6 +630,12 @@ bool readCamera(const Version& abcVersion, { undistortion->setParameters(undistortionParams); undistortion->setOffset(undistortionOffset); + + if (abcVersion >= Version(1, 2, 7)) + { + undistortion->setDiagonal(undistortionDiagonal); + } + // If undistortion exists, distortion does not intrinsicCasted->setDistortionObject(nullptr); } diff --git a/src/aliceVision/sfmDataIO/jsonIO.cpp b/src/aliceVision/sfmDataIO/jsonIO.cpp index 902e006ad8..55baee448a 100644 --- a/src/aliceVision/sfmDataIO/jsonIO.cpp +++ b/src/aliceVision/sfmDataIO/jsonIO.cpp @@ -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); @@ -182,7 +182,6 @@ void saveIntrinsic(const std::string& name, IndexT intrinsicId, const std::share if (intrinsicScaleOffsetDisto) { bpt::ptree distParamsTree; - std::shared_ptr distortionObject = intrinsicScaleOffsetDisto->getDistortion(); if (distortionObject) { @@ -202,8 +201,9 @@ void saveIntrinsic(const std::string& name, IndexT intrinsicId, const std::share std::shared_ptr undistortionObject = intrinsicScaleOffsetDisto->getUndistortion(); if (undistortionObject) - { + { saveMatrix("undistortionOffset", undistortionObject->getOffset(), intrinsicTree); + intrinsicTree.put("undistortionDiagonal", undistortionObject->getDiagonal()); for (double param : undistortionObject->getParameters()) { @@ -358,6 +358,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("undistortionDiagonal")); + } } // If undistortion exists, distortion does not diff --git a/src/aliceVision/sfmDataIO/sfmDataIO.hpp b/src/aliceVision/sfmDataIO/sfmDataIO.hpp index ed43a9616a..6566e9b007 100644 --- a/src/aliceVision/sfmDataIO/sfmDataIO.hpp +++ b/src/aliceVision/sfmDataIO/sfmDataIO.hpp @@ -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 \ diff --git a/src/aliceVision/version.hpp b/src/aliceVision/version.hpp index 475b50db01..b45727e5a9 100644 --- a/src/aliceVision/version.hpp +++ b/src/aliceVision/version.hpp @@ -65,6 +65,10 @@ class Version return false; } + bool operator>=(const Version& other) const + { + return !operator<(other); + } private: Vec3i _v; diff --git a/src/software/utils/main_applyCalibration.cpp b/src/software/utils/main_applyCalibration.cpp index 3f8ff34281..aedce95b60 100644 --- a/src/software/utils/main_applyCalibration.cpp +++ b/src/software/utils/main_applyCalibration.cpp @@ -207,8 +207,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 @@ -237,6 +243,8 @@ int aliceVision_main(int argc, char** argv) auto undistortion = camera::createUndistortion(calibratedUndistortion->getType()); undistortion->setSize(width, height); undistortion->setParameters(calibratedUndistortion->getParameters()); + + newIntrinsic->setUndistortionObject(undistortion); newIntrinsic->setDistortionInitializationMode(camera::EInitMode::CALIBRATED); } From 49871dac395099b0e807b30750aaea7f4e4ef628 Mon Sep 17 00:00:00 2001 From: Fabien Castan Date: Tue, 12 Mar 2024 23:24:16 +0100 Subject: [PATCH 2/2] [software] ApplyCalibration: add warnings if resolutions are not equals --- src/software/utils/main_applyCalibration.cpp | 23 +++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/software/utils/main_applyCalibration.cpp b/src/software/utils/main_applyCalibration.cpp index aedce95b60..738ccdd739 100644 --- a/src/software/utils/main_applyCalibration.cpp +++ b/src/software/utils/main_applyCalibration.cpp @@ -204,11 +204,28 @@ int aliceVision_main(int argc, char** argv) const unsigned int calibrationHeight = calibratedIntrinsic->h(); const double calibrationAspect = static_cast(calibrationHeight) / static_cast(calibrationWidth); - // Check that aspect ratios are approximately equal + if(width != calibrationWidth || height != calibrationHeight) + { + ALICEVISION_LOG_WARNING( + "The image size used for the calibration is different from the size of the sequence.\n" << + "Calibration size: " << calibrationWidth << "x" << calibrationHeight << "\n" << + "Sequence size: " << width << "x" << height); + ALICEVISION_LOG_WARNING( + "Calibration image ratio: " << calibrationAspect << "\n" << + "Sequence image ratio: " << aspect); + } + + // If aspect ratios are not approximately equal if (std::abs(aspect - calibrationAspect) > 1e-2) { - bool distortionOnly = (isDistortionCalibrated && !isIntrinsicCalibrated); - bool smaller = (width <= calibrationWidth && height <= calibrationHeight); + ALICEVISION_LOG_WARNING("Image aspect ratios are not approximately equal, so we need more checks."); + + const bool distortionOnly = (isDistortionCalibrated && !isIntrinsicCalibrated); + const bool smaller = (width <= calibrationWidth && height <= calibrationHeight); + + ALICEVISION_LOG_WARNING("Distorsion is calibrated: " << isDistortionCalibrated); + ALICEVISION_LOG_WARNING("Intrinsics are calibrated: " << isIntrinsicCalibrated); + ALICEVISION_LOG_WARNING("Size is smaller than calibration: " << smaller); if (distortionOnly == false || smaller == false) {