0

I'm creating a new Mat by jumping pixels of the original image, but I get this error:

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.

My code is:

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;

I also tried using uchar* ptr = img.ptr<uchar>(i) and ptr[j] so it's possible to access the data directly, but I receive the same error.

I was searching, and tried some "solution" such as sYSMALLOc: Assertion Failed error in opencv, but the trouble keeps appearing.

Community
  • 1
  • 1
Bardo91
  • 585
  • 2
  • 7
  • 17

1 Answers1

0

In general, this error occurs when you try to access memory at an invalid location. Your code has two issues that contribute to the problem.

First, you access elements of res with cv::Vec3b, which implies a three-channel image, but you initialize it as single channel. Change the initialization to:

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

Second, .at<>(i, j) accesses elements by their row index i and column index j. However, your i and j indices refer to column and row, respectively. Your iteration limits should be swapped:

for (int i = 0; i < height; i++) {
    for (int j = 0; j < width; j++) {
// ...
Aurelius
  • 11,111
  • 3
  • 52
  • 69
  • Oh! Thanks, yesterday I found the first error, but the second one... I feel a little bit "stupid" hahah. Thanks again! – Bardo91 Jul 25 '13 at 07:00