0

I don't quite understand what line 5 of the following code is doing. First 4 lines are importing the image from the folder and then storing it in "image1". Then on line 5 i dont get what is being done.

numFolder=fullfile('NumberZero/','Zero/');
for i=1:10;
    numName=sprintf('%d.bmp',i);
    image1=imread([numFolder, numName]);
    im1(:,:,i)=image1; % what is this line doing?
end
Recap
  • 168
  • 2
  • 15

2 Answers2

1

That loop is simply loading all of the image data into a variable called im1. The dimensions of this variable are going to be [nRows, nColumns, nImages]. This assumes that the images coming in are actually grayscale rather than RGB (third dimension == 1)

Once this is loaded in, you can then access the different images via the folling approach.

first_image = im1(:,:,1);
second_image = im1(:,:,2);

As a side note, it is recommended to not use i as a loop index.

Community
  • 1
  • 1
Suever
  • 64,497
  • 14
  • 82
  • 101
0

I posted this question on the "MATLAB Central" Q&A and "Image Analyst" answered my question very nicely.

The line

im1(:,:,i)=image1;

takes a 2D image called image1 and sticks it into the i'th slice (plane) of a 3D image called im1. If im1 already has i slices, then it just overwrites the i'th slice. If im1 does not yet have i slices, then this code will append a slice to grow the 3D image in the "Z" direction. So it's turning 2D images stored on your disk into a 3D image. The images1's must be grayscale for this code to work.

Recap
  • 168
  • 2
  • 15
  • Just curious, what caused this to be better than my answer below? – Suever Mar 13 '16 at 16:42
  • @Suever sorry its just that for me this explanation was more understandable. But i will mark yours as if you need it :P – Recap Mar 13 '16 at 22:32
  • 1
    It's not a problem at all. I'm just always trying to learn what makes one answer better than another! – Suever Mar 13 '16 at 22:37