Minimise memory requirements when importing many images

조회 수: 3 (최근 30일)
Steve Francis
Steve Francis 2022년 7월 18일
편집: Catalytic 2022년 7월 19일
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?
Steve Francis
Steve Francis 2022년 7월 18일
Thanks, Adam. I will give this a try.

댓글을 달려면 로그인하십시오.

답변 (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