Skip to content

Commit

Permalink
Clean up focal length and change anamorphic model
Browse files Browse the repository at this point in the history
  • Loading branch information
servantftechnicolor committed Sep 13, 2024
1 parent 573a69a commit 5a2abb6
Show file tree
Hide file tree
Showing 11 changed files with 140,349 additions and 75 deletions.
102 changes: 96 additions & 6 deletions src/aliceVision/camera/IntrinsicScaleOffset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,107 @@ bool IntrinsicScaleOffset::importFromParams(const std::vector<double>& params, c
return true;
}

double IntrinsicScaleOffset::getFocalLengthMM() const
double IntrinsicScaleOffset::getFocalLength() const
{
return _scale(0) * sensorWidth() / static_cast<double>(std::max(w(), h()));
const double maxSize = static_cast<double>(std::max(w(), h()));
const double fx = _scale(0);
const double fy = _scale(1);

//The bigger the scale or focal (which have the same direction)
//The smaller the observed area
const double focalInMillimeters = fx * sensorWidth() / maxSize;

//Pixel aspect ratio is the how the x dimension is stretched
//That means that it enlarge the observed area
//That means a larger pixel ratio leads to a smaller focal (in X).
const double pixelAspectRatio = getPixelAspectRatio();

//Assumming the focal length is *ignoring* the stretch
//Thus the returned focal is the bigger focal canceling the pixelAspectRatio
return focalInMillimeters * pixelAspectRatio;
}

void IntrinsicScaleOffset::setFocalLength(double focalLengthMM, double pixelAspectRatio)
double IntrinsicScaleOffset::getInitialFocalLength() const
{
const double focalRatio = 1.0 / pixelAspectRatio;
_scale(0) = (focalLengthMM / sensorWidth()) * double(w());
_scale(1) = _scale(0) / focalRatio;
const double maxSize = static_cast<double>(std::max(w(), h()));
const double fx = _initialScale(0);
const double fy = _initialScale(1);

if (fx < 0)
{
return -1.0;
}

//The bigger the scale or focal (which have the same direction)
//The smaller the observed area
const double focalInMillimeters = fx * sensorWidth() / maxSize;

//Pixel aspect ratio is the how the x dimension is stretched
//That means that it enlarge the observed area
//That means a larger pixel ratio leads to a smaller focal (in X).
const double pixelAspectRatio = getPixelAspectRatio();

//Assumming the focal length is *ignoring* the stretch
//Thus the returned focal is the bigger focal canceling the pixelAspectRatio
return focalInMillimeters * pixelAspectRatio;
}

double IntrinsicScaleOffset::getPixelAspectRatio() const
{
const double fx = _scale(0);
const double fy = _scale(1);

const double focalRatio = fx / fy;
const double pixelAspectRatio = 1.0 / focalRatio;

return pixelAspectRatio;
}

void IntrinsicScaleOffset::setFocalLength(double focalInMillimeters, double pixelAspectRatio, bool useCompatibility)
{
if (useCompatibility)
{
const double focalInMillimetersY = focalInMillimeters * pixelAspectRatio;
const double millimetersToPixels = double(w()) / sensorWidth();
_scale(0) = focalInMillimeters * millimetersToPixels;
_scale(1) = focalInMillimetersY * millimetersToPixels;
}
else
{
//We assume focalInMillimeters ignore the pixelAspectRatio in X
const double focalInMillimetersX = focalInMillimeters / pixelAspectRatio;
const double millimetersToPixels = double(w()) / sensorWidth();
_scale(0) = focalInMillimetersX * millimetersToPixels;
_scale(1) = focalInMillimeters * millimetersToPixels;
}
}

