2013-07-24 55 views
0

我跳原始图像的像素创建一个新的Mat,但我得到这个错误:opencv的 - 麻烦其他矩阵创建矩阵(malloc.c:2451:SYSMALLOC:断言)

PRM algorithm: malloc.c:2451: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed. 

我的代码是:

int width = round(img.cols/M); 
int height = round(img.rows/M); 

cv::Mat res(height, width, CV_8U); 

for (int i = 0; i < width; i++) { 
    for (int j = 0; j < height; j++) { 
     res.at<cv::Vec3b>(i, j)[0] = img.at<cv::Vec3b>(i * M, j * M)[0]; 
     res.at<cv::Vec3b>(i, j)[1] = img.at<cv::Vec3b>(i * M, j * M)[1]; 
     res.at<cv::Vec3b>(i, j)[2] = img.at<cv::Vec3b>(i * M, j * M)[2]; 
    } 
} 

return res; 

我也使用uchar* ptr = img.ptr<uchar>(i)ptr[j]所以它可以直接访问数据试过,但我收到同样的错误。

我正在寻找,并尝试了一些“解决方案”,如sYSMALLOc: Assertion Failed error in opencv,但麻烦不断出现。

回答

0

通常,当您尝试在无效位置访问内存时会发生此错误。你的代码有两个问题导致了这个问题。

首先,则可以访问的res元件与cv::Vec3b,这意味着一个三通道图像,但将其初始化为单信道。初始化更改为:

cv::Mat res(height, width, CV_8UC3); // Needs to be three channels! 

.at<>(i, j)访问由他们的行索引i和列索引j元素。但是,您的ij索引分别指向列和行。您的迭代限制应该交换:

for (int i = 0; i < height; i++) { 
    for (int j = 0; j < width; j++) { 
// ... 
+0

哦!谢谢,昨天我发现了第一个错误,但第二个错误......我觉得有点“愚蠢”,哈哈。再次感谢! – Bardo91