Minimise memory requirements when importing many images

2 views (last 30 days)
I am using imread to read in 430 monochrome (16-bit) images of resolution 1024x1280. I apply a common mask to each image, which is a 1024x1280 logical array. I also dispose of any pixels with values outside the range 50-60000.
I'm storing the processed images as layers of a 3-d array:
cube1 = zeros(1024,1280,430);
for k=1:430
image = double(imread (myImageFiles(k).name)));
image(~mask) = NaN;
image(image<50 | image>60000) = NaN; % replace 'bad' pixels outside limits with NaN
cube1(:,:,k) = image;
The image is cast as a double because I want to replace certain pixels with NaNs. If i just read in as uint16 then these pixels are stored as zeros. I also want to create a second cube in the same way and divide the elements in cube1 by the corresponding elements in cube2.
newCube = cube1./cube2;
However, I am coming up against 'out of memory' issues (system has 8GB RAM). I can reduce memory by not casting as a double but lose the NaN feature. Please could someone advise on a way to tackle this? I've tried saving as a .mat file, but each cube array is over 4GB (before any compression).

Sign in to comment.

Answers (1)

Catalytic on 19 Jul 2022
Edited: Catalytic on 19 Jul 2022
Why is it better to use NaNs instead of zeros? A value of 0 doesn't conflict with anything because all of your nontrivial values lie between 50 and 60000.
cube = zeros(1024,1280,430,'single');
for k=1:430
image1 = imread (myFiles1(k).name));
image2 = imread (myFiles2(k).name));
image1(image1<50 | image1>60000) = 0;
image2(image2<50 | image2>60000) = 0;
image3 = single(image1)./single(image2);


Find more on Convert Image Type in Help Center and File Exchange




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by