The Math Forum

Search All of the Math Forum:

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

Math Forum » Discussions » Software » comp.soft-sys.matlab

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

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]

Posts: 107
Registered: 2/27/10
Re: Indexing Images from For Loop Import
Posted: Jun 6, 2012 2:48 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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 " <> wrote in message <jqnpsn$813$>...
> 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);
> I = imread(fileNames(1).name); %Prod Doc used {1} instead of (1)
> sequence = zeros([size(I) numFrames], class(I));
> sequence(:,:,:,1) = I; %added the necessary dimension
> for p =2:numFrames
> sequence(:,:,:,p) = imread(fileNames(p).name);
> 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 " <> wrote in message <jqnnjj$qbt$>...

> > Thanks for the response. I found another example on the MathWorks Product Documentation site ( 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" <> wrote in message <jqllm7$2jk$>...

> > > "Zachary " <> wrote in message <jqlcuq$l6c$>...
> > > > 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:
> > > >
> > > > 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.

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.