Please explain difference in results between these variance algorithms which should be null?
Show older comments
Can someone please explain why these different algorithms are giving slightly different results for the variance of an image?
%References:
- http://en.wikipedia.org/wiki/Variance#Formulae_for_the_variance
- http://www.mathworks.com/help/images/ref/stdfilt.html
- http://mathworld.wolfram.com/Variance.html
% Load images and get basic parameters
load('img.mat')
nrows=size(img,1);
ncols=size(img,2);
nzs=size(img,3);
h = ones(7);
n = sum(h(:));
n1 = n - 1;
%Preinitialize
conv1=zeros(nrows,ncols,nzs);
conv2=zeros(nrows,ncols,nzs);
varIm=zeros(nrows,ncols,nzs);
En=zeros(nrows,ncols,nzs);
Mn=zeros(nrows,ncols,nzs);
Vn=zeros(nrows,ncols,nzs);
gImgSq=mat2gray(Img.^2);
gImg=mat2gray(Img);
%Algorithm #1
conv1 = imfilter(gImgSq, h/n1 , 'symmetric');
conv2 = imfilter(gImg, h, 'symmetric').^2 / (n*n1);
VarIm = (max((conv1 - conv2),0));
%Algorithm #2
for i=1:1:nzs;
En(:,:,i)=filter2(h, gImgSq(:,:,i)) / n;
end
for i=1:1:nzs;
Mn(:,:,i)=filter2(h, gImg(:,:,i)) / n;
end
Vn = n/n1.*(En-Mn.^2);
%Test the difference
Test= VarIm - Vn;
Accepted Answer
More Answers (0)
Categories
Find more on Image Arithmetic in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!