Please explain difference in results between these variance algorithms which should be null?
显示 更早的评论
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;
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Image Arithmetic 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!