bmeaut / grainautline

GrainAutLine: image processing for geology and material sciences
GNU General Public License v3.0
0 stars 2 forks source link

OutOfMemory issues with large marble images #7

Closed csorbakristof closed 8 years ago

csorbakristof commented 9 years ago

OpenCV seems to be unable to allocate sufficient memory. Although it does not seem to need much...

csorbakristof commented 9 years ago

Can be solved by x64 platform. Otherwise, we seem to fragment the heap too much.

szotsaki commented 9 years ago

Tried using the C API as suggested in http://stackoverflow.com/questions/9125817/opencv-imreadfilename-fails-in-debug-mode-when-using-release-libraries like

#ifdef Q_OS_WIN32
#  include <string>
#  include <opencv/cv.h>
#  define cv_imread(path) cv::cvarrToMat(cvLoadImage(path.c_str()), false)
#else
#  define cv_imread(path) cv::imread(path)
#endif

But unfortunately it didn't help.

The issue is in OpenCV and with only one cv::imread, so it's not related to heap fragmentation.

szotsaki commented 9 years ago

The output of the program after cv_imreadin ShowImageProcessor::GetOriginalImage:

Invalid parameter passed to C runtime function.
Invalid parameter passed to C runtime function.
[Fatal] ASSERT: "this->OriginalImage.data != nullptr" in file Processors\ShowImageProcessor.cpp, line 137 [qglobal.cpp:2966]
Invalid parameter passed to C runtime function.
Invalid parameter passed to C runtime function.
Invalid parameter passed to C runtime function.
Invalid parameter passed to C runtime function.
Invalid parameter passed to C runtime function.
Invalid parameter passed to C runtime function.
ASSERT: "mat.data" in file ..\..\GrainAutLine\GrainAutLine\ImageProviderBase.cpp, line 18
ASSERT: "mat.data" in file ..\..\GrainAutLine\GrainAutLine\ImageProviderBase.cpp, line 18
szotsaki commented 8 years ago

Copying the OpenCV source code into its proper place, normal backtraces happen. Opening a big file causes the following, more detailed error messages to appear:

OpenCV Error: Insufficient memory (Failed to allocate 271674744 bytes) in OutOfMemoryError, file C:\OpenCV30\opencv\sources\modules\core\src\alloc.cpp, line 52
OpenCV Error: Assertion failed (u != 0) in create, file C:\OpenCV30\opencv\sources\modules\core\src\matrix.cpp, line 411
szotsaki commented 8 years ago

After its launch, GrainAutLine uses about 1 200 MB of virtual memory out of 2 GB on Windows 32 bit: image

A simple, 500×500 pixel image occupies 750 000 bytes (RGB). After opening it, OpenCV allocates maximum 9 107 858 bytes, 12-times bigger than the image. Opening a 65 MB image will exceed the 2 GB limit, so x64 compiling will be a must.

Allocating scheme:

Allocate/deallocate [bytes] Currently allocated [bytes]
60 60
40 100
16 116
-40 76
750 000 750 076
-16 750 060
-60 750 000
-750 000 0
60 60
40 100
16 116
-40 76
750 000 750 076
750 000 1 500 076
1 000 000 2 500 076
750 000 3 250 076
750 000 4 000 076
59 643 4 059 719
-16 4 059 703
-60 4 059 643
59 643 4 119 286
79 524 4 198 810
238 572 4 437 382
750 000 5 187 382
1 000 000 6 187 382
-79 524 6 107 858
3 000 000 9 107 858
-238 572 8 869 286