Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

OpenCV out of memory problem on imread\imdecode

I have a problem with OpenCV. I need to read from memory, rotate, resize and save some images. It happens in a thread in 32-bit app (sadly, there is limitation, due to 3rd party library, that doesn't support 64-bit).

My code looks like this:

void processImage(std::vector<char> data)
{
 cv::Mat image = cv::imdecode(data, cv::IMREAD_UNCHANGED);
 cv::rotate(image, image, image_rotation_);
 cv::imwrite(path_.toStdString(), image);
 QString preview_path = path_.replace(path_.section("/",-1),"Previews/"+path_.section("/",-1));

 cv::resize(image, image, cv::Size(preview_size_.width(), preview_size_.height()));
 cv::imwrite(preview_path.toStdString(), image);
 image.release();
}

But it crashes on 1st to 3rd call of imdecode with message

OpenCV: terminate handler is called! The last OpenCV error is:
OpenCV(3.4.7) Error: Insufficient memory (Failed to allocate 150962688 bytes) in OutOfMemoryError, file C:\opencv-3.4.7\modules\core\src\alloc.cpp, line 72

Saving to disk and reading with imread leads to same problem.

My app uses around 500Mb of memory, which is definitely less than 4Gb that should be available to 32-bit app and there is about 40% of physical memory free. Is there any way to resolve such problem or maybe I'm doing something wrong?