MATLAB Answers

Storing calculated data in loop

1 view (last 30 days)
Hello everyone!
I need some assistance here.
I've calculated amounts of white pixels from an image, now i wanna store that value and then continue to the next image.
Here is my unfinished loop code:
How to store data from that numWhitePixel fucntion for multiple images?
image_folder = '/Volumes/Untitled/MATLAB copy/Foam_Experiments_inj_pressure/V3 @300mbar/2019-11-21_10-31-53'; % Enter name of folder from which you want to upload pictures with full path
filenames = dir(fullfile(image_folder, '*.jpg')); % read all images with specified extention, its jpg in our case
total_images = numel(filenames); % count total number of photos present in that folder
for n = 1:total_images
f= fullfile(image_folder, filenames(n).name); % it will specify images names with full path and extension
our_images = imread(f); % Read images
figure (n) % used tat index n so old figures are not over written by new new figures
J = imrotate(our_images,90,'bilinear','loose');
%imshow(J);
%p=ginput(2)
K=imcrop(J,[0.5 1220.5 5504 5810]);
%imshow(K);
red=K(:,:,1);
green=K(:,:,2);
blue=K(:,:,3);
%
% % Define thresholds for channel 1 based on histogram settings
channel1Min = 225.000;
channel1Max = 255.000;
%
% % Define thresholds for channel 2 based on histogram settings
channel2Min = 210.000;
channel2Max = 252.000;
%
% % Define thresholds for channel 3 based on histogram settings
channel3Min = 210.000;
channel3Max = 255.000;
% % Create mask based on chosen histogram thresholds
BW = (K(:,:,1) >= channel1Min ) & (K(:,:,1) <= channel1Max) & ...
(K(:,:,2) >= channel2Min ) & (K(:,:,2) <= channel2Max) & ...
(K(:,:,3) >= channel3Min ) & (K(:,:,3) <= channel3Max);
% % % Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
%fill=imfill(BW,'holes');
morph=bwmorph(BW,'erode');
dilate=bwmorph(morph,'dilate',1);
BW2 = bwareaopen(dilate,20);
numWhitePixels = numel(BW2)
end
Thank You!

  0 Comments

Sign in to comment.

Accepted Answer

David Hill
David Hill on 8 Jun 2020
Just index it with your for-loop:
numWhitePixels(n) = numel(BW2);

  1 Comment

Ravin Rayeok
Ravin Rayeok on 8 Jun 2020
well... didn't think about that. Thanks a lot !

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!