[英]Opencv: how does the 'centers' parameter work in cv::kmeans?
I'm clustering points using kmeans with C++. 我正在使用kmeans和C ++对点进行聚类。 The clustering and the plotting of each point work well, I can get something like this:
每个点的聚类和绘图效果很好,我可以得到这样的结果:
but I am not able to access the centers of each cluster (I want to plot them). 但我无法访问每个集群的中心(我想绘制它们)。 How can I get the centers positions without computing them by myself?
如何在不自己计算中心位置的情况下获得中心位置?
I call the function in this way: 我用这种方式调用函数:
vector<Point2f> points;
...
// clustering
int K = 4; // number of clusters
Mat labels; // cluster each point belongs to
Mat centers; // center of each cluster
kmeans(points, K, labels, TermCriteria( TermCriteria::EPS+TermCriteria::COUNT, 50, 1.0), 3, KMEANS_PP_CENTERS, centers);
and I plot them in this way: 我用这种方式绘制它们:
for(unsigned int i = 0; i < points.size(); i++ ){
int clusterIdx = labels.at<int>(i);
Point ipt = points[i];
circle(cluster_image, ipt, 10, colorTab[clusterIdx], -1, 8);
}
You might want to look at the the content of your centers matrix. 您可能希望查看中心矩阵的内容。
Size2i centersSize = centers.size();
std::list<Poind2f> centerPoints;
for(int i=0; i<centers.size(0); i++) {
Point2f pt = centers.row(i);
centerPoints.push_back(pt);
}
Even though the code above is untested, I hope it clarifies the idea. 即使上面的代码未经测试,我希望它澄清了这个想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.