how to load a folder of images in variables using for loop and arrays ?

 Respuesta aceptada

Stephen23
Stephen23 el 11 de Feb. de 2015
Editada: Stephen23 el 11 de Feb. de 2015
This is dealt with quite thoroughly in the MATLAB Wiki:
Do not try to create a separate dynamically named variable for each file. Save them in a cell array or structure instead:

6 comentarios

sir,im a beginner of matlab can you please give a sample code for my question
Stephen23
Stephen23 el 11 de Feb. de 2015
Editada: Stephen23 el 11 de Feb. de 2015
If you took the time to look at both of those links you will find example code there, complete with notes and explanations.
If your files are not sequentially numbered, then you will have to use some other method to determine their names: using dir or a hand-written cell array of filenames are two possibilities. This is also explained in the Wiki (scroll down to the next paragraph).
If you are a beginner you should take the time to learn some MATLAB code, and work through the tutorials:
well,my files are sequently numbered .
sir, i have loaded all these images in a varaible but with this its not done.i need to create variable to each image . myFolder = 'e:\\invertedimages'; if ~isdir(myFolder) errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder); uiwait(warndlg(errorMessage)); return; end filePattern = fullfile(myFolder, '*.bmp'); bmpFiles = dir(filePattern); for k = 1:length(bmpFiles) baseFileName = bmpFiles(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 this is how i coded all images are loaded in imagearray.i have to create imagearray1 for image1 and so on
Stephen23
Stephen23 el 11 de Feb. de 2015
Editada: Stephen23 el 11 de Feb. de 2015
When you post code here on this forum, please format it correctly: you can use the {} Code button that you will find above the text box, and check the preview panel below the text box, which shows what your text will look like, to confirm that it is neat and tidy.
Please edit your comment to make your code readable.
You can try this code, it saves the data of every .BMP image in the given directory the cell array imageArray, and displays them too:
myFolder = 'e:\\invertedimages';
filePattern = fullfile(myFolder, '*.bmp');
bmpFiles = dir(filePattern);
for k = length(bmpFiles):-1:1
baseFileName = bmpFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
imageArray{k} = imread(fullFileName);
imshow(imageArray{k});
%drawnow; % I don't know if this is actually required.
end
You write "i have to create imagearray1 for image1 and so on", to which I will repeat what I said in my original answer: DO NOT create a separate variable for each of your input files. Save the data in a cell array , like in my code above.
Question: Is there a reason why you choose the dir-based code, rather than the sequentially-numbered filename code?
if true
myFolder = 'e:\\invertedimages';
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.bmp');
bmpFiles = dir(filePattern);
for k = 1:length(bmpFiles)
baseFileName = bmpFiles(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
end

Iniciar sesión para comentar.

Más respuestas (2)

Hi, in similar cases I use strings... Here is the code, which I prepare for you. I hope it will help!
%Example code for reading 10 *.bmp files named as it follows: 1.bmp, 2.bmp.......... 10.bmp
%You should be (or go) into the correct directory which contains the files
for PicNum=1:1:10;
fullFileName=strcat(num2str(PicNum),'.bmp');
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray = imread(fullFileName);
imshow(imageArray); % Display image.
drawnow; % Force display to update immediately.
end
You should only rename your *.bmp files (if you are allowed to do it).
Good luck!
Stephen23
Stephen23 el 11 de Feb. de 2015
Editada: Stephen23 el 11 de Feb. de 2015
Use this code if you have a known sequence of filenames. It saves all of the image data in a cell array too:
myBase = 'image'; % selects the base filename: 'image1.bmp','image2.bmp', etc.
myFolder = 'e:\\invertedimages';
for PicNum = 10:-1:1;
fullFileName = fullfile(myFolder,sprintf('%s%.0f.bmp',myBase,PicNum));
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray{PicNum} = imread(fullFileName);
imshow(imageArray{PicNum}); % Display image.
end

3 comentarios

the imageArray{1} stores the value of one image right. I need to create logical type variable of 50*50 so that at output ie excel sheet should contain value of that image
The cell array imageArray stores all image data from every image that is loaded by this function. Each image is located in one cell of the cell array: imageArray{k}. The data is in a numeric array: the exact dimensions of each numeric array depend on the size of the image and its color-encoding.
"I need to create logical type variable of 50*50 ..." : this seems to be a different topic to your original question, which all of the answers given here have tried to resolve for you. If you wish to discuss a new topic, you should ask this as a new question, or even better do a little bit of your own research first:
neha, are you using imageArray{1} like you said rather than imageArray{k} or imageArray{PicNum} like Stephen told you here and I told you in your duplicate question???
And your second question is totally different, and not even answerable because it does not say how you'd like to convert the color or gray level image into a logical image (like by thresholding or whatever).

Iniciar sesión para comentar.

Categorías

Más información sobre Convert Image Type en Centro de ayuda y File Exchange.

Preguntada:

el 11 de Feb. de 2015

Comentada:

el 11 de Feb. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by