我在opencv,c ++中使用kmeans聚类。我正在尝试为找到的每个群集创建一个新的向量,并将结果点添加到它。
我有:
vector<cv::Point2f> markerOne;
vector<cv::Point2f> markerTwo;
vector<cv::Point2f> markerThree;
vector<cv::Point2f> markerFour;
Mat labels, centers;
int attempts = 50, numberClusters = 4, flags = cv::KMEANS_PP_CENTERS;
TermCriteria tc;
kmeans(centroids, cNumber, labels, tc, attempts, flags, centers);
for (int i = 0; i < labels.rows; i++)
{
int idx = labels.at<int>(i);
Point2f original_point = centroids[i];
Point2f clustered_center;
clustered_center.x = centers.at<float>(idx, 0);
clustered_center.y = centers.at<float>(idx, 1);
我需要的是根据此循环中返回的idx值创建一个向量。我现在有:
if (idx == 0)
{
markerOne.push_back(original_point);
}
if (idx == 1)
{
markerTwo.push_back(original_point);
}
if (idx == 2)
{
markerThree.push_back(original_point);
}
if (idx == 3)
{
markerFour.push_back(original_point);
}
最多可能有100个集群,所以我正在寻找一种更有活力的方法来实现这一目标。
谢谢!
答案 0 :(得分:1)
创建一个矢量矢量结构,并将这些点推入相应的矢量,如下所示:
std::vector<std::vector<Point2f> > vector_of_vector(labels.rows);
for (int i = 0; i < labels.rows; i++)
{
vector_of_vector[i].push_back(original_point);
}