我正在使用 opencv kmeans 对从凸包返回的点进行聚类。
就我而言,我会得到 3 分。在下一个循环中,我想为 kmeans 提供最后找到的 3 个点。我读到我必须设置 KMEANS_USE_INITIAL_LABELS。
但是我如何设置初始标签/点?
这是我到目前为止所拥有的,但它返回错误:
//get all convexhull points and average them in 3 groups
int dimensions = 2;
float pointsdata[sampleCount*2]; //[] = {1,1, 2,2, 6,6, 5,5, 10,10};
int cnt = 0;
for(int a=0; a<sampleCount; a++){
pointsdata[cnt] = convexHull[a].x;
cnt++;
pointsdata[cnt] = convexHull[a].y;
cnt++;
}
cv::Mat points;
points = cv::Mat(sampleCount,dimensions, CV_32F,pointsdata);
int clusterCount = 3; //i want 3 averaged points back
cv::Mat labels;
labels = cv::Mat(3,1,points.type());
labels.at<float>(0,0) = pointA.x;
labels.at<float>(0,1) = pointA.y;
labels.at<float>(0,2) = pointB.x;
labels.at<float>(0,3) = pointB.y;
labels.at<float>(0,4) = pointC.x;
labels.at<float>(0,5) = pointC.y;
cv::Mat centers;
centers = cv::Mat(clusterCount, 1, points.type());
kmeans(points, 3, labels, cv::TermCriteria(), 2,cv::KMEANS_USE_INITIAL_LABELS, ¢ers);
更新:好的,我现在知道标签意味着输入集群中点的索引,而不是实际坐标。所以应该更像这样。但还是错了。
cv::Mat labels;
labels = cv::Mat(3,1,points.type());
labels.at<int>(0,0) = labelA;
labels.at<int>(0,1) = labelB;
labels.at<int>(0,2) = labelC;