Minimise memory requirements when importing many images

3 vues (au cours des 30 derniers jours)
Steve Francis
Steve Francis le 18 Juil 2022
Modifié(e) : Catalytic le 19 Juil 2022
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;
end
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).
  2 commentaires
Adam Danz
Adam Danz le 18 Juil 2022
Modifié(e) : Adam Danz le 18 Juil 2022
Some ideas
  1. Use single instead of double.
  2. Since you're doing element-wise division, do you need to load all 430 images into the same array? Can you load image 1 from cube1 and then load image 1 from cube2, and then store the result of division, then get rid of the two image data sets and repeat for the next pair of images?
Steve Francis
Steve Francis le 18 Juil 2022
Thanks, Adam. I will give this a try.

Connectez-vous pour commenter.

Réponses (1)

Catalytic
Catalytic le 19 Juil 2022
Modifié(e) : Catalytic le 19 Juil 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);
image3(~isfinite(image3))=0;
cube(:,:,k)=image3;
end

Catégories

En savoir plus sur Images dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by