Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panorama alignment with a reference camera #1334

Merged
merged 6 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions src/aliceVision/sfm/utils/alignment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,8 +695,33 @@ IndexT getViewIdFromExpression(const sfmData::SfMData& sfmData, const std::strin
try
{
viewId = boost::lexical_cast<IndexT>(camName);
if(!sfmData.getViews().count(viewId))
viewId = -1;
if (!sfmData.getViews().count(viewId))
{
bool found = false;
//check if this view is an ancestor of a view
for (auto pv : sfmData.getViews())
{
for (auto ancestor : pv.second->getAncestors())
{
if (ancestor == viewId)
{
viewId = pv.first;
found = true;
break;
}
}

if (found)
{
break;
}
}

if (!found)
{
viewId = -1;
}
}
}
catch(const boost::bad_lexical_cast &)
{
Expand Down Expand Up @@ -938,5 +963,22 @@ bool computeNewCoordinateSystemFromGpsData(const sfmData::SfMData& sfmData, std:
return aliceVision::geometry::ACRansac_FindRTS(x1, x2, randomNumberGenerator, out_S, out_t, out_R, inliers, refine);
}

void getRotationNullifyX(Eigen::Matrix3d & out_R, const Eigen::Vector3d & pt)
{
/*
0 = [cos(x) 0 -sin(x)][X]
Y' = [0 1 0 ][Y]
Z' = [sin(x) 0 cos(x)][Z]

cos(x)X - sin(x)Z = 0
sin(x)/cos(x) = X/Z
tan(x) = X/Z
x = atan2(X, Z)
*/

double angle = std::atan2(pt(0), pt(2));
out_R = Eigen::AngleAxisd(angle, Vec3(0,-1,0)).toRotationMatrix();
}

} // namespace sfm
} // namespace aliceVision
9 changes: 9 additions & 0 deletions src/aliceVision/sfm/utils/alignment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,5 +306,14 @@ bool computeNewCoordinateSystemFromSpecificMarkers(const sfmData::SfMData& sfmDa
Mat3& out_R,
Vec3& out_t);

/**
* @brief Compute the 3D rotation matrix such that "pt"
* once rotated will have its x component equal to 0.
* This rotation will only affect rotate around the Y axis
* @param[out] out_R the result rotation matrix
* @param[in] pt the input point to nullify on X
*/
void getRotationNullifyX(Eigen::Matrix3d & out_R, const Eigen::Vector3d & pt);

} // namespace sfm
} // namespace aliceVision
53 changes: 52 additions & 1 deletion src/software/utils/main_sfmTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,36 @@ static void parseManualTransform(const std::string& manualTransform, double& S,

} // namespace

IndexT getReferenceViewIndex(const sfmData::SfMData & sfmData, const std::string & transform)
fabiencastan marked this conversation as resolved.
Show resolved Hide resolved
{
IndexT refPoseId;
fabiencastan marked this conversation as resolved.
Show resolved Hide resolved
try
{
refPoseId = sfm::getViewIdFromExpression(sfmData, transform);
servantftechnicolor marked this conversation as resolved.
Show resolved Hide resolved
}
catch (...)
{
refPoseId = UndefinedIndexT;
}

if (refPoseId == UndefinedIndexT)
{
// Sort views per timestamps
std::vector<std::pair<int64_t, IndexT>> sorted_views;
for (auto v : sfmData.getViews()) {
int64_t t = v.second->getMetadataDateTimestamp();
sorted_views.push_back(std::make_pair(t, v.second->getPoseId()));
servantftechnicolor marked this conversation as resolved.
Show resolved Hide resolved
fabiencastan marked this conversation as resolved.
Show resolved Hide resolved
}
std::sort(sorted_views.begin(), sorted_views.end());

// Get the view which was taken at the middle of the sequence
int median = sorted_views.size() / 2;
refPoseId = sorted_views[sorted_views.size() - 1].second;
}

return refPoseId;
}

int aliceVision_main(int argc, char **argv)
{
// command-line parameters
Expand Down Expand Up @@ -269,6 +299,10 @@ int aliceVision_main(int argc, char **argv)
return EXIT_FAILURE;
}

IndexT refPoseId = getReferenceViewIndex(sfmData, transform);

Eigen::Matrix3d ref_R_world = sfmData.getAbsolutePose(refPoseId).getTransform().rotation();
servantftechnicolor marked this conversation as resolved.
Show resolved Hide resolved

double S = 1.0;
Mat3 R = Mat3::Identity();
Vec3 t = Vec3::Zero();
Expand Down Expand Up @@ -306,8 +340,25 @@ int aliceVision_main(int argc, char **argv)
break;

case EAlignmentMethod::AUTO_FROM_CAMERAS_X_AXIS:
{
//Align with x axis
sfm::computeNewCoordinateSystemFromCamerasXAxis(sfmData, S, R, t);
break;

//Align with reference image
Eigen::Matrix3d refcam_R_updatedWorld = ref_R_world * R.transpose();
Eigen::Matrix3d updatedWorld_R_refcam = refcam_R_updatedWorld.transpose();
Eigen::Vector3d alignmentVector = updatedWorld_R_refcam * Eigen::Vector3d::UnitZ();

Eigen::Matrix3d zeroX_R_world;
sfm::getRotationNullifyX(zeroX_R_world, alignmentVector);

//Cumulate both transformations
//(zeroX_R_world * updatedWorld_R_refcam).transpose()
//(zeroX_R_world * (ref_R_world * R.transpose()).transpose()).transpose()
// ref_R_world * R.transpose() * zeroX_R_world.transpose()
R = zeroX_R_world * R;
servantftechnicolor marked this conversation as resolved.
Show resolved Hide resolved
}
break;

case EAlignmentMethod::AUTO_FROM_LANDMARKS:
sfm::computeNewCoordinateSystemFromLandmarks(sfmData, feature::EImageDescriberType_stringToEnums(landmarksDescriberTypesName), S, R, t);
Expand Down