GreycLab/CImg

Failed to allocate memory

Closed this issue · 6 comments

KirbX commented

Hello everybody.
Actually, I was wondering about the error : Cimg::CImg(): Failed to allocate memory(797.3Mio) for image ...
I open an image using cimg_library::CImg imgIn("1.jpg");
then i create a new image cimg_library::CImg imgOut;
and use a function that return an image and initialize it by using CImg ret(inSize_width, inSize_height,1,3); (with inSize_width and inSize_height the size of the image given in parameters). But i keep having this error.
Do you know why?

What is the size of the image ? How many RAM do you have ?
Are you on a 32bits or 64bits system ?
What are the values of inSize_width and inSize_height ?

KirbX commented

The image is 11804/5902 (width and height), size 76.5Mo.
I have 4Go RAM and i'm on a 64bits system.
Oh, can it be the source of my problems, my computer doesn't have enough to run the script? Knowing that i'm using a colored image, I create too many channels?

You are dealing with a quite big image. For an image 11804x5902 in RGB, if you load it as a float-valued image (so 4bytes / value), you'll end up indeed with a memory usage of 797 Mb to store your single image. Your file is only 76.5Mo, because the image data are stored in a compressed way.
So, if you need to work on one or two copies of this image, the available RAM will be quickly filled.

If you don't need a float-valued precision for the image, store it in a CImg<unsigned char> instead of a CImg<float> (which is the default), you'll get a gain of x3 on memory usage.

KirbX commented

Wouaw! You are very helpful and fast! Thanks a lot!
just to know : is there a way to copy a pixel (all 3 channels) from an image to another image? Is
SetXY do this?
If you have to search for it, don't answer me, i'll search myself! Anyway! Thanks again!

I'd do this way :

cimg_forC(dest,c) dest(x,y,c) = src(x,y,c);
KirbX commented

Ok! I'll give it a shot! thanks for all!
Edit :
In fact, i have to use iterator so I have to use something like (cause i work with these)

for (i ...){ for (j...){ }}