Reputation: 2684
This question is specific to opencv(c++). I was looking to cluster multiple Matricies/images together to create a single row of cluster centres and get the measure of the fit.
I'm currently using the BOWTrainer class, using the .add method to add mats then using the .cluster method to generate the centres. It doesn't however output a metric to describe the closeness of the fit though. I know the vanilla kmeans function returns a compactness metric but i haven't been able to find a way to cluster multiple distinct mat's with this, is this possible?
Upvotes: 0
Views: 569
Reputation: 41765
As you can see in bagofwords.cpp, kmeans
compactness measure is not stored into BOWTrainer
.
Mat BOWKMeansTrainer::cluster( const Mat& _descriptors ) const
{
Mat labels, vocabulary;
kmeans( _descriptors, clusterCount, labels, termcrit, attempts, flags, vocabulary );
return vocabulary;
}
so you can't retrieve that value directly from BOWKMeansTrainer
.
You can, however, perform the same operations as in BOWKMeansTrainer::cluster()
and retrieve the compactness measure:
// int clusterCount = ...
// TermCriteria termcrit = ...
// int attempts = ...
// int flags = ...
// Create trainer
BOWKMeansTrainer bow(clusterCount, termcrit, attempts, flags);
// Add some descriptors
// bow.add(...);
// Retrieve descriptors
vector<Mat> descriptors = bow.getDescriptors();
// Check that !descriptors.empty()
// Form a single descriptor matrix (as in "Mat BOWKMeansTrainer::cluster() const")
int descCount = 0;
for( size_t i = 0; i < descriptors.size(); i++ )
descCount += descriptors[i].rows;
Mat mergedDescriptors( descCount, descriptors[0].cols, descriptors[0].type() );
for( size_t i = 0, start = 0; i < descriptors.size(); i++ )
{
Mat submut = mergedDescriptors.rowRange((int)start, (int)(start + descriptors[i].rows));
descriptors[i].copyTo(submut);
start += descriptors[i].rows;
}
// Perform cluster (as in "Mat BOWKMeansTrainer::cluster( const Mat& _descriptors ) const")
Mat labels, vocabulary;
double compactness = kmeans( mergedDescriptors, clusterCount, labels, termcrit, attempts, flags, vocabulary );
Upvotes: 1