akira hinoshiro
akira hinoshiro

Reputation: 401

read 2d grey images and combine them to 3d matrix

I have some problems with matlab 2015a Win10 X64 16GB Ram.

There is a bunch of images (1280x960x8bit) and I want to load them into a 3d matrix. In theory that matrix should store ~1.2GB for 1001 images.

What I have so far is:

values(:,:,:)= zeros(960, 1280, 1001, 'uint8');
for i = Start:Steps:End
    file = strcat(folderStr, filenameStr, num2str(i), '.png');
    img = imread(file);
    values(:,:,i-Start+1) = img;
end

This code is working for small amount of images but using it for all 1001 images I get "Out of memory" error. Another problem is the speed. Reading 50 images and save them takes me ~2s, reading 100 images takes ~48s.

What I thought this method does is allocating the memory and change the "z-elements" of the matrix picture by picture. But obviously it holds more memory than needed to perform that single task.

Is there any method I can store the grey values of a 2d picture sequence to a 3d matrix in matlab without wasting that much time and ressources?

Thank you

Upvotes: 1

Views: 70

Answers (1)

Ander Biguri
Ander Biguri

Reputation: 35525

The only possibility I can see is that your idexes are bad. But I can only guess because the values of start step and End are not given. If End is 100000000, Start is 1 and step is 100000000, you are only reading 2 images, but you are accessing values(:,:,100000000) thus making the variable incredibly huge. That is, most likely, your problem.

To solve this, create a new variable:

imagenames=Start:Step:End; %note that using End as a variable sucks, better ending
for ii=1:numel(imagenames);
    file = strcat(folderStr, filenameStr, num2str(imagenames(ii)), '.png');
    img = imread(file);
    values(:,:,ii) = img;
end   

As Shai suggests, have a look to fullfile for better filename accessing

Upvotes: 1

Related Questions