How to load multiple images and processing them?

140 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Image Analyst
Image Analyst il 6 Feb 2013
Modificato: Image Analyst il 6 Feb 2013
% 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 Commenti
Tasneem Tabassum
Tasneem Tabassum il 4 Apr 2017
Modificato: Tasneem Tabassum il 4 Apr 2017
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 il 4 Apr 2017
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

Accedi per commentare.

Più risposte (1)

Youssef  Khmou
Youssef Khmou il 6 Feb 2013
Modificato: Youssef Khmou il 6 Feb 2013
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 .

Categorie

Scopri di più su Image Processing Toolbox in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by