MATLAB Answers

# How to read multiple jpg images in a loop?

405 views (last 30 days)
archana on 4 Mar 2014
Commented: Pooja Thillai on 10 Jun 2020
hello, i am a novice user in matlab my question is: i have stored some 100s of images in some folder and i want to read it one-by-one.how can i do this?

#### 15 Comments

SHUBHDEEP KAUR on 12 Sep 2018
the following code shows single image at a time ..how to display all the jpg images at once
Anil Varma on 13 Dec 2019
showing error
Error using randi
First input must be a positive scalar integer value IMAX, or two integer values [IMIN IMAX] with IMIN
less than or equal to IMAX.
Error in test (line 9)
idx=randi(n);
Pooja Thillai on 10 Jun 2020
images read simultaneously????

Sign in to comment.

### Accepted Answer

Dishant Arora on 4 Mar 2014

#### 6 Comments

Show 3 older comments
Dishant Arora on 6 Mar 2014
What do you want to do?? compression or resizing?? The two are completely different things. If you want to resize you are definitely going to loose some data, whereas compression can be both lossless or lossy depending upon the algorithm. I guess what you want is resize not compression. And convert it to grayscale first.
archana on 6 Mar 2014
sir,actually i want to obtain the covarience matrix of all these read images...for same i wrote a code but there i worked on each image seperatly (i.e. reading convrting to gray and the resized it) because of which code became verry bulky ,now with your guidlines i modified sem for loop reading. now what i supposed to do is 1st i should convert it to gray and then resize all..but fact is yet i didnot reached to covarience matrix stage...:( apologize me for mistake if any
Dishant Arora on 6 Mar 2014
Use the builtin cov to get the covariance matrix.

Sign in to comment.

### More Answers (6)

Karthik Karri on 6 Mar 2014
You can rename all images like..im1,im2,im3.....im100. To read 100 images one by one, run a loop 100 times with i as variable from 1 to 100 and in imread, m=imread(['folder_path\im' num2str(i) '.jpg']). I hope I answered your question.

#### 1 Comment

archana on 6 Mar 2014
thanks its working.....:)

Sign in to comment.

chaitra naveen on 20 Mar 2017
can you please help me out to read 100's of images from folder to matlab and resize them to 100x100.

#### 1 Comment

Image Analyst on 4 May 2017
Use imresize in a loop like the loops given in the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F

Sign in to comment.

Muhammad Sohail on 27 Nov 2017
filelist = dir('Training data');
for i=1 : length(filelist)
for i=1 : length(filelist)
filename = filelist(i);
if ~strcmp(filename.name , '.') && ~strcmp(filename.name , '..')
filename.name
end
end

#### 1 Comment

Haseeb Hassan on 20 Mar 2019
Simple and easy

Sign in to comment.

Haseeb Hassan on 30 Jun 2019
for i=1:16; % we have 16 images we have in or folder
clc;clear;
images ='C:\Users\Haseeb\Desktop\input\for_loop_input';
jpgfiles=dir(fullfile(images,'\*.jpg*'));
n=numel(jpgfiles(i));
im=jpgfiles(i).name
im1=imread(fullfile(images,im));
imshow(im1);
end
Here is the simple and short code, which will acces your images from your folder in a sequence.

#### 1 Comment

shital shinde on 21 Feb 2020
Actually it gives error as
Array indices must be positive integers or logical values.
n=numel(jpgfiles(i)); // for this line.
will you tell me why ?

Sign in to comment.

Xylo on 6 Mar 2014
You can do this easily..... before doing this program, you have to keep all of your image in a folder and rename them by any common char+number concatenation form (like a1.jpg, a2.jpg,a3.jpg....). after that you just use strcat() function for reading the path name, and then read the images.
for i=1:10 %suppose there are 10 image
file_name=dir(strcat('J:\multimedia\photo\')); % the path tht u hv imges
im=imread(strcat('J:\multimedia\photo\',file_name(i).name));
imtool(im);
end

#### 8 Comments

Show 5 older comments
Walter Roberson on 29 Nov 2016
Isha Pandya comments to Omar Ahmad:
I am not able to solve this problem. Please help.
Walter Roberson on 29 Nov 2016
Isha Pandya, please show your current code and describe what difficulty you are observing.
Zahoor abbas on 9 Jul 2018
How I can access the images from two folders by using one for loop

Sign in to comment.

Gebra maryam Alehegn on 13 May 2017
Question Please everyone how to extract color and texture features from multiple image and prepare for as traning?

#### 1 Comment

Walter Roberson on 13 May 2017
We have given a few links above on how to process sets of files. So you just have to take your existing functions that extract color and texture features and put a loop around them.

Sign in to comment.