average local variance

11 views (last 30 days)
ismaiel
ismaiel on 3 Mar 2012
Commented: Walter Roberson on 13 Aug 2018
Hi, I need to compute the diagonal error weight matrix (W), where W(i,i) measure the average local variance of pixels with gray level i. Can u please help me regarding this. i will be grateful.
  1 Comment
Oleg Komarov
Oleg Komarov on 3 Mar 2012
Not enough info: http://www.mathworks.com/matlabcentral/answers/6200-tutorial-how-to-ask-a-question-on-answers-and-get-a-fast-answer

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 3 Mar 2012
Again, I'm not sure what this means or if it is really what you want but I think it's what you described.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
fullFileName = fullfile(folder, baseFileName);
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(2, 3, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Calculate the standard deviation image.
sdImage = stdfilt(grayImage);
% Display the image.
subplot(2, 3, 2);
imshow(sdImage, []);
title('Std. Dev. Image', 'FontSize', fontSize);
% Calculate the variance image.
varianceImage = sdImage .^2;
% Display the image.
subplot(2, 3, 3);
imshow(varianceImage, []);
title('Variance Image', 'FontSize', fontSize);
% Calculate the mean of the variance image in a 3x3 region.
% Basically this is a blurred version of it.
meanVarianceImage = conv2(varianceImage, ones(3)/9);
% Display the image.
subplot(2, 3, 4);
imshow(meanVarianceImage, []);
title('Mean Variance Image', 'FontSize', fontSize);
for gl = 0:255
% Find pixels in the original image with this gray level.
matchingIndexes = (grayImage == gl);
% Get the mean of only those pixels from the mean variance image.
W(gl+1) = mean(meanVarianceImage(matchingIndexes));
end
% Plot W
subplot(2, 3, 5);
plot(W);
grid on;
title('Plot of W', 'FontSize', fontSize);
xlabel('Gray Level', 'FontSize', fontSize);
  1 Comment
Walter Roberson
Walter Roberson on 13 Aug 2018
dinial utami comments
I like it thanks

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 3 Mar 2012
I'm not sure what W is. But you can use stdfilt() to get the st.dev. in a local window of an image. Square it to get the variance. If you want the average of the 9 variances of the 9 pixels in a 3x3 neighborhood, then you can use imfilter() or conv2() with a kernel of ones(3)/9. Then you need to go through each gray level, extracting that mean variance, something like (maybe??? untested)
for gl = 0:255
% Find pixels in the original image with this gray level.
matchingIndexes = (imageArray == gl);
% Get the mean of only those pixels from the mean variance image.
W(gl+1) = mean(meanVarianceImage(matchingIndexes ));
end
  2 Comments
ismaiel
ismaiel on 3 Mar 2012
I did not understand what you mean by the following sentence
Then you need to go through each gray level, extracting that mean variance.
Please give an example if possible
note i used function stdfilt(I),where I is an image and the result is zero matrix?why?
Image Analyst
Image Analyst on 3 Mar 2012
No, it's not all zero - you're just not using [] to display in with imshow. See my other answer for a more complete demo.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!