Effacer les filtres
Effacer les filtres

Calculate the coefficient of variation of many images

5 vues (au cours des 30 derniers jours)
Patrice Gaofei
Patrice Gaofei le 15 Déc 2020
Commenté : Patrice Gaofei le 16 Déc 2020
Hello everyone,
I am not very familiar with Matlab. I am having many images that are stored in a directory. I am trying to calculate the coefficient of variation for each of the images, and then compute the mean or average so as to obtain the result for the entire dataset. I have made used of a For loop. However, there seems to be something incorrect. Only the value of the last image is returned. Given my low programming skills, I cannot figure out the problem. Any comments and suggestions would be highly appreciated. My code sample is as follows.
COV = 0;
for k = 1:5%length(Inputs)
img = imread([data Inputs{k}]);
Dev = std(double(img(:)));
%Mn = mean(img);
Mn = mean(img(:));
C = Dev / Mn;
COV =+ C;
M = mean(COV);
disp(M);
end

Réponse acceptée

Image Analyst
Image Analyst le 15 Déc 2020
See attached code. Change the file extension to whatever you're using: jpg, png, tiff, bmp, etc. If it works for youi, then please "Accept this answer" (or you could wait for other answers first if you want).
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 22;
fprintf('Beginning to run %s.m ...\n', mfilename);
% Specify the folder where the files live.
myFolder = pwd; % or 'C:\Users\yourUserName\Documents\My Pictures'; or wherever you want.
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.bmp'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
numImages = length(theFiles);
allMeans = zeros(1, numImages);
allSD = zeros(1, numImages);
cov = zeros(1, numImages);
for k = 1 : numImages
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
%fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
imageArray = imread(fullFileName);
subplot(2, 2, 1);
imshow(imageArray); % Display image.
caption = sprintf('Image #%d of %d : %s', k, numImages, baseFileName);
title(caption, 'FontSize', fontSize);
if k == 1
% Maximize figure window.
g = gcf;
g.WindowState = 'maximized'
end
% Compute and plot the mean
allMeans(k) = mean(imageArray(:));
subplot(2, 2, 2);
plot(allMeans, 'b-', 'LineWidth', 2);
title('Means', 'FontSize', fontSize);
xlabel('Image #', 'FontSize', fontSize);
ylabel('Mean Gray Level', 'FontSize', fontSize);
grid on;
% Compute and plot the standard deviation
allSD(k) = std(single(imageArray(:)));
subplot(2, 2, 3);
plot(allSD, 'b-', 'LineWidth', 2);
title('Standard Deviation', 'FontSize', fontSize);
xlabel('Image #', 'FontSize', fontSize);
ylabel('Standard Deviation in Gray Levels', 'FontSize', fontSize);
grid on;
% Compute and plot the COV
cov(k) = allMeans(k) ./ allSD(k);
subplot(2, 2, 4);
plot(cov, 'b-', 'LineWidth', 2);
title('Coefficient of Variation', 'FontSize', fontSize);
xlabel('Image #', 'FontSize', fontSize);
ylabel('COV', 'FontSize', fontSize);
grid on;
fprintf('For image #%d of %d ("%s"), the mean = %.3f, the StDev = %.3f, and the COV = %.3f.\n',...
k, numImages, baseFileName, allMeans(k), allSD(k), cov(k));
drawnow; % Force display to update immediately.
end
%=========================================================================================
% Compute the mean and std dev of the whole collection
% Compute the mean of the means
meanMean = mean(allMeans);
% Compute the mean of the standard deviations
meanSD = mean(allSD);
% Compute the mean of the COV
meanCov = mean(cov);
% Compute the SD of the means
sdMean = std(allMeans);
% Compute the SD of the standard deviations
sdSD = std(allSD);
% Compute the SD of the COV
sdCov = std(cov);
fprintf('-----------------------------------------------\nMeans of all %d images:\n', numImages);
fprintf(' of means = %.3f\n of SDs = %.3f\n of COVs = %.3f\n', meanMean, meanSD, meanCov);
fprintf('-----------------------------------------------\nStandard Deviations of all %d images:\n', numImages);
fprintf(' of means = %.3f\n of SDs = %.3f\n of COVs = %.3f\n', sdMean, sdSD, sdCov);
msgbox('Done! Check out the command window for results.');
In the command window you'll see something like:
-----------------------------------------------
Means of all 10 images:
of means = 94.887
of SDs = 52.608
of COVs = 2.392
-----------------------------------------------
Standard Deviations of all 10 images:
of means = 83.424
of SDs = 41.919
of COVs = 2.667
  4 commentaires
Image Analyst
Image Analyst le 16 Déc 2020
Well, you're getting confused as to what "original" and "restoration" mean.
If you have an image, let's call it image2, and it has noise and you remove the noise and produce image3, then cov3 will be less than cov2. But that won't be as close to the original as if the cov was closer to cov2. That's because you're defining the "original" as the noisy image2. So a cov3 that's closer to the cov2 will look more like the original, noisy image.
Now, what if the original is an image BEFORE you added noise to it?
Now if you had image1 and added noise to it and created image2, then removed noise and created image3, then cov3 should be closer to cov1 and look more like the noise-free image1 than image2 does. So if you're defining the "original" as the noise-free version, then the closer cov3 gets to cov1 the more "realistic/genuine" it will look. But in that case you're defining image1 as the original. In the first case where you just have 2 images, a noisy one and a denoised one, then you want the final cov3 to be far lower than cov2 because the "original" was the bad/noisy version.
Let's take an example. Let's say you have only a noisy image2 and a denoised image3, and cov2 = .6. Now if you did a good job denoising, cov3 might b2 0.3. So it's lower and the lower you make it by removing more noise and details, and the image become blurred or has uniform patches, then the less it will look liike the original noisy image.
Now let's say you have an image1 with cov1=0.1, then you add noise to get image2 with cov2 = 0.6, and then you denoise image2 to get image3 with cov3 = 0.15. Now image3 probably looks a lot more like image1 than noisy image2 does. So as the cov3 gets closer to cov1 and farther from cov2 then the more "original" it looks.
So you see, it just depends on how you define original.
I hope you followed that. Reread it if you get confused. It's kind of detailed.
Patrice Gaofei
Patrice Gaofei le 16 Déc 2020
Yes sir, thank you very much for the detailed explanation. I have gotten it much clearer now.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Image Data Workflows dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by