Skip to content

Commit

Permalink
Merge pull request #786 from alicevision/fix/voctree/unsigned
Browse files Browse the repository at this point in the history
Fix/voctree/unsigned
  • Loading branch information
fabiencastan authored May 10, 2020
2 parents e6b6f9a + bfc0e19 commit 0998635
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/aliceVision/voctree/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ void Database::find( const SparseHistogram& query, std::size_t N, std::vector<Do
{
// for each document/image in the database compute the distance between the
// histograms of the query image and the others
float distance = sparseDistance(query, document.second, distanceMethod, word_weights_);
const float distance = sparseDistance(query, document.second, distanceMethod, word_weights_);
acc(DocMatch(document.first, distance));
}

Expand Down
63 changes: 34 additions & 29 deletions src/aliceVision/voctree/VocabularyTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@

#include "VocabularyTree.hpp"

#include <algorithm>

namespace aliceVision {
namespace voctree {

float sparseDistance(const SparseHistogram& v1, const SparseHistogram& v2, const std::string &distanceMethod, const std::vector<float>& word_weights)
{

float distance = 0.0f;
float epsilon = 0.001;
float distance{0.0f};
const float epsilon{0.001f};

SparseHistogram::const_iterator i1 = v1.begin(), i1e = v1.end();
SparseHistogram::const_iterator i2 = v2.begin(), i2e = v2.end();
auto i1 = v1.cbegin();
auto i1e = v1.cend();
auto i2 = v2.cbegin();
auto i2e = v2.cend();

if(distanceMethod.compare("classic") == 0)
if(distanceMethod == "classic")
{
while(i1 != i1e && i2 != i2e)
{
Expand All @@ -34,7 +38,8 @@ float sparseDistance(const SparseHistogram& v1, const SparseHistogram& v2, const
}
else
{
distance += fabs(i1->second.size() - i2->second.size());
const auto val = std::minmax(i1->second.size(), i2->second.size());
distance += static_cast<float>(val.second - val.first);
++i1;
++i2;
}
Expand All @@ -53,11 +58,11 @@ float sparseDistance(const SparseHistogram& v1, const SparseHistogram& v2, const
}
}

else if(distanceMethod.compare("commonPoints") == 0)
else if(distanceMethod == "commonPoints")
{
double score = 0.0;
double N1 = 0.0;
double N2 = 0.0;
float score{0.f};
float N1{0.f};
float N2{0.f};

while(i1 != i1e && i2 != i2e)
{
Expand Down Expand Up @@ -96,11 +101,11 @@ float sparseDistance(const SparseHistogram& v1, const SparseHistogram& v2, const
distance = - score;
}

else if(distanceMethod.compare("strongCommonPoints") == 0)
else if(distanceMethod == "strongCommonPoints")
{
double score = 0.0;
double N1 = 0.0;
double N2 = 0.0;
float score{0.f};
float N1{0.f};
float N2{0.f};

while(i1 != i1e && i2 != i2e)
{
Expand All @@ -116,7 +121,7 @@ float sparseDistance(const SparseHistogram& v1, const SparseHistogram& v2, const
}
else
{
if( ( fabs(i1->second.size() - 1.0) < epsilon ) && ( fabs(i2->second.size() - 1.0) < epsilon) )
if( ( fabs(i1->second.size() - 1.f) < epsilon ) && ( fabs(i2->second.size() - 1.f) < epsilon) )
{
score += 1;
N1 += 1;
Expand All @@ -142,11 +147,11 @@ float sparseDistance(const SparseHistogram& v1, const SparseHistogram& v2, const
distance = - score;
}

else if(distanceMethod.compare("weightedStrongCommonPoints") == 0)
else if(distanceMethod == "weightedStrongCommonPoints")
{
double score = 0.0;
double N1 = 0.0;
double N2 = 0.0;
float score{0.f};
float N1{0.f};
float N2{0.f};

while(i1 != i1e && i2 != i2e)
{
Expand All @@ -160,7 +165,7 @@ float sparseDistance(const SparseHistogram& v1, const SparseHistogram& v2, const
N1 += i1->second.size()*word_weights[i1->first];
++i1;
}
if( ( fabs(i1->second.size() - 1.0) < epsilon ) && ( fabs(i2->second.size() - 1.0) < epsilon) )
if( ( fabs(i1->second.size() - 1.f) < epsilon ) && ( fabs(i2->second.size() - 1.f) < epsilon) )
{
score += word_weights[i1->first];
N1 += word_weights[i1->first];
Expand All @@ -185,12 +190,12 @@ float sparseDistance(const SparseHistogram& v1, const SparseHistogram& v2, const
distance = - score;
}

else if(distanceMethod.compare("inversedWeightedCommonPoints") == 0)
else if(distanceMethod == "inversedWeightedCommonPoints")
{
double score = 0.0;
double N1 = 0.0;
double N2 = 0.0;
std::map<int,int> compteur;
float score{0.f};
float N1{0.f};
float N2{0.f};
std::map<int,int> counter;

while(i1 != i1e && i2 != i2e)
{
Expand All @@ -206,7 +211,7 @@ float sparseDistance(const SparseHistogram& v1, const SparseHistogram& v2, const
}
else
{
compteur[i1->first] += std::min(i1->second.size(), i2->second.size());
counter[i1->first] += std::min(i1->second.size(), i2->second.size());
N1 += i1->second.size() / word_weights[i1->first];
N2 += i2->second.size() / word_weights[i2->first];
++i1;
Expand All @@ -222,12 +227,12 @@ float sparseDistance(const SparseHistogram& v1, const SparseHistogram& v2, const

while(i2 != i2e)
{
N2 += i2->second.size() / word_weights[i2->first];;
N2 += i2->second.size() / word_weights[i2->first];
++i2;
}

for(auto iCompteur = compteur.begin(); iCompteur != compteur.end(); iCompteur++)
score += (1.0/iCompteur->second) * word_weights[iCompteur->first];
for(const auto elem : counter)
score += (1.f/ elem.second) * word_weights[elem.first];

distance = - score;
}
Expand Down

0 comments on commit 0998635

Please sign in to comment.