1. 区域标定算法
http://www.codersource.net/csharp_labelling_connected_components.aspx
2. 区域内特征不变几何特性提取算法
http://www.codersource.net/csharp_calculating_rts_invariant_moments.aspx
(1)计算图像函数的(p+q)阶距
private void calcAreaCenter()
{
double momentV=0;
double momentH=0;
int val;
unsafe
{
byte*p=(byte*)(void*)ptr;
for (int row = 0; row < numRows; row++)
{
for (int col = 0; col < numCols; col++)
{
val=(p+(r+row)*stride+(c+col)*3)[0];
if ( val==label)
{
++area;
momentV += row;
momentH += col;
}
}
}
}
centerVertical = momentV / area;
centerHorizontal = momentH / area;
}
public double centralMoment (int p, int q, int objectId)
{
double v, h;
int pv;
double mom = 0;
unsafe
{
byte*pt=(byte*)(void*)ptr;
for (int row = 0; row < numRows; row++)
{
for (int col = 0; col < numCols; col++)
{
pv=((pt+(r+row)*stride+(c+col)*3))[0];
if (pv == objectId)
{
v = row - centerVertical;
h = col - centerHorizontal;
mom += Math.Pow (v, p) * Math.Pow (h, q);
}
}
}
}
return mom;
}
public double normCentralMoment (int p, int q,int objectId)
{
double mom = centralMoment (p, q, objectId);
double gamma = (p + q)/2.0 + 1;
double normalizedMom = mom / Math.Pow (area, gamma);
return normalizedMom;
}
public double invariantMoment (int n, int objectId)
{
double invMom = 0;
double eta11 = 0, eta02 = 0, eta20 = 0, eta03 = 0,
eta30 = 0, eta21 = 0, eta12 = 0;
if (n <= 2 || n == 6)
{
eta20 = normCentralMoment (2, 0, objectId);
eta02 = normCentralMoment (0, 2, objectId);
}
if (n >= 3)
{
eta12 = normCentralMoment (1, 2, objectId);
eta21 = normCentralMoment (2, 1, objectId);
eta03 = normCentralMoment (0, 3, objectId);
eta30 = normCentralMoment (3, 0, objectId);
}
if (n == 1)
{
invMom = eta20 + eta02;
}
else if (n == 2)
{
eta11 = normCentralMoment (1, 1, objectId);
invMom = sq (eta20 - eta02)
+ 4 * sq (eta11);
}
else if (n == 3)
{
invMom = sq(eta30 - 3*eta12) + sq(eta03 - 3*eta21);
}
else if (n == 4)
{
invMom = sq(eta30 + 3*eta12) + sq(eta03 + 3*eta21);
}
else if (n == 5)
{
invMom = (eta03 - 3*eta12) * (eta30 + eta12) *
(sq(eta30 + eta12) - 3 * sq(eta21 + eta03))
+ (3 * eta21 - eta03) * (eta21 + eta03) *
(3 * sq(eta30 + eta12) - sq(eta21 + eta30));
}
else if (n == 6)
{
invMom = (eta02 - eta20) *
(sq(eta30 + eta12) - sq(eta21 + eta03))
+ 4 * eta11 * (eta30 + eta12) * (eta21 + eta03);
}
else if (n == 7)
{
invMom = (3 * eta21 - eta03) * (eta30 + eta12) *
(sq(eta30 + eta12) - 3 * (eta21 + eta03))
+ 3*(eta12 - eta30) * (eta21 + eta03) *
(3 * sq(eta30 + eta12) - sq(eta21 + eta03));
}
return invMom;
}
ps: 这段代码写得不错!