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

15 views (last 30 days)
Im working on dip.I have a folder of binary images and i want to create variable to each image using arrays and for loop concept

Accepted Answer

Stephen23
Stephen23 on 11 Feb 2015
Edited: Stephen23 on 11 Feb 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 Comments
Stephen23
Stephen23 on 11 Feb 2015
Edited: Stephen23 on 11 Feb 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?
neha viswanath
neha viswanath on 11 Feb 2015
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

Sign in to comment.

More Answers (2)

Sevil Ahmed
Sevil Ahmed on 11 Feb 2015
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 on 11 Feb 2015
Edited: Stephen23 on 11 Feb 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 Comments
Stephen23
Stephen23 on 11 Feb 2015
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:
Image Analyst
Image Analyst on 11 Feb 2015
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).

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!