0

我正在Mat通过跳跃原始图像的像素来创建一个新图像,但出现此错误:

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]因此可以直接访问数据,但我收到同样的错误。

我正在搜索,并尝试了一些“解决方案”,例如sSYSMALLOc: Assertion Failed error in opencv,但问题不断出现。

4

1 回答 1

0

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

首先res,您访问with的元素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++) {
// ...
于 2013-07-25T00:18:27.220 回答