Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Indexing Images from For Loop Import
Replies: 4   Last Post: Jun 6, 2012 2:48 PM

 Messages: [ Previous | Next ]
 Jeff Posts: 107 Registered: 2/27/10
Re: Indexing Images from For Loop Import
Posted: Jun 6, 2012 2:48 PM

If your images are truly grayscale, you can collapse them from 3 dimensions to 2. There are many ways to check this, but a nice interactive way is using imtool's Pixel Region functionality.

The reason for this is that imread will interpret some image formats (i.e. tiff, jpg) as RGB images even when they are intended to be intensity images. They are represented as a mxnx3 dimension array, with the third dimension containing red, green and blue intensity values...which should be identical if they are grayscale images.

In your code below, you can do this with something like the following:
sequence(:,:,1) = I(:,:,1);

"Zachary " <beachbumz123@yahoo.com> wrote in message <jqnpsn\$813\$1@newscl01ah.mathworks.com>...
> Hi Guys
>
> I just want to let you all know that I solved my problem. I needed another dimension in my array "sequence". Below is the code I modified from MathWorks Product Documentation "Working with Image Sequences". I spent a while on this ever simple task (since I am a beginner). I hope this helps anyone who can't get their images to read into MATLAB and for anyone who wants all their images organized into an array.
>
> The point of my code is to read in all the image files (which for me are the same size) into an array. If the images were not the same size, I think you would need to use cells.
>
> fileNames = dir('*.tif'); %I did this just because I am working in my current dir
> numFrames = numel(fileNames);
> sequence = zeros([size(I) numFrames], class(I));
> sequence(:,:,:,1) = I; %added the necessary dimension
> for p =2:numFrames
> end
>
> Now you can access each individual image by recalling sequence(:,:,:,1), sequence(:,:,:,2), etc.
>
> Let me know if you guys see anywhere I can improve. See you guys around.
>
> Thanks
> Zach
>
>
>
> "Zachary " <beachbumz123@yahoo.com> wrote in message <jqnnjj\$qbt\$1@newscl01ah.mathworks.com>...

> > Thanks for the response. I found another example on the MathWorks Product Documentation site (http://www.mathworks.com/help/toolbox/images/f14-17682.html). I have tried using their example to processing image sequences, but I am having no luck. When I use their code on my directory of gray-scale.tif images I come up with an error (cell contents reference from non-cell array obj. So I changed the fileNames{1}.name to fileNames(1).name. After that I get an error Assignment has more non-singleton rhs dimensions than non-singleton subscripts. I do not know what to do to fix this error. Below is the code.
> >
> > fileNames = dir('*.tif');
> > numFrames = numel(fileNames);
> > I = imread(fileNames{1}.name); %I changed the {1} to (1)
> > sequence = zeros([size(I) numFrames],class(I));
> > sequence(:,:,1) = I;
> >
> > Thanks for your help.
> > Zach
> >
> > "Jeff EA" <jea@gene.dot.com> wrote in message <jqllm7\$2jk\$1@newscl01ah.mathworks.com>...

> > > "Zachary " <beachbumz123@yahoo.com> wrote in message <jqlcuq\$l6c\$1@newscl01ah.mathworks.com>...
> > > > Hi
> > > >
> > > > I am struggling with a simple code where I want to index all of the images I am importing from the directory so that I can manipulate each image individually. I read that MATLAB imread stores the image in an array; however, I am struggling with accessing the images. I have tried using both cat() and cell() with no luck. I am using the same code as in the FAQ to bring the pictures into MATLAB. How can I access each image from imageArray? Note: I have tried using imshow(imageArray(2)), but it gives me the same image as imshow(imageArray(1))
> > > >
> > > > Link to FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
> > > >
> > > > Thanks
> > > > Z

> > > In that FAQ, the images are being read in one at a time, and not stored as a multidimensional array. imageArray is overwritten every time the loop is run. To access a specific image you need to run all the code in that loop, including setting jpegFiles(k).name, where k = the index of the image you want to access.
> > >
> > > If you want to store all the images, you will probably want to use a structure (i.e. images(k).image = imread(fullFileName); ). If you can guarantee all the images have the same dimensions, then an array would work also.

Date Subject Author
6/5/12 Zachary
6/5/12 Jeff
6/6/12 Zachary
6/6/12 Zachary
6/6/12 Jeff