c#已经注释,这里就省略了注释,可以对比看
//image0,image1А8ㄧ计8瓜钩矪瞶
void CLineBufProcess::mg_CannySideGrandiant(BYTE* image0, BYTE* image1, int w, int h)
{
int* p=new int[h*w];
int * q=new int[h*w];
int * m=new int[h*w];
double *t=new double[h*w];
memset(p,0,w*h);
memset(q,0,w*h);
memset(m,0,w*h);
memset(t,0,w*h);
int** P = (int**)malloc(sizeof(int*)*(h));
for(int y=0; y<h; y++)
{
P[y] = p+y*w;
}
int** Q = (int**)malloc(sizeof(int*)*(h));
for(int y=0; y<h; y++)
{
Q[y] = q+y*w;
}
int** M = (int**)malloc(sizeof(int*)*(h));
for(int y=0; y<h; y++)
{
M[y] = m+y*w;
}
double** Theta = (double**)malloc(sizeof(double*)*(h));
for(int y=0; y<h; y++)
{
Theta[y] = t+y*w;
}
for(int y=0; y<(h-1); y++)
{
for(int x=0; x<(w-1); x++)
{
int n = (y*w+x);
p[y*w+x] = AshCount(image0, n, n+1);//函数在末尾,求x梯度图像
q[y*w+x]= AshCount(image0, n, n+w);//我们只使用了梯度图像,并没有用sobel增强边缘
}
}
for(int i=0; i<h-1; i++)
{
for(int j=0; j<w-1; j++)
{
m[i*w+j]=p[i*w+j]+q[i*w+j];
m[i*w+j] = m[i*w+j]>255?255:m[i*w+j];
Theta[i][j] = atan(Q[i][j]/(P[i][j]*1.0)) * 57.3;
if(Theta[i][j] < 0)
Theta[i][j] += 360;
}
}
//┮惠碭璸衡Ч拨獶伐ы
BYTE* n=new BYTE[w*h];
memset(n,0,w*h);
memcpy(n,image1,w*h);
BYTE** N = (BYTE**)malloc(sizeof(BYTE*)*(h));
for(int y=0; y<h; y++)
{
N[y] = n+y*w;
}
int g1=0, g2=0, g3=0, g4=0;
double dTmp1=0.0, dTmp2=0.0;
double dWeight=0.0;
for(int i=0; i<w; i++)
{
N[0][i] = 0;
N[h-1][i] = 0;
}
for(int j=0; j<h; j++)
{
N[j][0] = 0;
N[j][w-1] = 0;
}
for(int i=1; i<(w-1); i++)
{
for(int j=1; j<(h-1); j++)
{
//辫0獶娩翴
if(M[j][i] == 0)
{
N[j][i] = 0;
}
else
{
材///
/ g1 g2 /
/ C /
/ g4 g3 /
/
if( ((Theta[j][i]>=90)&&(Theta[j][i]<135)) ||
((Theta[j][i]>=270)&&(Theta[j][i]<315)))
{
g1 = M[j-1][i-1];
g2 = M[j-1][i];
g4 = M[j+1][i];
g3 = M[j+1][i+1];
dWeight = abs(P[j][i]/(Q[j][i]*1.0));
dTmp1 = g1*dWeight+g2*(1-dWeight);
dTmp2 = g3*dWeight+g4*(1-dWeight);
}
材///
/ g1 /
/ g4 C g2 /
/ g3 /
/
else if( ((Theta[j][i]>=0)&&(Theta[j][i]<45)) ||
((Theta[j][i]>=180)&&(Theta[j][i]<225)))
{ //int nPointIdx = i+j*w;
g3 = M[j+1][i-1];
g2 = M[j][i+1];
g1 = M[j-1][i+1];
g4 = M[j][i-1];
dWeight = abs(Q[j][i]/(P[j][i]*1.0)); //
dTmp1 = g1*dWeight+g2*(1-dWeight);
dTmp2 = g3*dWeight+g4*(1-dWeight);
}
材///
/ g2 g1 /
/ C /
/ g3 g4 /
/
else if( ((Theta[j][i]>=45)&&(Theta[j][i]<90)) ||
((Theta[j][i]>=225)&&(Theta[j][i]<270)))
{ //int nPointIdx = i+j*w;
g2 = M[j-1][i];
g1 = M[j-1][i+1];
g4 = M[j+1][i];
g3 = M[j+1][i-1];
dWeight = abs(P[j][i]/(Q[j][i]*1.0)); //はタち
dTmp1 = g1*dWeight+g2*(1-dWeight);
dTmp2 = g3*dWeight+g4*(1-dWeight);
}
材///
/ g3 /
/ g4 C g2 /
/ g1 /
/
else if( ((Theta[j][i]>=135)&&(Theta[j][i]<180)) ||
((Theta[j][i]>=315)&&(Theta[j][i]<360)))
{ //int nPointIdx = i+j*w;
g1 = M[j+1][i+1];
g2 = M[j][i+1];
g3 = M[j-1][i-1];
g4 = M[j][i-1];
dWeight = abs(Q[j][i]/(P[j][i]*1.0)); //タち
dTmp1 = g1*dWeight+g2*(1-dWeight);
dTmp2 = g3*dWeight+g4*(1-dWeight);
}
}
if((M[j][i]>=dTmp1) && (M[j][i]>=dTmp2))
{
N[j][i] = 128;
}
else
{
N[j][i] = 0;
}
}
}
//N is 0 or 128 gray bitmap
//蔼耬砞﹚
int nHist[361];
int nEdgeNum;
int nMaxMag = 0;
int nHighCount;
for(int i=0;i<361;i++)
nHist[i] = 0;
for(int i=0; i<h; i++)
{
for(int j=0; j<w; j++)
{
if(N[i][j]==128)
nHist[M[i][j]]++;
}
}
nEdgeNum = nHist[0];
nMaxMag = 0;
for(int i=1; i<361; i++)
{
if(nHist[i] != 0)
{
nMaxMag = i;
}
nEdgeNum += nHist[i];
}
double dRatHigh = m_twoThres; ノㄓ絋﹚蔼耬
double dThrHigh;
int dThrLow;
double dRatLow = 0.5;
nHighCount = (int)(dRatHigh * nEdgeNum + 0.5);
int j=1;
nEdgeNum = nHist[1];
while((j<(nMaxMag-1)) && (nEdgeNum < nHighCount))
{
j++;
nEdgeNum += nHist[j];
}
dThrHigh = j;
dThrLow = (int)((dThrHigh) * dRatLow + 0.5);
SIZE sz;
sz.cx = w;
sz.cy = h;
for(int i=0; i<h; i++)
{
for(int j=0; j<w; j++)
{
if((N[i][j]==128) && (M[i][j] >= dThrHigh))
{
N[i][j] = 255;
TraceEdge(i, j, dThrLow, n, m, sz); //
}
}
}
for(int i=0; i<h; i++)
{
for(int j=0; j<w; j++)
{
if(N[i][j] != 255)
{
N[i][j] = 0 ;
}
}
}
memcpy(image1,n,w*h);
memcpy(image0,n,w*h);
free(P);
free(p);
free(Q);
free(q);
free(N);
free(M);
free(m);
free(Theta);
free(t);
delete n;
}
/
int CLineBufProcess::AshCount(BYTE* image0, unsigned int c1, unsigned int c2)
{
int dAsh=abs( image0[c1] - image0[c2] );
return dAsh;
}
好,canny(坎尼?)告一段落!