我正在尝试从 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 );
}