From f116db14f85c5994d3810293456040a5c9620e90 Mon Sep 17 00:00:00 2001 From: yinonb4 Date: Wed, 26 Jul 2023 12:43:57 +0300 Subject: [PATCH] fixing centroids override by using cv::mat deep copy --- src/Vocabulary.cpp | 15 ++++++++------- src/Vocabulary.h | 7 ++++++- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/Vocabulary.cpp b/src/Vocabulary.cpp index 9f0eff3..4353e6e 100644 --- a/src/Vocabulary.cpp +++ b/src/Vocabulary.cpp @@ -396,14 +396,18 @@ void Vocabulary::HKmeansStep(NodeId parent_id, void Vocabulary::initiateClusters (const std::vector &descriptors, - std::vector &clusters) const + std::vector &clusters) const { initiateClustersKMpp(descriptors, clusters); } // -------------------------------------------------------------------------- - +void Vocabulary::createNewCentroid(cv::Mat &feature,std::vector &clusters){ + cv::Mat copy; + feature.copyTo(copy); + clusters.push_back(copy); +} void Vocabulary::initiateClustersKMpp( const std::vector &pfeatures, std::vector &clusters) const @@ -431,8 +435,7 @@ void Vocabulary::initiateClustersKMpp( int ifeature = rand()% pfeatures.size();//DUtils::Random::RandomInt(0, pfeatures.size()-1); // create first cluster - clusters.push_back(pfeatures[ifeature]); - + createNewCentroid(pfeatures[ifeature],clusters); // compute the initial distances std::vector::iterator dit; dit = min_dists.begin(); @@ -477,9 +480,7 @@ void Vocabulary::initiateClustersKMpp( ifeature = pfeatures.size()-1; else ifeature = dit - min_dists.begin(); - - - clusters.push_back(pfeatures[ifeature]); + createNewCentroid(pfeatures[ifeature],clusters); } // if dist_sum > 0 else break; diff --git a/src/Vocabulary.h b/src/Vocabulary.h index 7cf5f5a..da7faff 100644 --- a/src/Vocabulary.h +++ b/src/Vocabulary.h @@ -402,7 +402,12 @@ friend class FastSearch; */ virtual void initiateClusters(const std::vector &descriptors, std::vector &clusters) const; - + /** + * @brief Creates a new centroid by making a deep copy of the input feature and stores it in clusters. + * @param feature The input feature to be deep copied and used as a new centroid. + * @param clusters A vector of cv::Mat objects representing the existing clusters. + */ + void createNewCentroid(cv::Mat &feature,std::vector &clusters); /** * Creates k clusters from the given descriptor sets by running the * initial step of kmeans++