【问题标题】:Input Data Type of K-Means Clustering Method in OpenCV (vector<Mat>)OpenCV中K-Means聚类方法的输入数据类型(vector<Mat>)
【发布时间】:2017-04-27 15:45:15
【问题描述】:

我一直在研究 MNIST 数据集以对手写数字图像进行分类。我可以读取图像并计算它们的直方图。然后,我将直方图的 Mat 推回向量中。我无法实现 K-Means 聚类算法方法 (kmeans()),因为 method(InputArray) 的第一个参数会导致一些错误

已使用OpenCV 3.0。

vector<Mat> histogram_list;


//some implementations


int clusterCount = 10;
Mat labels, centers;
int attempts = 5;
kmeans(samplingHist(histogram_list, h_bins, s_bins), clusterCount, labels,
    TermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 10000, 0.0001),
    attempts, KMEANS_PP_CENTERS, centers);

有什么建议可以解决这个问题吗?

编辑:

mat.int.hpp 文件。关于断言的错误失败了......它让我回到那段代码。

template<typename _Tp> inline_Tp& Mat::at(int i0, int i1) {
CV_DbgAssert(dims <= 2);
CV_DbgAssert(data);
CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
CV_DbgAssert((unsigned)(i1 * DataType<_Tp>::channels) < (unsigned)(size.p[1] * channels()));
CV_DbgAssert(CV_ELEM_SIZE1(DataType<_Tp>::depth) == elemSize1());
return ((_Tp*)(data + step.p[0] * i0))[i1];

}

我尝试解决从向量采样到 Mat 的问题。但是,它无法帮助解决它。我在哪里犯了错误?

Mat samplingHist(vector<Mat> &vec, int h, int s) {
    Mat samples(vec.size(), h * s, CV_32F);
    for (int k = 0; k < vec.size(); k++)
        for (int y = 0; y < h; y++)
            for (int x = 0; x < s; x++)
                samples.at<float>(k, y* s + x) = vec[k].at<float>(y, x);
    return samples;
}

【问题讨论】:

  • 错误是什么?
  • 你用的是哪个opencv版本?

标签: c++ opencv k-means


【解决方案1】:

您传递了错误的参数。 InputArray data 的文档必须是 Mat 类型或至少是 std::vector&lt;cv::Point2f&gt;,而不是 std::vector&lt;Mat&gt;

看看这个例子: http://docs.opencv.org/3.1.0/de/d63/kmeans_8cpp-example.html

【讨论】:

  • 我尝试通过采样解决它,但不起作用。
猜你喜欢
  • 2015-02-15
  • 2013-02-07
  • 2015-04-11
  • 1970-01-01
  • 2014-01-04
  • 2021-08-19
  • 2011-03-08
  • 2011-08-13
  • 2013-08-08
相关资源
最近更新 更多