Alessandro
Alessandro

Reputation: 21

Failed to allocate huge images in OpenCV

I'm working since a year on image processing with OpenCV 2.2.0.

I get a memory allocation error ONLY if I try to allocate a >2GB IplImage, given that the same allocation with CvMat works. I can allocate whatever I want using CvMat, I tried also >10 GB.

OpenCV was 64-bit compiled and also this simple application. Furthermore, I'm sure that the application runs in 64-bit mode as I can see from the Task Manager. The O.S. (Windows 7) is 64-bit too.

int main(int argc, char* argv[])
{
    printf("trying to allocate >2GB matrix...\n");
    CvMat *huge_matrix = cvCreateMat(40000,30000,CV_16UC1);
    cvSet(huge_matrix,cvScalar(5));
    printf("...done!\n\n");
    system("PAUSE");

    printf("trying to allocate >2GB image...\n");
    IplImage *huge_img = cvCreateImage(cvSize(40000,30000),IPL_DEPTH_16U, 1);
    cvSet(huge_img,cvScalar(5));
    printf("...done!\n\n");
    system("PAUSE");

    cvReleaseMat(&huge_matrix);
    cvReleaseImage(&huge_img);
}

The error message is "Insufficient memory: in unknown function... can it be a bug?

Upvotes: 2

Views: 1833

Answers (1)

Andrey Kamaev
Andrey Kamaev

Reputation: 30122

IplImage structure does not support images bigger then 2Gb because it stores total image size in the field of int type. Even if you allocate IplImage bigger then 2Gb with some hack then other methods will not be able to process it correctly. OpenCV inherited IplImage structure from the Intel Image Processing Library so there are no chance that format will be changed.

You should use newer structures (CvMat in C interface or cv::Mat in C++ interface) to operate with huge images.

Upvotes: 2

Related Questions