princcomp2d
princcomp2d
princcomp2d
Miguel A. Lerma
Abstract
1 Introduction
1
Figure 1: A rotated “HELLO”
Let X be the matrix whose columns are the original coordinates of the given
points:
x1 x2 · · · xn
X=
y1 y2 · · · yn
and X 0 is the analogous matrix with the new coordinates:
0
x1 x02 · · · x0n
0
X = = RX .
y10 y20 · · · yn0
The correlation matrix of X is
σx2 σxy
1
C = XX T = ,
n σxy σy2
and that of X 0 is
1 0 0T 1
C0 = X X = RXX T RT = RCRT ,
n n
Since R is a rotation it is orthogonal and we have RT = R−1 . On the other
hand we want σx0 y0 = 0, hence C 0 must be a diagonal matrix:
2
σx0 0
C0 = .
0 σy20
2
So the problem amounts to diagonalizing C. This can be done in the usual
way, by finding the eigenvalues and eigenvectors of C.
σx2 − λ σxy
= λ2 − (σx2 + σy2 )λ + σx2 σy2 − σxy
2
σxy σy2 − λ
The roots can be found with the usual formula to solve second degree equa-
tions: q
σx2 + σy2 ± (σx2 + σy2 )2 − 4σx2 σy2 + 4σxy
2
λ=
q 2
2 2
σx + σy ± (σx − σy2 )2 + 4σxy
2 2
= .
2
Let λ+ be the root obtained using the plus sign in the formula, and λ− the
one obtained with the minus sign. Then it can be easily checked that the
following are eigenvectors verifying CV± = λ± V± :
2 2
σx + σxy − λ− σx + σxy − λ+
V+ = V− =
σy2 + σxy − λ− σy2 + σxy − λ+
We now apply the method to locating the position, orientation, and ap-
proximate size of a set of points {(x1 , y1 ), (x2 , y2 ), . . . , (xn , yn )} in the plane,
and illustrate it for the example shown in figure (1). The set of points in
3
this example is the one given by the black points of the word “HELLO”
in the image. Each has some coordinates (xi , yi ), and its barycenter (x̄, ȳ)
is the first reference we use for the location of the set on the plane. The
eigenvectors V+ and V− determine the directions of maximum and minimum
dispersion, and their directions can be used for the axes of an ellipse centered
in the barycenter. It remains the determine the values of the semi axes p of
the ellipse, pwhich will be proportional to the standard deviations σx0 = λ+
and σy0 = λ− respectively. In this particular example the sizes has been
chosen to be double of the standard deviations, and the result can be seen
in figure (2). The ellipse has been drawn in green, and the eigenvectors,
scaled so that their lengths coincide with double the standard deviations of
x0 and y 0 , are drawn in red and yellow respectively.
See the appendix for specific code designed to perform the computations.
The program used in the example shown here was actually implemented in
Matlab, and has not been included here, but the computations are basically
identical to the ones performed by the code included below.
4 Conclusion
4
Appendix: sample code implementation
// scan the image (nLength and nHeight are the dimensions of the image)
for(x=0; x<nLength; x++)
{
for(y=0; y<nHeight; y++)
{
if (region(x,y)) // true if we are in the region of interest
{
sumx+=x; sumy+=y; sumxx+=x*x;
sumyy+=y*y; sumxy+=x*y; npix++;
}
}
}
// baricenter
xbar = ((double)sumx)/npix;
ybar = ((double)sumy)/npix;
5
// eigenvalues
lambdaplus = (sumvars + sqrtdiscr)/2;
lambdaminus = (sumvars - sqrtdiscr)/2;
aParallel = aplus/denomPlus;
bParallel = bplus/denomPlus;
aNormal = aminus/denomMinus;
bNormal = bminus/denomMinus;
// semi axes
double k = 2 // scale factor
double majoraxis = k*sqrtf(lambdaplus);
double minoraxis = k*sqrtf(lambdaminus);