how to call multiple binary images in for loop?

3 次查看(过去 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

回答(3 个)

AJ von Alt
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

Amit
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

Image Analyst
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 个评论
nitesh patil
nitesh patil 2014-1-21
is it possible to perform alternative method using for loop instead 9 lines of sum operation?
Image Analyst
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 CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by