Simple Image Analysis by Moments
Simple Image Analysis by Moments
Version 0.2∗
Maintained by: Johannes Kilian
March 15, 2001
Abstract
This document is a preliminary version of a manual on using moments to
analyze the form of objects in an image. Only moments up to second order
are used to get simple features of the object, like center of gravity, semi-axes,
orientation and eccentricity of the object ellipsis.
If you have comments or can contribute to this document, please mail J. Kilian
c
°This document is freely distributable.
Contents
1 General remarks on moments 1
1.1 Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Order of moments . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Spatial, central and central normalized moments . . . . . . . . . 3
2 Moments in OpenCV 4
1
Z Z
mp,q = xp y q f (x, y)dxdy (1)
The integration is calculated over the area of the object. Generally each
other pixel based feature instead of the gray value could be used to calculate
the moments of the object.
Using binary images the gray value function f (x, y) becomes
1 Object
f (x, y) = b(x, y) = { (2)
0 Background
and can be neglected in the subsequent formulas.
The first order moments contain information about the center of gravity
of the object;
m1,0
xc =
m0,0
m0,1
yc = (5)
m0,0
• ...
2
1.3 Spatial, central and central normalized moments
Formula (1) describes general spatial moments of the object.
From the spatial moments the central moments can be derived by reducing
the spatial moments with the center of gravity (xc , yc ) of the object, so all the
the central moments refer to the center of gravity of the object. Expressed as
formula the central moments are calculated as follows
Z Z
µp,q = (x − xc )p (y − yc )q f (x, y)dxdy (7)
µ0,0 = m0,0
µ1,0 = µ0,1 = 0 (8)
The central moments of first or higher order can directly be derived from
the spatial moments by
µ ¶p µ ¶q
mp,q m1,0 m0,1
µp,q = − ∗ (9)
m0,0 m0,0 m0,0
Using formula (9) the central moments of first and second order can be
derived from spatial moments as follows:
µ ¶
m1,0 m1,0
µ1,0 = − =0
m0,0 m0,0
µ ¶
m0,1 m0,1
µ0,1 = − =0
m0,0 m0,0
µ ¶2
m2,0 m1,0 m2,0
µ2,0 = − = − x2c (10)
m0,0 m0,0 m0,0
µ ¶2
m0,2 m0,1 m0,2
µ0,2 = − = − yc2
m0,0 m0,0 m0,0
µ ¶ µ ¶
m1,1 m1,0 m0,1 m1,1
µ1,1 = − ∗ = − xc ∗ yc
m0,0 m0,0 m0,0 m0,0
where xc , yc is the center of gravity of the object (see formula (5)).
The main advantage of central moments is their invariancy to translations
of the object. Therefore they are suited well to describe the form of the object.
A disadvantage of the spatial and central moments is their dependency on
the size of the object. This is disturbing, when trying to compare objects, which
are looked at from different distances. To allow a comparison of those objects,
scaling of the moments is necessary. Normally the area A of the object is used
as a scaling factor. Dividing the central moments µp,q with powers of A we get
central normalized moments νp,q .
µp,q
νp,q = p+q (11)
+1
m0,02
The main advantage of normalized moments is their invariancy to the size
of the object.
3
2 Moments in OpenCV
As a data structure to maintain the moments the moment state structure
CvMoments is defined in file CvTypes.h as follows:
4
It has to be considered that the moments calculated in OpenCV do have
negative values, because cvContourMoments() is affected by the contour orien-
tation of OpenCV. Because contours are retrieved in mirrored coordinate system
(origin is top-left, y-axis is oriented downward), all the contours do have clock-
wise orientation and thus do have negative area according to Green’s formula.
From this fact result negative moments with clockwise oriented contours.
Errors in cvContourMoments(): Up to version Nov 08 2000 there are se-
vere errors in calculating the central moments, but the spatial moments are
calculated correctly. For this reason, use formula (9) to determine the central
moments correctly! Also the sign of the spatial moment is determined wrongly
(it is positive in each case and should be negative in some cases (with clock-
wise oriented contours)). This errors should be corrected in version later than
version Nov 08 2000
A = m0,0 (12)
OpenCV offers a separate function cvContourArea() to calculate the area
of the object bounded by the contour, but it could also retrieved from member
m00 of the struct CVMoments.
m1,0 m1,0
xc = =
A m0,0
m0,1 m0,1
yc = = (13)
A m0,0
5
y
y’
a b
x
Θ
x’
3.3.2 Orientation θ
The orientation of the object is defined as the tilt angle between the x-axes and
the axis, around which the object can be rotated with minimal inertia (i.e. the
direction of the major semi-axis a). This corresponds to the eigenvector with
minimal eigenvalue. In this direction the object has its biggest extension. It is
calculated as follows:
6
1 2µ1,1
θ= arctan (16)
2 µ2,0 − µ0,2
The tilt angle θ is illustrated in figure (1).
There is an ambiguity in the tilt angle θ of the object which can be resolved
by choosing θ always to be the angle between the x-axis and the semimajor axis
a, i.e. by definition a ≥ b
Secondly, we pick the principal value of the arc tangent such that − π2 ≤
arctan x ≤ π2
With this results for the tilt angle θ are arrived that are given in Table 1.
p2
κ= (17)
A
Because a circle has the maximal Area A within a given perimeter p, a
scaling of roundness κ is performed:
p2
κ= (18)
2πA
Therefore κ for a circle is equal 1, for other objects > 1.
The eccentricity ε can directly derived from the semi-major and semi-minor
axes a and b of the object:
√
a2 − b2
ε= (19)
a
ε can be directly calculated from the central moments of second order by
7
The eccentricity ε can have values from 0 to 1. It is 0 with a perfectly round
object and 1 by a line shaped object.
The eccentricity ε is a better measure than the roundness κ of the object,
because it has a clearly defined range of values and therefore it can be compared
much better.
References
[Jähne, 1997] Jähne, B. (1997). Digitale Bildverarbeitung. Springer-Verlag,
Berlin-Heidelberg, 4. edition.
[Teague, 1980] Teague, M. R. (1980). Image analysis via the general theory of
moments. Journal of the Optical Society of America, 70(8):920–930.