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 Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by