how to call multiple binary images in for loop?
조회 수: 1 (최근 30일)
이전 댓글 표시
i am doing black pixel count in 9 images , so i created a for loop to perform count on each image one after other , please help me how to call 9 images one after other
댓글 수: 0
답변 (3개)
AJ von Alt
2014년 1월 21일
편집: AJ von Alt
2014년 1월 21일
A cell array would be your best bet. Read each image in and store it as an element of the cell array. You can then iterate through the array in a loop.
Here's a quick example:
% Read in images and store them in a cell array
imarray{1} = imread('trees.tif');
imarray{2} = imread('trees.tif');
imarray{3} = imread('trees.tif');
imarray{4} = imread('trees.tif');
imarray{5} = imread('trees.tif');
imarray{6} = imread('trees.tif');
for i = 1:6
% load each image and operate on it.
currImage = imarray{i};
% your code here
end
댓글 수: 0
Amit
2014년 1월 21일
A = {'Image1.jpg','image2.jpg' ..... etc 'image9.jpg'}; % Store the names of all 9 image files.
for i = 1:9
B = imread(A{i});
_ Do you operation _
end
댓글 수: 0
Image Analyst
2014년 1월 21일
See the FAQ for code samples: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F. I don't think you need to create cell arrays to store the images, like the others suggested, unless you want to use them later for some reason.
myFolder = 'C:\Documents and Settings\yourUserName\My Documents\My Pictures';
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.jpg');
jpegFiles = dir(filePattern);
for k = 1:length(jpegFiles)
baseFileName = jpegFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray = imread(fullFileName);
% Note, imageArray is not a cell array, and is not being saved after the loop ends.
imshow(imageArray); % Display image.
drawnow; % Force display to update immediately.
% Now binarize the image and count black pixels.
binaryImage = imageArray > thresholdValue; % which can be whatever value you want.
sumOfBlackPixels(k) = sum(~binaryImage(:));
end
Or, alternatively if you already have your 9 binary images (which is not very many), you can simply do it in 9 lines of code
sumOfBlackPixels(1) = sum(~binaryImage1(:));
sumOfBlackPixels(2) = sum(~binaryImage2(:));
sumOfBlackPixels(3) = sum(~binaryImage3(:));
sumOfBlackPixels(4) = sum(~binaryImage4(:));
sumOfBlackPixels(5) = sum(~binaryImage5(:));
sumOfBlackPixels(6) = sum(~binaryImage6(:));
sumOfBlackPixels(7) = sum(~binaryImage7(:));
sumOfBlackPixels(8) = sum(~binaryImage8(:));
sumOfBlackPixels(9) = sum(~binaryImage9(:));
댓글 수: 2
Image Analyst
2014년 1월 21일
Do you already have the 9 images before the for loop would start? If so, then you can't unless you load them into a cell array first (like amit and AJ showed you) and then do the for loop, which seems like more lines of code and an unnecessary complication. If you have some variable number of images - not always 9 - then you'll have to read them in in a for loop like the FAQ code above. But like I said, I wouldn't bother with a for loop unless you needed the images in some code after the for loop has finished. But if you do all your processing in the for loop, then no cells are needed.
See the FAQ for a good discussion of cells: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!