how to load a folder of images in variables using for loop and arrays ?
8 次查看(过去 30 天)
显示 更早的评论
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
采纳的回答
Stephen23
2015-2-11
编辑:Stephen23
2015-2-11
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 个评论
Stephen23
2015-2-11
编辑:Stephen23
2015-2-11
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?
更多回答(2 个)
Sevil Ahmed
2015-2-11
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!
0 个评论
Stephen23
2015-2-11
编辑:Stephen23
2015-2-11
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 个评论
Stephen23
2015-2-11
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
2015-2-11
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).
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!