[BV] Added variance function

Signed-off-by: Jim Martens <github@2martens.de>
This commit is contained in:
Jim Martens 2016-11-01 15:05:21 +01:00
parent de4654c1d7
commit 10a9e654ab
1 changed files with 31 additions and 0 deletions

31
bv/variance.m Normal file
View File

@ -0,0 +1,31 @@
function [v] = variance( image )
%VARIANCE Calculates the variance of an image
if isinteger(image)
error(message('MATLAB:var:integerClass'));
end
% prepare some values needed for both mean and variance
n = 0;
mean = 0.0;
M2 = 0.0;
s = size(image);
rows = s(1);
cols = s(2);
for x = 1:cols
for y = 1:rows
n = n + 1;
delta = image(y, x) - mean;
mean = mean + delta/n;
M2 = M2 + delta*(image(y,x) - mean);
end
end
if n < 2
v = float('nan');
else
v = M2 / (n - 1);
end
end