OpenCV image size limit

后端 未结 2 1386
小蘑菇
小蘑菇 2021-01-20 17:17

I\'m trying to pass a huge Mat image (98304x51968) between openCV and itk using the ITK to openCV Bridge. I have an error :

Insufficient memory (OverFlow for imageS

2条回答
  •  -上瘾入骨i
    2021-01-20 18:04

    There seems to be a signed int (typically 32 bit) limitation in IplImage: From the named .cpp file here's the code snippet that leads to the error:

    const int64 imageSize_tmp = (int64)image->widthStep*(int64)image->height;
    image->imageSize = (int)imageSize_tmp;
    if( (int64)image->imageSize != imageSize_tmp )
        CV_Error( CV_StsNoMem, "Overflow for imageSize" );
    

    Which looks like (without checking) image->imageSize is a 32 bit signed int and this part of the code will detect and handle overflows. According to your posted link in the comments, the IplImage "bug" might got fixed (I didn't check that), so MAYBE you can remove this overflow detection step in the OpenCV code for newer IplImage versions, but that's just a guess and has to be confirmed. You'll have to check the type of image->imageSize. If it is a 64 bit type, you can probably change the openCV code to support Mats bigger than 2147483647 bytes.

    EDIT: REMARK: I checked the code in OpenCV 3.4 but the code line was the right one, so probably in Version 4.0 there's no change yet.

    If your are sure that the IplImage limitation got fixed, you can try this:

    const int64 imageSize_tmp = (int64)image->widthStep*(int64)image->height;
    image->imageSize = imageSize_tmp; // imageSize isn't 32 bit signed int anymore!
    //if( (int64)image->imageSize != imageSize_tmp ) // no overflow detection necessary anymore
    //    CV_Error( CV_StsNoMem, "Overflow for imageSize" ); // no overflow detection necessary anymore
    

    but better make sure that IplImage's imageSize is 64 bit now ;)

    UPDATE: The linked fix in https://github.com/opencv/opencv/pull/7507/commits/a89aa8c90a625c78e40f4288d145996d9cda3599 ADDED the overflow detection, so PROBABLY IplImage still has the 32 bit int imageSize limitation! Be careful here!

提交回复
热议问题