opencv kmeans聚集多个Mats

时间:2015-11-03 14:04:07

标签: c++ opencv cluster-analysis k-means

这个问题特定于opencv(c ++)。我期待将多个Matricies /图像聚集在一起以创建单行聚类中心并获得拟合的度量。

我目前正在使用BOWTrainer类,使用.add方法添加填充,然后使用.cluster方法生成中心。然而,它并没有输出一个度量来描述拟合的接近程度。我知道vanilla kmeans函数返回一个紧凑性度量标准,但是我无法找到一种方法来聚集多个不同的mat,这可能吗?

1 个答案:

答案 0 :(得分:1)

正如您在bagofwords.cpp中看到的那样,kmeans紧凑度量度未存储在BOWTrainer中。

Mat BOWKMeansTrainer::cluster( const Mat& _descriptors ) const
{
    Mat labels, vocabulary;
    kmeans( _descriptors, clusterCount, labels, termcrit, attempts, flags, vocabulary );
    return vocabulary;
}

因此您无法直接从BOWKMeansTrainer检索该值。

但是,您可以执行与BOWKMeansTrainer::cluster()中相同的操作并检索紧凑度度量:

// 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 );