Minimise memory requirements when importing many images

3 次查看(过去 30 天)
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 个评论
Adam Danz
Adam Danz 2022-7-18
编辑:Adam Danz 2022-7-18
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?

请先登录,再进行评论。

回答(1 个)

Catalytic
Catalytic 2022-7-19
编辑:Catalytic 2022-7-19
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

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

标签

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by