how to store and plot values in a loop?
11 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jamal Riaz
am 9 Dez. 2020
Kommentiert: Jamal Riaz
am 10 Dez. 2020
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
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 9 Dez. 2020
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 Kommentare
Image Analyst
am 10 Dez. 2020
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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu MATLAB Mobile finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!