Reducing Memory Usage in ImageTools - Maple Programming Help

Home : Support : Online Help : Graphics : Image Processing : ImageTools Package : ImageTools/reduce_memory

Reducing Memory Usage in ImageTools

 Introduction Because an image is stored as an rtable with datatype = float[8], each pixel in each layer requires eight bytes of memory. The memory allocated for large images can be significant. To reduce memory usage, many of the commands in ImageTools have the options inplace and output that permit reusing previously allocated memory. This help page shows how they can be used.

Options

 • inplace = truefalse
 Specifies whether the operation is performed in-place. The default is false.
 • output = Image
 Specifies a data structure into which the output is written. The size and number of layers must match that of the input. The output image is redimensioned so that its row and column indices match the input. The default is NULL.

Examples

 > $\mathrm{with}\left(\mathrm{ImageTools}\right):$
 > $\mathrm{imgfile}≔\mathrm{cat}\left(\mathrm{kernelopts}\left(\mathrm{datadir}\right),"/images/fjords.jpg"\right):$
 > $\mathrm{img}≔\mathrm{Read}\left(\mathrm{imgfile}\right):$
 > $\mathrm{Embed}\left(\mathrm{img}\right)$

The memory usage for this 480 x 640 x 3 layer image is

 > $\mathrm{size}≔\mathrm{length}\left(\mathrm{img}\right)\mathrm{kernelopts}\left('\mathrm{wordsize}'\right)⟦'\mathrm{bit}'⟧:$
 > $\mathrm{evalf}[2]\left(\mathrm{convert}\left(\mathrm{size},\mathrm{units},\mathrm{mebibyte}\right)\right)$
 ${7.0}{}⟦{\mathrm{mebibyte}}⟧$ (3.1)

Assume that we want to adjust the gamma of layers 2 and 3 of img.  The Gamma command works on all layers, so to do this we need to extract a layer, make the adjustment, and insert the modified layer back into the image.  To reduce memory usage we can allocate a temporary grayscale image that is the size of one layer of the image and then reuse it as needed.  To determine the effect of these options, we compare the memory usage with that when the options are not used.  Assign a procedure that returns the memory allocated by the kernel.

 > $\mathrm{memalloc}≔\left(\right)→\mathrm{evalf}[2]\left(\mathrm{convert}\left(\mathrm{kernelopts}\left('\mathrm{bytesalloc}'\right)⟦'\mathrm{byte}'⟧,\mathrm{units},\mathrm{mebibyte}\right)\right):$

Save the current memory usage.

 > $\mathrm{mem}≔\mathrm{memalloc}\left(\right)$
 ${\mathrm{mem}}{≔}{48.}{}⟦{\mathrm{mebibyte}}⟧$ (3.2)

Create the temporary image.

 > $\mathrm{tmp}≔\mathrm{Create}\left(\mathrm{Height}\left(\mathrm{img}\right),\mathrm{Width}\left(\mathrm{img}\right)\right):$$\mathrm{memalloc}\left(\right)$
 ${51.}{}⟦{\mathrm{mebibyte}}⟧$ (3.3)

Extract layer 2, increase its gamma, then reinsert it, inplace, into the original image.

 > $\mathrm{layer}≔2:$
 > $\mathrm{GetLayer}\left(\mathrm{img},\mathrm{layer},\mathrm{output}=\mathrm{tmp}\right):$$\mathrm{memalloc}\left(\right)$
 ${51.}{}⟦{\mathrm{mebibyte}}⟧$ (3.4)
 > $\mathrm{Gamma}\left(\mathrm{tmp},1.2,\mathrm{inplace}\right):$$\mathrm{memalloc}\left(\right)$
 ${51.}{}⟦{\mathrm{mebibyte}}⟧$ (3.5)
 > $\mathrm{SetLayer}\left(\mathrm{img},\mathrm{tmp},\mathrm{layer},\mathrm{inplace}\right):$$\mathrm{memalloc}\left(\right)$
 ${51.}{}⟦{\mathrm{mebibyte}}⟧$ (3.6)

Do the same with layer 3, but decrease its gamma.

 > $\mathrm{layer}≔3:$
 > $\mathrm{GetLayer}\left(\mathrm{img},\mathrm{layer},\mathrm{output}=\mathrm{tmp}\right):$$\mathrm{memalloc}\left(\right)$
 ${51.}{}⟦{\mathrm{mebibyte}}⟧$ (3.7)
 > $\mathrm{Gamma}\left(\mathrm{tmp},0.8,\mathrm{inplace}\right):$$\mathrm{memalloc}\left(\right)$
 ${51.}{}⟦{\mathrm{mebibyte}}⟧$ (3.8)
 > $\mathrm{SetLayer}\left(\mathrm{img},\mathrm{tmp},\mathrm{layer},\mathrm{inplace}\right):$$\mathrm{memalloc}\left(\right)$
 ${51.}{}⟦{\mathrm{mebibyte}}⟧$ (3.9)

 > $\mathrm{memalloc}\left(\right)-\mathrm{mem}$
 ${3.}{}⟦{\mathrm{mebibyte}}⟧$ (3.10)

Repeat the computation (actually, do its inverse), but use assignment statements rather than inplace and output to assign tmp and img.

 > $\mathrm{mem}≔\mathrm{memalloc}\left(\right)$
 ${\mathrm{mem}}{≔}{51.}{}⟦{\mathrm{mebibyte}}⟧$ (3.11)
 > $\mathrm{layer}≔2:$
 > $\mathrm{tmp}≔\mathrm{GetLayer}\left(\mathrm{img},\mathrm{layer}\right):$$\mathrm{memalloc}\left(\right)$
 ${53.}{}⟦{\mathrm{mebibyte}}⟧$ (3.12)
 > $\mathrm{tmp}≔\mathrm{Gamma}\left(\mathrm{tmp},\frac{1}{1.2}\right):$$\mathrm{memalloc}\left(\right)$
 ${55.}{}⟦{\mathrm{mebibyte}}⟧$ (3.13)
 > $\mathrm{img}≔\mathrm{SetLayer}\left(\mathrm{img},\mathrm{tmp},\mathrm{layer}\right):$$\mathrm{memalloc}\left(\right)$
 ${62.}{}⟦{\mathrm{mebibyte}}⟧$ (3.14)
 > $\mathrm{layer}≔3:$
 > $\mathrm{tmp}≔\mathrm{GetLayer}\left(\mathrm{img},\mathrm{layer}\right):$$\mathrm{memalloc}\left(\right)$
 ${65.}{}⟦{\mathrm{mebibyte}}⟧$ (3.15)
 > $\mathrm{tmp}≔\mathrm{Gamma}\left(\mathrm{tmp},\frac{1}{0.8}\right):$$\mathrm{memalloc}\left(\right)$
 ${53.}{}⟦{\mathrm{mebibyte}}⟧$ (3.16)
 > $\mathrm{img}≔\mathrm{SetLayer}\left(\mathrm{img},\mathrm{tmp},\mathrm{layer}\right):$$\mathrm{memalloc}\left(\right)$
 ${60.}{}⟦{\mathrm{mebibyte}}⟧$ (3.17)
 > $\mathrm{memalloc}\left(\right)-\mathrm{mem}$
 ${9.}{}⟦{\mathrm{mebibyte}}⟧$ (3.18)
 > 

The additional memory allocated is about three times that of the previous method, which used the inplace and output options.