How to read multiple jpg images in a loop?

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?

17 件のコメント

Xylo
Xylo 2014 年 3 月 6 日
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
anish m r
anish m r 2015 年 3 月 3 日
hi while doing this i am getting an error like Error using imread (line 366) Can't open file "E:\Image." for reading; you may not have read permission.
Error in Untitled (line 3) im=imread(strcat('E:\Image',file_name(i).name));
pls help me to solve this
Thanke
Walter Roberson
Walter Roberson 2016 年 11 月 29 日
im=imread( fullfile('E:\Image', file_name(i).name ) );
Yogesh Bhandari
Yogesh Bhandari 2017 年 3 月 10 日
編集済み: Image Analyst 2017 年 3 月 10 日
Hi, I am trying to implement similar code of reading images and later try to randomly select one of the images. However, while performing the reading of images, I am getting an error :Cannot open file "\\ad.uillinois.edu\engr\instructional\yogeshb2\Desktop\Researchdata\Test case for image extraction\ImaGES_FOR_QUESTIONS." for reading. You might not have read permission.
Code Snippet
for i=1:15
file_name=dir(strcat('\\ad.uillinois.edu\engr\instructional\yogeshb2\Desktop\Researchdata\Test case for image extraction\ImaGES_FOR_QUESTIONS'));
im=imread(strcat('\\ad.uillinois.edu\engr\instructional\yogeshb2\Desktop\Researchdata\Test case for image extraction\ImaGES_FOR_QUESTIONS',file_name(i).name));
imtool(im)
end
if possible could you please guide me through this.
Walter Roberson
Walter Roberson 2017 年 3 月 10 日
Do not use strcat to create your file name. Use fullfile().
Image Analyst
Image Analyst 2017 年 3 月 10 日
Yogesh, perhaps this but I'm getting zero files found there:
folder = '//ad.uillinois.edu\engr\instructional\yogeshb2\Desktop\Researchdata\Test case for image extraction\ImaGES_FOR_QUESTIONS'
files = dir(fullfile(folder, '*.*'))
for k=1:length(files)
thisFileName = files(k).name
im=imread(thisFileName);
imshow(im);
drawnow;
end
Yogesh Bhandari
Yogesh Bhandari 2017 年 3 月 20 日
編集済み: Yogesh Bhandari 2017 年 3 月 20 日
Thank you for your comments. However, I slightly modified my code and have attached the updated code.
images=imageSet('\\ad.uillinois.edu\engr\instructional\yogeshb2\Desktop\Rese archdata\Test case for image extraction\ImaGES_FOR_QUESTIONS');
n=numel(images.ImageLocation);
idx=randi(n);
im=imread(images.ImageLocation(idx).jpg);
But, it still am giving the following error
Struct contents reference from a non-struct array object.
Error in codeResearch2 (line 8)
im=imread(images.ImageLocation(idx).jpg);
could you please help me through this.
Walter Roberson
Walter Roberson 2017 年 3 月 20 日
編集済み: Walter Roberson 2017 年 3 月 20 日
im = read(images(1), idx);
Yogesh Bhandari
Yogesh Bhandari 2017 年 3 月 20 日
編集済み: Yogesh Bhandari 2017 年 3 月 20 日
Dear Walter, Thank you it works. However could you please explain the reason for this.
Is it something related to the fact that its 1-D array having row fixed and the number of columns would be changing
Walter Roberson
Walter Roberson 2017 年 3 月 20 日
The ImageLocation property is not the list of images: it is the list of directories in which the images are to be found.
Indexing the imageset at (1) talks about the first directory out of the list. read() with an index is what is used to imread() in the individual images from the directory.
Yogesh Bhandari
Yogesh Bhandari 2017 年 3 月 20 日
編集済み: Yogesh Bhandari 2017 年 3 月 20 日
Thank You Walter, for your writing detailed response. However based on your previous comments I prepared another code
images='\\ad.uillinois.edu\engr\instructional\yogeshb2\Desktop\Researchdata\Test case for image extraction\ImaGES_FOR_QUESTIONS';
jpgfiles=dir(fullfile(images,'\*.jpg*'))
n=numel(jpgfiles);
idx=randi(n);
im=imread(jpgfiles(idx).name)
However, I am receiving the error
Error using imread (line 349)
File "a1.jpg" does not exist
Error in codeResearch2 (line 6)
im=imread(jpgfiles(idx).name)
Yogesh Bhandari
Yogesh Bhandari 2017 年 3 月 21 日
I guess I resolve the query following reference from your other post. (PS:thanks) Following is the code snippet.
images ='\\ad.uillinois.edu\engr\instructional\yogeshb2\Desktop\Researchdata\Test case for image extraction\ImaGES_FOR_QUESTIONS';
jpgfiles=dir(fullfile(images,'\*.jpg*'))
n=numel(jpgfiles);
idx=randi(n);
im=jpgfiles(idx).name
im1=imread(fullfile(images,im))
imshow(im1)
Gebra maryam Alehegn
Gebra maryam Alehegn 2017 年 5 月 13 日
移動済み: DGM 2022 年 12 月 20 日
Question Please everyone how to extract color and texture features from multiple image and prepare for as traning?
Walter Roberson
Walter Roberson 2017 年 5 月 13 日
移動済み: DGM 2022 年 12 月 20 日
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.
SHUBHDEEP KAUR
SHUBHDEEP KAUR 2018 年 9 月 12 日
the following code shows single image at a time ..how to display all the jpg images at once
Anil Varma
Anil Varma 2019 年 12 月 13 日
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);
shweta banait
shweta banait 2020 年 12 月 3 日
移動済み: DGM 2022 年 12 月 20 日
how to display five images in matlab

