how to store and plot values in a loop?

조회 수: 1 (최근 30일)
Jamal Riaz
Jamal Riaz 2020년 12월 9일
댓글: Jamal Riaz 2020년 12월 10일
The code below currently reads in all the jpg images in a file and outputs the average intensity of the imagearray (sum of image matrix), what I am struggling to do, is convert this code so that it measures the intensity of each singular image, stores the value and then plots the change in intensity across all the images.
ImageFolder = 'H:\My Documents\Dissertation';
if ~isfolder(ImageFolder)
ErrorMessage = print('Error: The following folder does not exist: Please specify a new folder.', ImageFolder);
uiwait(warndlg(ErrorMessage));
ImageFolder = uigetdir();
if ImageFolder == 0
return;
end
end
FileType = fullfile(ImageFolder, '*.jpg');
TheImages = dir(FileType);
for k = 1:length(TheImages)
baseFileName = TheImages(k).name;
fullFileName = fullfile(TheImages(k).folder, baseFileName);
if exist(fullFileName,'file')
imageArray = imread(fullFileName);
end
meanIntensity = mean(imageArray(:));
matrix(k) = mean(imageArray(:));
end
plot(k,meanIntensity,'-.x');
hold on

채택된 답변

Image Analyst
Image Analyst 2020년 12월 9일
Try it like this:
ImageFolder = 'H:\My Documents\Dissertation';
if ~isfolder(ImageFolder)
ErrorMessage = print('Error: The following folder does not exist: Please specify a new folder.', ImageFolder);
uiwait(warndlg(ErrorMessage));
ImageFolder = uigetdir();
if ImageFolder == 0
return;
end
end
filePattern = fullfile(ImageFolder, '*.jpg');
TheImages = dir(filePattern);
for k = 1 : 10% length(TheImages)
baseFileName = TheImages(k).name;
fullFileName = fullfile(TheImages(k).folder, baseFileName);
% if ~isfile(fullFileName,'file') % Will never happen because you used dir()
% continue;
% end
imageArray = imread(fullFileName);
meanIntensity(k) = mean(imageArray(:));
end
plot(meanIntensity,'-.x', 'LineWidth', 2, 'MarkerSize', 16);
hold on
grid on;
title('Image Means', 'FontSize', 18);
xlabel('Index', 'FontSize', 18);
ylabel('Mean Gray Level', 'FontSize', 18);
  댓글 수: 3
Image Analyst
Image Analyst 2020년 12월 10일
When you did this:
meanIntensity = mean(imageArray(:));
matrix(k) = mean(imageArray(:));
end
plot(k,meanIntensity,'-.x');
you assigned the mean intensity to a scalar called "meanIntensity". You also assigned it to an element of "matrix". However you didn't plot the vector called "matrix". You plotted the scalar "meanIntensity", which is just one number that holds the mean intensity of the very last image that was read in. So that plot will only plot a single dot, not the entire list. To plot ALL the mean intensities, you'd have to plot the (poorly-named) "matrix".
Secondly, you read in the image only if it exists, but compute the mean intensity regardless if it exists or not because that was outside the "if". So if the image didn't exist, you'd be measuring the last imageArray that DID exist. However, it's not even needed because if we used dir(), then we'd never get files that don't exist since dir() returns files that only DO exist. Hence the "if" block was not needed. It didn't hurt, but it was not needed either.
Jamal Riaz
Jamal Riaz 2020년 12월 10일
Thanks, You are really are an asset to the MATLAB community :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by