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
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?
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 Exchange에서 Image 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