void IntrinsicScaleOffset::setInitialFocalLength(double initialFocalInMillimeters, double pixelAspectRatio, bool useCompatibility)
{
if (initialFocalInMillimeters < 0.0)
{
_initialScale(0) = -1.0;
_initialScale(1) = -1.0;
}

const double millimetersToPixels = double(w()) / sensorWidth();

if (useCompatibility)
{
//We assume focalInMillimeters ignore the pixelAspectRatio in X
const double initialFocalInMillimetersY = initialFocalInMillimeters * pixelAspectRatio;
_initialScale(0) = initialFocalInMillimeters * millimetersToPixels;
_initialScale(1) = initialFocalInMillimetersY * millimetersToPixels;
}
else
{
//We assume focalInMillimeters ignore the pixelAspectRatio in X
const double initialFocalInMillimetersX = initialFocalInMillimeters / pixelAspectRatio;
_initialScale(0) = initialFocalInMillimetersX * millimetersToPixels;
_initialScale(1) = initialFocalInMillimeters * millimetersToPixels;
}
}


} // namespace camera
} // namespace aliceVision
27 changes: 24 additions & 3 deletions src/aliceVision/camera/IntrinsicScaleOffset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,35 @@ class IntrinsicScaleOffset : public IntrinsicBase
* @brief get focal length in mm
* @return focal length in mm
*/
double getFocalLengthMM() const;
double getFocalLength() const;

/**
* @brief get initial focal length in mm
* @return initial focal length in mm
*/
double getInitialFocalLength() const;

/**
* @brief estimate the pixel aspect ratio
* @return the pixel aspect ratio
*/
double getPixelAspectRatio() const;

/**
* @Brief set scale given focal length and pixelaspectratio
* @param focalLengthMM focal length in mm
* @param pixelAspectRatio pixel aspect ratio (image with = pixelAspectRatio * realwith)
* @param pixelAspectRatio pixel aspect ratio (image with = pixelAspectRatio * realwidth)
* @param usePixelRatioForX use pixel ratio
*/
void setFocalLength(double focalLengthMM, double pixelAspectRatio, bool useCompatibility = false);

/**
* @Brief set initial scale given initial focal length and pixelaspectratio
* @param initialFocalLengthMM initial focal length in mm
* @param pixelAspectRatio pixel aspect ratio (image with = pixelAspectRatio * realwidth)
* @param useCompability make sure compatibility mode is used
*/
void setFocalLength(double focalLengthMM, double pixelAspectRatio);
void setInitialFocalLength(double initialFocalLengthMM, double pixelAspectRatio, bool useCompatibility = false);

protected:
Vec2 _scale{1.0, 1.0};
Expand Down
3 changes: 1 addition & 2 deletions src/aliceVision/sfmDataIO/AlembicExporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,7 @@ void AlembicExporter::DataImpl::addCamera(const std::string& name,
std::vector<::uint32_t> sensorSize_pix = {intrinsicCasted->w(), intrinsicCasted->h()};
std::vector<double> sensorSize_mm = {sensorWidth, sensorHeight};

double initialFocalLength =
(intrinsicCasted->getInitialScale().x() > 0) ? (intrinsicCasted->getInitialScale().x() * sensorWidth / double(intrinsicCasted->w())) : -1;
double initialFocalLength = intrinsicCasted->getInitialFocalLength();

OUInt32ArrayProperty(userProps, "mvg_sensorSizePix").set(sensorSize_pix);
ODoubleArrayProperty(userProps, "mvg_sensorSizeMm").set(sensorSize_mm);
Expand Down
8 changes: 6 additions & 2 deletions src/aliceVision/sfmDataIO/AlembicImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -673,8 +673,12 @@ bool readCamera(const Version& abcVersion,
if (intrinsicCasted)
{
// fy_pix = fx_pix * fy/fx
initialFocalLengthPix(1) =
(initialFocalLengthPix(0) > 0) ? initialFocalLengthPix(0) * mvg_intrinsicParams[1] / mvg_intrinsicParams[0] : -1;
initialFocalLengthPix(1) = initialFocalLengthPix(0);
if (initialFocalLengthPix(0) > 0.0)
{
initialFocalLengthPix(0) = initialFocalLengthPix(0) / intrinsicCasted->getPixelAspectRatio();
}

intrinsicCasted->setInitialScale(initialFocalLengthPix);
intrinsicCasted->setRatioLocked(lockRatio);
intrinsicCasted->setOffsetLocked(lockOffset);
Expand Down
Binary file not shown.
Loading

0 comments on commit 5a2abb6

Please sign in to comment.