major / minor axis

I was trying to remember how to calculate the major and minor axis of a contour from its central moments (to get orientation). I got the answer but maybe this is useful to someone else also! will try to post an example project with it later…

  
  
double cm20 = cvGetCentralMoment(momes, 2, 0);  
double cm02 = cvGetCentralMoment(momes, 0, 2);  
double cm11 = cvGetCentralMoment(momes, 1, 1);  
  
if (cm11 == 0.0f) cm11 = 0.00001f;  
float d = (cm20 - cm02);  
float b = d/cm11;  
float quadratic = ((-1.0f*b) + sqrtf(b*b + 4.0f))/2.0f;  
float theta = atanf(quadratic);  
  
if (((d < 0) && (quadratic < 1.0f)) ||  
        ((d > 0) && (quadratic > 1.0f))) { theta += HALF_PI;}