MATLAB Answers

How to read images in a folder

1,665 views (last 30 days)
I am a student and I need to find the code to read the images in a file in order to do cropping. In here I named my images as
user001-01.bmp
user001-02.bmp etc.
I need help for this because I'm a beginner to MATLAB.
  5 Comments

Sign in to comment.

Accepted Answer

Michelle Hirsch
Michelle Hirsch on 25 Jan 2011
Nilushi -
We won't be able to hand you a complete answer, but can give some pointers. Here's a code snippet that shows common patterns for reading data from files. There are a bunch of concepts you are going to want to learn about to understand this code:
  • Working with structures. When you get a list of image files, their names will be stored inside of a structure.
  • Working with cell arrays. We'll use a cell array to store the data coming from the different images, since it allows for each image to be a different size. If you happen to know that all of your images are exactly the same size you can use a regular numeric array instead.
% Get list of all BMP files in this directory
% DIR returns as a structure array. You will need to use () and . to get
% the file names.
imagefiles = dir('*.bmp');
nfiles = length(imagefiles); % Number of files found
for ii=1:nfiles
currentfilename = imagefiles(ii).name;
currentimage = imread(currentfilename);
images{ii} = currentimage;
end
Best of luck! - scott
  9 Comments
Hasaan Ijaz
Hasaan Ijaz on 12 Sep 2020
Worked for me as well. Thank you!

Sign in to comment.

More Answers (5)

Sebastian
Sebastian on 25 Jan 2011
There might be different ways to do this. An easy one is
for a = 1:10
filename = ['user001-' num2str(a,'%02d') '.bmp'];
img = imread(filename);
% do something with img
end
  4 Comments
Liu Sam
Liu Sam on 4 Mar 2018
Thank you! This is the exact answer I want!

Sign in to comment.


Walter Roberson
Walter Roberson on 25 Jan 2011
Please check the FAQ on this topic
  1 Comment
Nilushi Dissanayake
Nilushi Dissanayake on 25 Jan 2011
thank you very much,,

Sign in to comment.


George
George on 24 Sep 2016
You can try ImageDatastore
  3 Comments
Walter Roberson
Walter Roberson on 11 Sep 2017
Before R2015a, the closest equivalent was the dataset array from the Statistics toolbox.

Sign in to comment.


Paul De
Paul De on 9 Nov 2017
THANK YOu! I've been looking for a long time the solution of this problem)

Ashraf Rayed
Ashraf Rayed on 12 May 2020
okay it is awesoe coding. Thanks a lot. I have a question here, look here we will read some pictures of a folder and we will get some output also of those pictures. Can we save those data sequentially in a xcel sheet??

Tags

No tags entered yet.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!