-
-
Notifications
You must be signed in to change notification settings - Fork 876
Apply distance weighting for observations #2123
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
Open
servantftransperfect
wants to merge
1
commit into
develop
Choose a base branch
from
dev/residualNormalization
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
src/aliceVision/sfm/pipeline/expanding/DistanceWeighting.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| // This file is part of the AliceVision project. | ||
| // Copyright (c) 2026 AliceVision contributors. | ||
| // This Source Code Form is subject to the terms of the Mozilla Public License, | ||
| // v. 2.0. If a copy of the MPL was not distributed with this file, | ||
| // You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
|
||
| #include <aliceVision/sfm/pipeline/expanding/DistanceWeighting.hpp> | ||
| #include "nanoflann.hpp" | ||
|
|
||
| #include <algorithm> | ||
| #include <numeric> | ||
| #include <cmath> | ||
|
|
||
| namespace aliceVision { | ||
| namespace sfm { | ||
|
|
||
| struct PointCloud2D | ||
| { | ||
| std::vector<sfmData::Observation *> pts; | ||
|
|
||
| // ── mandatory nanoflann interface ────────── | ||
|
|
||
| // Total number of points | ||
| inline size_t kdtree_get_point_count() const { return pts.size(); } | ||
|
|
||
| // Value of the d-th coordinate of the i-th point | ||
| inline double kdtree_get_pt(const size_t idx, const size_t dim) const | ||
| { | ||
| return dim == 0 ? pts[idx]->getX() : pts[idx]->getY(); | ||
| } | ||
|
|
||
| // Optional bounding-box computation (return false = let nanoflann compute it) | ||
| template <class BBOX> | ||
| bool kdtree_get_bbox(BBOX&) const { return false; } | ||
| }; | ||
|
|
||
| bool weightObservationsFromDistance(sfmData::SfMData & sfmData, size_t neighboorRank, double ratioDefaultArea) | ||
| { | ||
| const int K = static_cast<int>(neighboorRank); | ||
|
|
||
| sfmData::Landmarks & landmarks = sfmData.getLandmarks(); | ||
|
|
||
| std::map<IndexT, PointCloud2D> perViewObservations; | ||
|
|
||
| // Make list of landmark per view | ||
| for (auto & [_, landmark] : landmarks) | ||
| { | ||
| for (auto & [idView, obs] : landmark.getObservations()) | ||
| { | ||
| perViewObservations[idView].pts.push_back(&obs); | ||
| } | ||
| } | ||
|
|
||
| // Loop per view | ||
| for (auto & [idView, pointCloud] : perViewObservations) | ||
| { | ||
| int N = pointCloud.pts.size(); | ||
|
|
||
| if (N == 0) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| const sfmData::View & v = sfmData.getView(idView); | ||
| const double viewArea = v.getImage().getWidth() * v.getImage().getHeight(); | ||
|
|
||
|
|
||
| // average area per feature | ||
| const double featArea = viewArea / N; | ||
| const double minSqDist = 1.0; | ||
| const double maxSqDist = ratioDefaultArea * featArea; | ||
|
|
||
| if (N < 10) | ||
| { | ||
| for (size_t i = 0; i < N; ++i) | ||
| { | ||
| pointCloud.pts[i]->setWeight(1.0); | ||
| } | ||
| continue; | ||
| } | ||
|
|
||
| using KDTree2D = nanoflann::KDTreeSingleIndexAdaptor<nanoflann::L2_Simple_Adaptor<double, PointCloud2D>, PointCloud2D, 2>; | ||
|
|
||
| KDTree2D index(2, pointCloud, nanoflann::KDTreeSingleIndexAdaptorParams(10)); | ||
| index.buildIndex(); | ||
|
|
||
| std::vector<double> knnSquaredDistance(N); | ||
|
|
||
| // Find the distance to the Kth nearest neighbor | ||
| #pragma omp parallel | ||
| { | ||
| std::vector<unsigned int> indices(K); | ||
| std::vector<double> sq_dists(K); | ||
|
|
||
| #pragma omp for schedule(static) | ||
| for (size_t i = 0; i < N; ++i) | ||
| { | ||
| const double query[2] = { pointCloud.pts[i]->getX(), pointCloud.pts[i]->getY() }; | ||
| index.knnSearch(query, K, indices.data(), sq_dists.data()); | ||
| knnSquaredDistance[i] = std::clamp(sq_dists[K - 1], minSqDist, maxSqDist); | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| // Normalize so the mean is 1 (overall influence is 1).what is | ||
| const double meanNormalizedDistance = std::accumulate(knnSquaredDistance.begin(), knnSquaredDistance.end(), 0.0) / static_cast<double>(N); | ||
|
|
||
| // Fallback to uniform weighting if the stats are incorrect | ||
| if (meanNormalizedDistance < 1e-12) | ||
| { | ||
| for (size_t i = 0; i < N; ++i) | ||
| { | ||
| pointCloud.pts[i]->setWeight(1.0); | ||
| } | ||
| continue; | ||
| } | ||
|
|
||
| for (size_t i = 0; i < N; ++i) | ||
| { | ||
| knnSquaredDistance[i] /= meanNormalizedDistance; | ||
| pointCloud.pts[i]->setWeight(knnSquaredDistance[i]); | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| } | ||
| } | ||
32 changes: 32 additions & 0 deletions
32
src/aliceVision/sfm/pipeline/expanding/DistanceWeighting.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // This file is part of the AliceVision project. | ||
| // Copyright (c) 2026 AliceVision contributors. | ||
| // This Source Code Form is subject to the terms of the Mozilla Public License, | ||
| // v. 2.0. If a copy of the MPL was not distributed with this file, | ||
| // You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <aliceVision/sfmData/SfMData.hpp> | ||
|
|
||
| namespace aliceVision { | ||
| namespace sfm { | ||
|
|
||
| /** | ||
| * @brief Weight observations based on their spatial distribution in the image. | ||
| * Points that are in sparse areas (far from their neighbors) get a higher weight, | ||
| * while points in dense clusters get a lower weight. This helps to balance the | ||
| * influence of features across the image. | ||
| * | ||
| * The weight is calculated based on the squared distance to the K-th nearest neighbor (neighboorRank), | ||
| * clamped to a maximum area (ratioDefaultArea * averageFeatureArea). | ||
| * Weights are normalized per view so that their mean is 1.0. | ||
| * | ||
| * @param[in,out] sfmData The SfM data containing views and landmarks to be weighted. | ||
| * @param[in] neighboorRank The rank (K) of the neighbor to use for distance calculation. | ||
| * @param[in] ratioDefaultArea Multiplier for clamping the maximum area to consider for a point. | ||
| * @return True if successful. | ||
| */ | ||
| bool weightObservationsFromDistance(sfmData::SfMData & sfmData, size_t neighboorRank, double ratioDefaultArea); | ||
|
|
||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.