writen by wqj1212@yahoo.com.cn
double * CRecognise::recogniseInvariantMoment(IplImage *src)
{
float cx=0.0;
float cy=0.0;
float Mpq=0.0;
int width;
int height;
double q1=0.0,q2=0.0,q3=0.0,q4=0.0,q5=0.0,q6=0.0,q7=0.0;
double n20=0.0,n02=0.0,n11=0.0,n30=0.0,n12=0.0,n03=0.0,n21=0.0,n00=0.0;
double u20=0.0,u02=0.0,u11=0.0,u30=0.0,u12=0.0,u03=0.0,u21=0.0,u00=0.0;
width=src->width;
height=src->height;
for(int j=1;j<=height;j++)
for(int i=1;i<=width;i++)
{
Mpq+=(float)(CV_IMAGE_ELEM(src,uchar,j,i));
cx+=i*CV_IMAGE_ELEM(src,uchar,j,i);
cy+=j*CV_IMAGE_ELEM(src,uchar,j,i);
}
cx/=Mpq;
cy/=Mpq;
for(j=1;j<=height;j++)
for(int i=1;i<=width;i++)
{
u20+=(i-cx)*(i-cx)*CV_IMAGE_ELEM(src,uchar,j,i);
u02+=(j-cy)*(j-cy)*CV_IMAGE_ELEM(src,uchar,j,i);
u11+=(i-cx)*(j-cy)*CV_IMAGE_ELEM(src,uchar,j,i);
u30+=(i-cx)*(i-cx)*(i-cx)*CV_IMAGE_ELEM(src,uchar,j,i);
u12+=(i-cx)*(j-cy)*(j-cy)*CV_IMAGE_ELEM(src,uchar,j,i);
u03+=(j-cy)*(j-cy)*(j-cy)*CV_IMAGE_ELEM(src,uchar,j,i);
u21+=(i-cx)*(i-cx)*(j-cy)*CV_IMAGE_ELEM(src,uchar,j,i);
}
u00=Mpq;
n20=u20/(u00*u00);
n02=u02/(u00*u00);
n11=u11/(u00*u00);
n30=u30/sqrt(u00*u00*u00*u00*u00);
n12=u12/sqrt(u00*u00*u00*u00*u00);
n03=u03/sqrt(u00*u00*u00*u00*u00);
n21=u21/sqrt(u00*u00*u00*u00*u00);
q1=n20+n02;
q2=(n20-n02)*(n20-n02)+4*n11*n11;
q3=(n30-3*n12)*(n30-3*n12)+(n03+3*n21)*(n03+3*n21);
q4=(n30+n12)*(n30+n12)+(n03+n21)*(n03+n21);
q5=(n30-3*n12)*(n30-n12)*((n30+n12)*(n30+n12)-3*(n03+n21)*(n03+n21))+(3*n21-n03)*(n21+n03)*(3*(n30+n12)*(n30+n12)-(n03+n21)*(n03+n21));
q6=(n20-n02)*((n30+n12)*(n30+n12)-(n03+n21)*(n03+n21))+4*n11*(n30+n12)*(n03+n21);
q7=(3*n21-n03)*(n30+n12)*((n30+n12)*(n30+n12)-3*(n03+n21)*(n03+n21))+(3*n12-n30)*(n21+n03)*(3*(n30+n12)*(n30+n12)-(n03+n21)*(n03+n21));
double pt[7];
pt[0]=q1;
*(pt+1)=q2;
*(pt+2)=q3;
*(pt+3)=q4;
*(pt+4)=q5;
*(pt+5)=q6;
*(pt+6)=q7;
return pt;
}
{
float cx=0.0;
float cy=0.0;
float Mpq=0.0;
int width;
int height;
double q1=0.0,q2=0.0,q3=0.0,q4=0.0,q5=0.0,q6=0.0,q7=0.0;
double n20=0.0,n02=0.0,n11=0.0,n30=0.0,n12=0.0,n03=0.0,n21=0.0,n00=0.0;
double u20=0.0,u02=0.0,u11=0.0,u30=0.0,u12=0.0,u03=0.0,u21=0.0,u00=0.0;
width=src->width;
height=src->height;
for(int j=1;j<=height;j++)
for(int i=1;i<=width;i++)
{
Mpq+=(float)(CV_IMAGE_ELEM(src,uchar,j,i));
cx+=i*CV_IMAGE_ELEM(src,uchar,j,i);
cy+=j*CV_IMAGE_ELEM(src,uchar,j,i);
}
cx/=Mpq;
cy/=Mpq;
for(j=1;j<=height;j++)
for(int i=1;i<=width;i++)
{
u20+=(i-cx)*(i-cx)*CV_IMAGE_ELEM(src,uchar,j,i);
u02+=(j-cy)*(j-cy)*CV_IMAGE_ELEM(src,uchar,j,i);
u11+=(i-cx)*(j-cy)*CV_IMAGE_ELEM(src,uchar,j,i);
u30+=(i-cx)*(i-cx)*(i-cx)*CV_IMAGE_ELEM(src,uchar,j,i);
u12+=(i-cx)*(j-cy)*(j-cy)*CV_IMAGE_ELEM(src,uchar,j,i);
u03+=(j-cy)*(j-cy)*(j-cy)*CV_IMAGE_ELEM(src,uchar,j,i);
u21+=(i-cx)*(i-cx)*(j-cy)*CV_IMAGE_ELEM(src,uchar,j,i);
}
u00=Mpq;
n20=u20/(u00*u00);
n02=u02/(u00*u00);
n11=u11/(u00*u00);
n30=u30/sqrt(u00*u00*u00*u00*u00);
n12=u12/sqrt(u00*u00*u00*u00*u00);
n03=u03/sqrt(u00*u00*u00*u00*u00);
n21=u21/sqrt(u00*u00*u00*u00*u00);
q1=n20+n02;
q2=(n20-n02)*(n20-n02)+4*n11*n11;
q3=(n30-3*n12)*(n30-3*n12)+(n03+3*n21)*(n03+3*n21);
q4=(n30+n12)*(n30+n12)+(n03+n21)*(n03+n21);
q5=(n30-3*n12)*(n30-n12)*((n30+n12)*(n30+n12)-3*(n03+n21)*(n03+n21))+(3*n21-n03)*(n21+n03)*(3*(n30+n12)*(n30+n12)-(n03+n21)*(n03+n21));
q6=(n20-n02)*((n30+n12)*(n30+n12)-(n03+n21)*(n03+n21))+4*n11*(n30+n12)*(n03+n21);
q7=(3*n21-n03)*(n30+n12)*((n30+n12)*(n30+n12)-3*(n03+n21)*(n03+n21))+(3*n12-n30)*(n21+n03)*(3*(n30+n12)*(n30+n12)-(n03+n21)*(n03+n21));
double pt[7];
pt[0]=q1;
*(pt+1)=q2;
*(pt+2)=q3;
*(pt+3)=q4;
*(pt+4)=q5;
*(pt+5)=q6;
*(pt+6)=q7;
return pt;
}