source: birds/moments.m @ 141

Last change on this file since 141 was 74, checked in by dtax, 11 years ago

More useful features for images.

  • Property svn:executable set to *
File size: 1.5 KB
Line 
1% M = MOMENTS (IM, P, Q, CENTRAL, SCALED)
2%
3% Calculates moments of order (P+Q) (can be arrays of indentical length)
4% on image IM. If CENTRAL is set to 1 (default: 0), returns translation-
5% invariant moments; if SCALED is set to 1 (default: 0), returns scale-
6% invariant moments.
7%
8% After: M. Sonka et al., Image processing, analysis and machine vision.
9
10function m = moments (im,p,q,central,scaled)
11
12        if (nargin < 5), scaled = 0;    end;
13        if (nargin < 4), central = 0; end;
14  if (nargin < 3)
15        error ('Insufficient number of parameters.');
16  end;
17   
18        if (length(p) ~= length(q))
19        error ('Arrays P and Q should have equal length.');
20  end;
21   
22  if (scaled & ~central)
23        error ('Scale-invariant moments should always be central.');
24  end;
25
26  % xx, yy are grids with co-ordinates
27  [xs,ys] = size(im);
28  [xx,yy] = meshgrid(-(ys-1)/2:1:(ys-1)/2,-(xs-1)/2:1:(xs-1)/2);
29   
30        if (central)
31     
32        % Calculate zeroth and first order moments
33          m00 = sum(sum(im));
34          m10 = sum(sum(im.*xx));
35          m01 = sum(sum(im.*yy));
36     
37    % This gives the center of gravity
38    xc  = m10/m00;
39    yc  = m01/m00;
40     
41    % Subtract this from the grids to center the object
42    xx  = xx - xc;
43    yy  = yy - yc;
44     
45  end;
46   
47  % Calculate moment(s) (p,q).
48  for i = 1:length(p)
49                m(i) = sum(sum((xx.^p(i)).*(yy.^q(i)).*im));
50  end;
51   
52  if (scaled)
53     
54        c = 1 + (p+q)/2;
55     
56    % m00 should be known, as scaled moments are always central
57    m = m ./ (m00.^c);
58     
59        end;
60             
61return;
Note: See TracBrowser for help on using the repository browser.