Please explain difference in image variance algorithms ... E-10 for all tests
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
% You may need to load your own image or one of Matlab's demo images, like cameraman
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
M2a = zeros(nrows,ncols,nzs);
M1a = zeros(nrows,ncols,nzs);
varImA = zeros(nrows,ncols,nzs);
M2b = zeros(nrows,ncols,nzs);
M1b = zeros(nrows,ncols,nzs);
varImB = zeros(nrows,ncols,nzs);
M2c = zeros(nrows,ncols,nzs);
M1c = zeros(nrows,ncols,nzs);
varImC = zeros(nrows,ncols,nzs);
imgSq = img.^2;
%Algorithm #1
M2a = imfilter(imgSq, h/n1 , 'symmetric');
M1a = imfilter(img, h, 'symmetric').^2 / (n*n1);
varImA = (max((M2a - M1a),0));
%Algorithm #2
M2b = imfilter(imgSq, h/n1 , 'symmetric');
M1b = imfilter(img, h/n, 'symmetric');
M1S = (M1b.^2)*n/n1;
varImB = max((M2b - M1S),0);
%Algorithm #3
for i=1:1:nzs;
M2c(:,:,i) = filter2(h, imgSq(:,:,i)) / n;
end
for i=1:1:nzs;
M1c(:,:,i) = filter2(h, img(:,:,i)) / n;
end
varImC = n/n1.*(M2c-M1c.^2);
% Test the difference (need to crop image borders to get relevant results)
Test1 = varImA(20:end-20,20:end-20,:) - varImB(20:end-20,20:end-20,:);
Test2 = varImA(20:end-20,20:end-20,:) - varImC(20:end-20,20:end-20,:);
Test3 = varImB(20:end-20,20:end-20,:) - varImC(20:end-20,20:end-20,:);
% Display results;
max(Test1(:))
min(Test1(:))
max(Test2(:))
min(Test2(:))
max(Test3(:))
min(Test3(:))
1 commentaire
Jan
le 3 Mai 2014
Loading cameraman.tif replies an integer matrix, such that it sufficient as test data. There are some typos in "VarImB" and "VarImC", which both need a lowercase "v".
Réponses (1)
Jan
le 3 Mai 2014
Modifié(e) : Jan
le 3 Mai 2014
When I use img = rand(256, 256, 3) as input data and fix the typos, I get differences of the magnitude 6.6613e-16. This is 3*eps, which means, that the differences are very tiny. Such differences are caused by the limited precision of the representation of numbers. See FAQ: Why is 0.3-0.2-0.1 not equal to 0.
Voir également
Catégories
En savoir plus sur Image Processing Toolbox 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!