Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Read Multiple Images from a Folder

Subject: Read Multiple Images from a Folder

From: Nehal

Date: 29 Aug, 2010 21:06:03

Message: 1 of 5

I have 1500 images in a folder called "Sample" in MATLAB folder. So how can I read 1500 images one by one and make some operations on them? I know I need a for loop, but I cannot think a better way to do this.

I can read multiple images from MATLAB folder using this code...

tifFiles = dir('*.tif');
for k = 1:length(tifFiles)
filename = tifFiles(k).name;
I = imread(filename);
% operations on "I"
end

But if I want to read all the images from a folder called "Sample", what should be my code..?

Subject: Read Multiple Images from a Folder

From: dpb

Date: 29 Aug, 2010 21:16:24

Message: 2 of 5

Nehal wrote:
> I have 1500 images in a folder called "Sample" in MATLAB folder. So how
> can I read 1500 images one by one and make some operations on them? I
> know I need a for loop, but I cannot think a better way to do this.
>
> I can read multiple images from MATLAB folder using this code...
>
> tifFiles = dir('*.tif'); for k = 1:length(tifFiles)
> filename = tifFiles(k).name;
> I = imread(filename);
> % operations on "I"
> end
>
> But if I want to read all the images from a folder called "Sample", what
> should be my code..?

Either include the directory name sotoo

filename = ['sample\' tifFiles(k).name]; % salt to suit...

or you could change the working directory to the one containing the files.

Or you might find

doc fullpath % and friends

useful

--

Subject: Read Multiple Images from a Folder

From: Walter Roberson

Date: 29 Aug, 2010 21:24:35

Message: 3 of 5

On 29/08/10 4:06 PM, Nehal wrote:
> I have 1500 images in a folder called "Sample" in MATLAB folder. So how
> can I read 1500 images one by one and make some operations on them? I
> know I need a for loop, but I cannot think a better way to do this.
>
> I can read multiple images from MATLAB folder using this code...
>
> tifFiles = dir('*.tif'); for k = 1:length(tifFiles)
> filename = tifFiles(k).name;
> I = imread(filename);
> % operations on "I"
> end
>
> But if I want to read all the images from a folder called "Sample", what
> should be my code..?

The code outline remains very similar to what you had already:

sdirectory = 'Sample';
tifffiles = dir([sdirectory '/*.tif']);
for k = 1:length(tifffiles)
filename = [sdirectory '/' tifffiles(k).name];
I = imread(filename);
% operations on "I"
end

The major thing to note here is that dir() does not retain any directory
information in the .name field of the structure, so if you dir()
something other than the current directory you have to add back in the
directory name when you want to operate on the file.

Subject: Read Multiple Images from a Folder

From: Nehal

Date: 30 Aug, 2010 07:41:05

Message: 4 of 5

Walter Roberson <roberson@hushmail.com> wrote in message <neAeo.2092$rC7.1689@newsfe10.iad>...
> On 29/08/10 4:06 PM, Nehal wrote:
> > I have 1500 images in a folder called "Sample" in MATLAB folder. So how
> > can I read 1500 images one by one and make some operations on them? I
> > know I need a for loop, but I cannot think a better way to do this.
> >
> > I can read multiple images from MATLAB folder using this code...
> >
> > tifFiles = dir('*.tif'); for k = 1:length(tifFiles)
> > filename = tifFiles(k).name;
> > I = imread(filename);
> > % operations on "I"
> > end
> >
> > But if I want to read all the images from a folder called "Sample", what
> > should be my code..?
>
> The code outline remains very similar to what you had already:
>
> sdirectory = 'Sample';
> tifffiles = dir([sdirectory '/*.tif']);
> for k = 1:length(tifffiles)
> filename = [sdirectory '/' tifffiles(k).name];
> I = imread(filename);
> % operations on "I"
> end
>
> The major thing to note here is that dir() does not retain any directory
> information in the .name field of the structure, so if you dir()
> something other than the current directory you have to add back in the
> directory name when you want to operate on the file.

Thank You.

Subject: Read Multiple Images from a Folder

From: Fa Fa

Date: 9 Jul, 2012 10:42:07

Message: 5 of 5

Hi,i have 21image and i like to read it at the same time but i can read only from 1 to 9 and then stop can somebody help?the name of the images is anpage.1 to 20.

for i=1:21
    if i<20
         t(i,:)= ['anpage.' num2str(i) '.jpg'];
         filenamee=['C:\Users\houssam\Documents\MATLAB\DP\faces94\female\anpage\' t(i,:)]
           
        I=imread(filenamee);
end
        

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us