Background
We are dealing with very large TIFF image files that are imported, processed and exported using Mathematica 9. Many of our algorithms only work with an array representation of the images, where pixel values are stored in nested lists. For this, we are currently using the function ImageData to directly convert images into an array representation.
Problem
I am struggeling with the increase in memory usage when converting images to the array representation using ImageData. What I am wondering about is that when I convert images with ImageData the allocated memory is four times higher compared to having only the images in memory.
Example
Consider the following example. I first create a stack of 50 images that is exported in a TIFF file. Then I import the file again with the option "Data". I check the memory before and after the import. Compared to importing the file without any option, this takes up four times more memory.
In[31]:= Export["testFile.tif", ConstantArray[Image[ConstantArray[0, {1024, 1024}], "Bit16"], 50]];
In[32]:= FileByteCount["testFile.tif"]
Out[32]= 104901507
In[33]:= MemoryInUse[]
Out[33]= 8617736552
In[34]:= imgData = Import["testFile.tif", "Data"];
In[35]:= MemoryInUse[]
Out[35]= 9037228760
In[36]:= 9037228760 - 8617736552
Out[36]= 419492208
If I import the file without the option, the memory used is about 104905128 bytes. This gives a factor of about 4 (3.99) more memory needed. I repeated this several times and I have to admit I do not have any explanation.
Questions
- Is there a simple explanation for the increase of memory usage?
- How can I reduce the amount of allocated memory when using the array representation?
Attempts
After reading about Developer`ToPackedArray in What is a Mathematica packed array? I tried to convert the data array hoping that this would give me a decrease in memory usage. Nevertheless, I am not really sure if I am using the function correctly in this case. The code I tried is the following:
packed = Developer`ToPackedArray[Import["testFile.tif", "Data"]];
Thanks in advance for any help!
Edit
OS: Windows 7 Professional, Service Pack 1 (64-bit) Processor: Intel Xeon CPU E5630 @ 2.53 GHz (2 Processors) Installed memory: 96 GB Mathematica 9.0
Java version 1.7.0_07
Additional question
Still one problem remains: Is their a clean way in Mathematica, how I could reduce the number of bits used for Integer to change the amount of memory used for the array representation (ImageData) without losing information? Or do I have to stick to Image in that case?



ImageFileApplyandImageFileFilter, which are "used to read, process, and write successive blocks of data from a very large image file whose size could exceed available memory", according to WRI. – cormullion Jan 10 at 13:05Integeruses a minimum of 8 on a 64-bit system. – Simon Woods Jan 10 at 14:33