how to load a folder of images in variables using for loop and arrays ?
조회 수: 13 (최근 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에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!