OpenCV kmeans:N>=K 异常,错误 (-215)

OpenCV kmeans: N>=K exception, error (-215)

当我尝试这样使用 kmeans 时:

int K = 4;
Mat labels;
Mat centers;
std::vector<float> values;

// (put a bunch of values into "values" here...)

kmeans(values, K, labels, TermCriteria(TermCriteria::COUNT + TermCriteria::EPS, 10, 1.0), 10, KMEANS_PP_CENTERS, centers);

我收到错误:"error: (-215) N >= K in function kmeans"

values.size() = 360000,所以N明显大于K。什么给出?谢谢

OpenCV 奇怪地将一维数据解释为 1 元素数组。

像下面这样的东西应该可以解决这个问题:

kmeans(cv::Mat(values).reshape(1, values.size()), K, labels, TermCriteria(TermCriteria::COUNT + TermCriteria::EPS, 10, 1.0), 10, KMEANS_PP_CENTERS, centers);

我遇到了同样的问题。

这对我有用。 将向量转换为 Mat.

int K = 4;
Mat labels;
Mat centers;
std::vector<float> values;
Mat points=Mat(values,true);
points.convertTo(points, CV_32F);
kmeans(points, K, labels, TermCriteria(TermCriteria::COUNT + TermCriteria::EPS, 10, 1.0), 10, KMEANS_PP_CENTERS, centers);