サインインしてコメントする。

 採用された回答

Dishant Arora
Dishant Arora 2014 年 3 月 4 日

1 投票

6 件のコメント

archana
archana 2014 年 3 月 6 日
編集済み: archana 2014 年 3 月 6 日
thank you sir...! now can you please tell me how can i convert all these images to gray and make all of them of same sizes for further purpose..
Dishant Arora
Dishant Arora 2014 年 3 月 6 日
use imresize and rgb2gray.
archana
archana 2014 年 3 月 6 日
編集済み: archana 2014 年 3 月 6 日
sir, i wrote this code for reading images
myFolder = 'C:\Users\shree\Desktop\final data\1'; if ~isdir(myFolder) errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder); uiwait(warndlg(errorMessage)); return; end filePattern = fullfile(myFolder, '*.jpg'); jpegFiles = dir(filePattern); for k = 1:length(jpegFiles) baseFileName = jpegFiles(k).name; fullFileName = fullfile(myFolder, baseFileName); %fprintf(1, 'Now reading %s\n', fullFileName); imageArray = imread(fullFileName); imshow(imageArray); % Display image. drawnow; % Force display to update immediately. end
now i want to convert it to gray and then make them of same size by compressing them.i also want to ask you what should i do 1st should i compress 1st and then convert to gray or what i must not lose any features of images..
Dishant Arora
Dishant Arora 2014 年 3 月 6 日
編集済み: Dishant Arora 2014 年 3 月 6 日
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
archana 2014 年 3 月 6 日
編集済み: archana 2014 年 3 月 6 日
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
Dishant Arora 2014 年 3 月 6 日
Use the builtin cov to get the covariance matrix.

サインインしてコメントする。

その他の回答 (5 件)

Karthik Karri
Karthik Karri 2014 年 3 月 6 日

3 投票

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 件のコメント

archana
archana 2014 年 3 月 6 日
thanks its working.....:)

サインインしてコメントする。

chaitra naveen
chaitra naveen 2017 年 3 月 20 日

1 投票

can you please help me out to read 100's of images from folder to matlab and resize them to 100x100.
Muhammad Sohail
Muhammad Sohail 2017 年 11 月 27 日

1 投票

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
Haseeb Hassan
Haseeb Hassan 2019 年 6 月 30 日

1 投票

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 件のコメント

shital shinde
shital shinde 2020 年 2 月 21 日
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 ?

サインインしてコメントする。

Xylo
Xylo 2014 年 3 月 6 日

0 投票

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 件のコメント

archana
archana 2014 年 3 月 7 日
heyy thanks for answering now can you please tell me how can i store the image immidiatly after reading it by some name like g1,g2,g3,jpg.....as i want to do some operation on these all images..... regards....:)
Xylo
Xylo 2014 年 3 月 11 日
ya..... offcrse u can. just use imwrite function to save. like this:- imwrite(im,'your path address','name1','jpg'); here name1:-name of the that you want to save
Xylo
Xylo 2014 年 3 月 11 日
and use this code in the for loop, that is after im=imread(...) code
Omar Ahmad
Omar Ahmad 2014 年 12 月 8 日
Xylo sir,
I pasted the path very correctly in the im=imread.... code but it gives me an error saying you may not have read permission. I even changed the path to another drive..but same error.. why does that occur? plz help
Omar Ahmad
Omar Ahmad 2014 年 12 月 8 日
I got the above problem right. I have one more:
How can I subtract and find the image difference one from another. e.g. from gray_2.jpg and gray_1.jpg? I mean once reading these, how can we separate them into individual images?
Walter Roberson
Walter Roberson 2016 年 11 月 29 日
Isha Pandya comments to Omar Ahmad:
I am not able to solve this problem. Please help.
Walter Roberson
Walter Roberson 2016 年 11 月 29 日
Isha Pandya, please show your current code and describe what difficulty you are observing.
Zahoor abbas
Zahoor abbas 2018 年 7 月 9 日
How I can access the images from two folders by using one for loop

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2014 年 3 月 4 日

移動済み:

DGM
2022 年 12 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by