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

fixing centroids override by using cv::mat deep copy #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
15 changes: 8 additions & 7 deletions src/Vocabulary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,14 +396,18 @@ void Vocabulary::HKmeansStep(NodeId parent_id,

void Vocabulary::initiateClusters
(const std::vector<cv::Mat> &descriptors,
std::vector<cv::Mat> &clusters) const
std::vector<cv::Mat> &clusters) const
{
initiateClustersKMpp(descriptors, clusters);
}

// --------------------------------------------------------------------------


void Vocabulary::createNewCentroid(cv::Mat &feature,std::vector<cv::Mat> &clusters){
cv::Mat copy;
feature.copyTo(copy);
clusters.push_back(copy);
}
void Vocabulary::initiateClustersKMpp(
const std::vector<cv::Mat> &pfeatures,
std::vector<cv::Mat> &clusters) const
Expand Down Expand Up @@ -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<double>::iterator dit;
dit = min_dists.begin();
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion src/Vocabulary.h
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,12 @@ friend class FastSearch;
*/
virtual void initiateClusters(const std::vector<cv::Mat> &descriptors,
std::vector<cv::Mat> &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<cv::Mat> &clusters);
/**
* Creates k clusters from the given descriptor sets by running the
* initial step of kmeans++
Expand Down