-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstatmoments.m
59 lines (50 loc) · 1.72 KB
/
statmoments.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
function [v, unv] = statmoments(p, n)
%STATMOMENTS Computes statistical central moments of image histogram.
% [W, UNV] = STATMOMENTS(P, N) computes up to the Nth statistical
% central moment of a histogram whose components are in vector
% P. The length of P must equal 256 or 65536.
%
% The program outputs a vector V with V(1) = mean, V(2) = variance,
% V(3) = 3rd moment, . . . V(N) = Nth central moment. The random
% variable values are normalized to the range [0, 1], so all
% moments also are in this range.
%
% The program also outputs a vector UNV containing the same moments
% as V, but using un-normalized random variable values (e.g., 0 to
% 255 if length(P) = 2^8). For example, if length(P) = 256 and V(1)
% = 0.5, then UNV(1) would have the value UNV(1) = 127.5 (half of
% the [0 255] range).
% Copyright 2002-2004 R. C. Gonzalez, R. E. Woods, & S. L. Eddins
% Digital Image Processing Using MATLAB, Prentice-Hall, 2004
% $Revision: 1.3 $ $Date: 2003/05/24 22:43:02 $
Lp = length(p);
if (Lp ~= 256) & (Lp ~= 65536)
error('P must be a 256- or 65536-element vector.');
end
G = Lp - 1;
% Make sure the histogram has unit area, and convert it to a
% column vector.
p = p/sum(p); p = p(:);
% Form a vector of all the possible values of the
% random variable.
z = 0:G;
% Now normalize the z's to the range [0, 1].
z = z./G;
% The mean.
m = z*p;
% Center random variables about the mean.
z = z - m;
% Compute the central moments.
v = zeros(1, n);
v(1) = m;
for j = 2:n
v(j) = (z.^j)*p;
end
if nargout > 1
% Compute the uncentralized moments.
unv = zeros(1, n);
unv(1)=m.*G;
for j = 2:n
unv(j) = ((z*G).^j)*p;
end
end