3

我正在尝试从 kmeans 函数返回的数据创建一个聚类图像。我尝试以类似的方式从 OpenCV 示例中提取数据,但这似乎让我崩溃了。经过一些进一步的研究,我看到有人通过使用这些中心提取了数据,但没有对这些数据做任何事情,所以我的追踪就到此为止了。

我在下面包含了我的代码片段和我在做什么。任何帮助将不胜感激。

编辑 我已经将我的代码恢复到原始状态,没有任何测试变量。上述错误仍然存​​在。我还在下面添加了一些关于我的图像的调试信息:

图片信息:

  • 尺寸:2
  • 图像数据:
  • 尺寸 0:256
  • 尺寸 1:256
  • 元素尺寸 1: 12
  • 元素尺寸 2: 4

虽然数据为 NULL,但如果我调用cv::imshow它,我仍然可以查看数据。

// mImage is a cv::Mat that was created from a 256 x 256 image with the depth of
// CV_32F and 3 channels
// labels is a cv::Mat that was created by converting the image into a 256 x 256
// CV_8UC1 IplImage
kmeans(mImage, 6, labels, termcrit, 3, cv::KMEANS_PP_CENTERS, centers);
float* c = new float[6];
memcpy(c, centers.ptr<float>(0), sizeof(float)*6);

// Block of code that crashes when I do "mImage.at<cv::Point2f>(i)" with the error:
/* OpenCV Error: Assertion failed (dims <= 2 && data && (size.p[0] == 1 || 
       size.p[1] == 1) && (unsigned)i0 < (unsigned)(size.p[0] + size.p[1] - 1) && 
       elemSize() == (((((DataType<_Tp>::type) & ((512 - 1) << 3)) >> 3) + 1) << 
       ((((sizeof(size_t)/ 4+1)*16384|0x3a50) >> ((DataType<_Tp>::type) & 
       ((1 << 3) - 1))*2) & 3))) in unknown function, file 
       c:\opencv-2.3.0\modules\core\include\opencv2\core\mat.hpp, line 583 */

for(int i = 0; i < list.rows; i++)
{
    int clusterIdx = list.at<int>(i);
    cv::Point ipt = mImage.at<cv::Point2f>(i);
    circle( miplImage, ipt, 2, colorTab[clusterIdx], CV_FILLED, CV_AA );
}
4

1 回答 1

1

您在调用 mRegion.at() 时收到断言错误。mRegion 在哪里定义?

kmeans.cpp以下是示例代码中的等效片段:

    kmeans(points, clusterCount, labels, 
           TermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0),
           3, KMEANS_PP_CENTERS, centers);

    img = Scalar::all(0);

    for( i = 0; i < sampleCount; i++ )
    {
        int clusterIdx = labels.at<int>(i);
        Point ipt = points.at<Point2f>(i);
        circle( img, ipt, 2, colorTab[clusterIdx], CV_FILLED, CV_AA );
    }

points它对循环的第一个参数kmeans和循环的源使用相同的ipt变量for。我想你可能想mImagefor循环中使用,而不是 mRegion。

编辑现在您已经更改了代码以在for循环中使用 mImage,您需要了解为什么会出现断言错误。断言消息被模板代码弄得很乱,所以它来自 mat.hpp:

dims <= 2 && data && (size.p[0] == 1 || size.p[1] == 1) &&
             (unsigned)i0 < (unsigned)(size.p[0] + size.p[1] - 1) &&
             elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type)

其中一项或多项测试失败。如果您使用的是空的 mImage,那将是失败的,因为data它将为零。您不能调用at空矩阵。在任何情况下,检查 mImage 的属性以查看导致断言错误的原因。

于 2011-08-11T14:13:14.180 回答