Skip to content

Commit

Permalink
[WIP] merge tracks
Browse files Browse the repository at this point in the history
  • Loading branch information
almarouk committed Oct 4, 2023
1 parent a68c925 commit 8b8dcc0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
56 changes: 45 additions & 11 deletions src/aliceVision/track/TracksBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ namespace track {
using namespace aliceVision::matching;
using namespace lemon;

// IndexedFeaturePair is: pair<viewId, keypointId>
using IndexedFeaturePair = std::pair<std::size_t, KeypointId>;
using IndexMap = lemon::ListDigraph::NodeMap<std::size_t>;
using UnionFindObject = lemon::UnionFindEnum< IndexMap >;

Expand Down Expand Up @@ -76,7 +74,8 @@ TracksBuilder::TracksBuilder()
TracksBuilder::~TracksBuilder() = default;

void mergeTracks(const feature::MapFeaturesPerView& featuresPerView, const MapIndexToNode& map_indexToNode,
const PairwiseMatches& pairwiseMatches, std::unique_ptr<TracksBuilderData>& _d)
const PairwiseMatches& pairwiseMatches, std::unique_ptr<TracksBuilderData>& _d,
stl::flat_map<IndexedFeaturePair, size_t>& _duplicateFeaturesMap)
{
// make the union according to duplicate features
// (same position, scale and describer type, but different orientations)
Expand All @@ -86,7 +85,7 @@ void mergeTracks(const feature::MapFeaturesPerView& featuresPerView, const MapIn
// map of DuplicateFeatureId(x, y, scale) to
// pair of (set<featureId>, node)
HashMap<size_t, HashMap<feature::EImageDescriberType,
HashMap<DuplicateFeatureId, std::pair<std::set<size_t>, const MapIndexToNode::mapped_type*>>>>
HashMap<DuplicateFeatureId, std::pair<std::set<size_t>, const MapIndexToNode::mapped_type*>>>>
duplicateFeaturesPerView;

// per viewId pair
Expand Down Expand Up @@ -150,6 +149,20 @@ void mergeTracks(const feature::MapFeaturesPerView& featuresPerView, const MapIn
}
}
}

// fill duplicate features map
for(const auto& [viewId, duplicateFeaturesPerDesc] : duplicateFeaturesPerView)
for(const auto& [descType, duplicateFeatures] : duplicateFeaturesPerDesc)
for(const auto& [duplicateFeatureId, duplicateFeature] : duplicateFeatures)
{
auto& duplicateFeatureIdsSet = duplicateFeature.first;
size_t indexedFeaturePair_0 = *duplicateFeatureIdsSet.begin();
for(const auto& featureId : duplicateFeatureIdsSet)
{
const auto& indexedFeaturePair_i = IndexedFeaturePair(viewId, KeypointId(descType, featureId));
_duplicateFeaturesMap[indexedFeaturePair_i] = indexedFeaturePair_0;
}
}
}

void TracksBuilder::build(const PairwiseMatches& pairwiseMatches, const feature::MapFeaturesPerView* featuresPerView)
Expand Down Expand Up @@ -225,7 +238,7 @@ void TracksBuilder::build(const PairwiseMatches& pairwiseMatches, const feature:
}

if(featuresPerView != NULL)
mergeTracks(*featuresPerView, map_indexToNode, pairwiseMatches, _d);
mergeTracks(*featuresPerView, map_indexToNode, pairwiseMatches, _d, _duplicateFeaturesMap);
}

void TracksBuilder::filter(bool clearForks, std::size_t minTrackLength, bool multithreaded)
Expand All @@ -244,14 +257,31 @@ void TracksBuilder::filter(bool clearForks, std::size_t minTrackLength, bool mul

#pragma omp single nowait
{
std::size_t cpt = 0;
std::set<std::size_t> myset;
bool flag = false;
stl::flat_map<size_t, IndexedFeaturePair> myset;
for(lemon::UnionFindEnum<IndexMap>::ItemIt iit(*_d->tracksUF, cit); iit != INVALID; ++iit)
{
myset.insert(_d->map_nodeToIndex[iit].first);
++cpt;
IndexedFeaturePair currentPair = _d->map_nodeToIndex[iit];
{
const auto& duplicateIt = _duplicateFeaturesMap.find(currentPair);
if(duplicateIt != _duplicateFeaturesMap.end())
currentPair.second.featIndex = duplicateIt->second;
}
const auto& myIt = myset.find(currentPair.first);
if(myIt != myset.end())
{
if(myIt->second < currentPair || currentPair < myIt->second)
{
flag = true;
// break if no minTrackLength ?
}
}
else
{
myset[currentPair.first] = currentPair;
}
}
if((clearForks && myset.size() != cpt) || myset.size() < minTrackLength)
if((clearForks && flag) || myset.size() < minTrackLength)
{
#pragma omp critical
set_classToErase.insert(cit.operator int());
Expand Down Expand Up @@ -302,7 +332,11 @@ void TracksBuilder::exportToSTL(TracksMap& allTracks) const
// all descType inside the track will be the same
outTrack.descType = currentPair.second.descType;
// overwrites featureIndex if clearForks is False (value is False by default in Meshroom and True by default in AliceVision)
outTrack.featPerView[currentPair.first] = currentPair.second.featIndex;
const auto& duplicateIt = _duplicateFeaturesMap.find(currentPair);
if(duplicateIt != _duplicateFeaturesMap.end())
outTrack.featPerView[currentPair.first] = duplicateIt->second;
else
outTrack.featPerView[currentPair.first] = currentPair.second.featIndex;
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/aliceVision/track/TracksBuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
namespace aliceVision {
namespace track {

// IndexedFeaturePair is: pair<viewId, keypointId>
using IndexedFeaturePair = std::pair<std::size_t, KeypointId>;

struct TracksBuilderData;

/**
Expand Down Expand Up @@ -84,6 +87,7 @@ class TracksBuilder

private:
std::unique_ptr<TracksBuilderData> _d;
stl::flat_map<IndexedFeaturePair, size_t> _duplicateFeaturesMap;
};

} // namespace track
Expand Down

0 comments on commit 8b8dcc0

Please sign in to comment.