我正在尝试从kmeans函数返回的数据中创建一个集群图像。我试图以类似于OpenCV示例的方式提取数据,但是那似乎在我的计算机上崩溃了。经过进一步的研究,我发现有人通过使用中心提取数据,但对该数据没有进行任何处理,所以我的尝试到此结束。
下面是我的代码片段和我的操作。非常感谢您的帮助。
编辑 我将代码恢复到原始状态,没有测试变量。上述错误仍然存在。我还添加了有关我的图像的一些调试信息:
图像信息:
- 维度:2
- 图像数据:
- 大小0:256
- 大小1:256
- Elemsize 1:12
- Elemsize 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 );
}
dims <= 2
测试。你可能失败了,因为data
为空或i
大于点数。在for
循环之前尝试添加cout << "data: " << mImage.data << endl;
。 - SSteve