Background
We are dealing with very large TIFF image files that are imported, processed and exported using Mathematica 9.0.1. Many of our algorithms only work with an array representation of the images, where pixel values are stored in the form of 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 to a TIFF file. Then I import the file again with the option "Data"
. I check the memory before and after Import
. Compared to importing the file without any option, this takes up four times more memory.
Export["testFile.tif", ConstantArray[Image[ConstantArray[0, {1024, 1024}], "Bit16"], 50]];
FileByteCount["testFile.tif"]
104901507
MemoryInUse[]
8617736552
imgData = Import["testFile.tif", "Data"];
MemoryInUse[]
9037228760
9037228760 - 8617736552
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 the function 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"]];
System specifictation
- 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.1
- 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?
ImageFileApply
andImageFileFilter
, 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. $\endgroup$Integer
uses a minimum of 8 on a 64-bit system. $\endgroup$