How to load multiple images and processing them?

Hi!!!
i've been looking for some info about loading "n" images with a script but i havent find anything...
well my idea was create a function with a for loop that changes the index of the image to load, in that way if the firts image its called "1.jpg" the program will load and process the images 'till it reaches a variable called "m" that is the total of images in my directory....
well i've tried like this a lot of times but i cant find the way to make it work. :( so if anyone can help me i will be realy grateful

 採用された回答

Image Analyst
Image Analyst 2013 年 2 月 6 日
編集済み: Image Analyst 2013 年 2 月 6 日

6 投票

% Read files file1.txt through file20.txt, mat1.mat through mat20.mat
% and image1.jpg through image20.jpg. Files are in the current directory.
for k = 1:20
matFilename = sprintf('mat%d.mat', k);
matData = load(matFilename);
jpgFilename = strcat('image', num2str(k), '.jpg');
imageData = imread(jpgFilename);
textFilename = ['file' num2str(k) '.txt'];
fid = fopen(textFilename, 'rt');
textData = fread(fid);
fclose(fid);
end
A very slight adaptation gives you:
% Read 1.jpg through m.jpg.
% Files are in the "yourFolder" directory.
for k = 1:m
jpgFilename = sprintf('%d.jpg', k);
fullFileName = fullfile(yourFolder, jpgFilename);
if exist(fullFileName, 'file')
imageData = imread(fullFileName );
else
warningMessage = sprintf('Warning: image file does not exist:\n%s', fullFileName);
uiwait(warndlg(warningMessage));
end
imshow(imageData);
end
Note how I make it more robust by using exist() to check for the file's existence before attempting to show it, and how I make it more flexible by allowing you to specify that the files live in some particular folder "yourFolder" rather than living in the current folder only.
However if you want to do all the image files in the folder, then use the second chunk of code in the FAQ. That will also eliminate the need to use exist() because dir() will only give you files that are known to exist. However, if you want to be alerted if a file is missing, then you could still use the part of the first example to warn you of a missing file.

5 件のコメント

Jesus
Jesus 2013 年 2 月 7 日
移動済み: DGM 2022 年 12 月 20 日
ive done what u told me and it works!!!
thanks for answering my already answered question, i promise i will check up the FAQ's before ask any question :)
its for my vision systems class by the way im a beginner
Image Analyst
Image Analyst 2013 年 2 月 7 日
移動済み: DGM 2022 年 12 月 20 日
You should have made a comment to my answer instead of posting it as an Answer in itself. Glad the FAQ helped. There is more interesting stuff in there so make sure you look over the rest of it. Since it works, go ahead and mark my answer as "Accepted".
-Image Analyst
sangeetha jagadish
sangeetha jagadish 2016 年 10 月 19 日
that was really usefull.. and it worked... thanks for help..
Tasneem Tabassum
Tasneem Tabassum 2017 年 4 月 4 日
編集済み: Tasneem Tabassum 2017 年 4 月 4 日
I have a question. i have searched a lot but in vain. what if the series of images i want to read doesn't start from the initial image position available?
Image Analyst
Image Analyst 2017 年 4 月 4 日
I don't know what that means. Start your own, new question and give an example of what filenames you have.
By the way, you can start and stop your for loop wherever you want if you know numbers in advance, for example:
for k = 13 : 157

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

その他の回答 (1 件)

Youssef  Khmou
Youssef Khmou 2013 年 2 月 6 日
編集済み: Youssef Khmou 2013 年 2 月 6 日

2 投票

Hi, i saw a similar question before, you can find the answer by searching , anyway :
Suppose your images are "image1.jpg", "image2.jpg",...,"imagem.jpg" :
1.You get the size of sample .
2.You initialize a container .
3.You read through a loop.
I=imread('image1.jpg');
[r n p]=size(I); % Your Images are either 2D or 3D
Manifold=zeros(r,n,p,m); % 3D with singleton or 4D
for x=1:m
filename=strcat('image',num2str(x),'.jpg');
Manifold(:,:,:,x)=imread(filename);
end
Now you can blindly check if the images are gray-scale or 3d :
if p==1
Manifold=squeeze(Manifold); % you delete the singleton dimension
end
Just an addition , with singleton 4D Manifold, there is a command with which you can show the whole images in one figure , i just do not remember it .

カテゴリ

ヘルプ センター および File ExchangeImage Processing Toolbox についてさらに検索

質問済み:

2013 年 2 月 6 日

移動済み:

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