MATLAB: Average local variance

compute the diagonal error weight matrix

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.

Best Answer

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);
Related Question