嗨,使用opencv c ++,我想进行聚类,根据区域和高度对连接的组件进行分类。 我确实理解了聚类的概念,但我很难在opencv c ++中实现它。
在opencv中
http://docs.opencv.org/modules/core/doc/clustering.html
有一种聚类方法kmeans
我搜索的网站大部分都是在opencv c ++中解释kmeans功能的概念和参数,其中大部分是从opencv文档网站复制的。
double kmeans(InputArray data, int K, InputOutputArray bestLabels, TermCriteria criteria, int attempts, int flags, OutputArray centers=noArray() )
这里也有很好的例子,但它是用Python实现的
正如我上面提到的,我拥有所有连接的组件,我可以计算每个连接组件的面积和高度。
我想使用群集来区分连接的组件。
例如,使用k-means方法我会使用k = 2.
感谢..
答案 0 :(得分:2)
我正在发布片段,希望这会对你有帮助.... 组件的高度和面积可以用作kmean的特征。现在这里为每个功能kmean将给你中心。即1个区域中心和1个组件高度中心。
Mat labels;
int attempts = 11;
Mat centers;
int no_of_features = 2;//(i.e. height, area)
Mat samples(no_of_connected_components, no_of_features, CV_32F);
int no_of_sub_classes = 1; // vary for more sub classes
for (int j = 0; j < no_of_connected_components; j++)
{
for (int x = 0; x < no_of_features; x++)
{
samples.at<float>(j, x) = connected_component_values[j,x];
//fill the values(height, area) of connected component labelling
}
}
cv::kmeans(samples, no_of_sub_classes, labels, TermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 10000, 0.001), attempts, KMEANS_PP_CENTERS, centers);
for (size_t si_i = 0; si_i < no_of_sub_classes ; si_i++)
{
for (size_t si_j = 0; si_j < no_of_features; si_j++)
{
KmeanTable[si_i*no_of_sub_classes + si_i][si_j] = centers.at<float>(si_i, si_j);
}
}
这里我将中心存储在kmeanTable 2D数组中,您可以使用它。现在,对于每个连接的组件,您可以计算距离中心的欧氏距离。 较低的差异特征有资格进行分类。
答案 1 :(得分:0)
检查this。
除了迭代x,y和z之外,你将迭代组件和属性(区域和高度)。