Visual C++ 實現Kirsch運算元
Kirsch運算元實現起來相對來說稍微麻煩一些,它採用8個模板對映像上的每一個像素點進行卷積求導數,這8個模板代表8個方向,對映像上的8個特定邊緣方向作出最大響應,運算中取最大值作為映像的邊緣輸出(上述演算法中用到的8個模板在下面的實現代碼中給出)。為了便於讀者理解該演算法的實現,這裡我們給出實現該演算法的函數代碼,可以稍加改動應用到自己的項目中去。
BOOL Kirsch(BYTE *pData,int Width,int Height)
{//定義實現Kirsch演算法的8個模板;
int i,j,s,t,k,max,sum[8];
static a[3][3]={{+5,+5,+5},{-3,0,-3},{-3,-3,-3}};
static a1[3][3]={{-3,+5,+5},{-3,0,+5},{-3,-3,-3}};
static a2[3][3]={{-3,-3,+5},{-3,0,+5},{-3,-3,+5}};
static a3[3][3]={{-3,-3,-3},{-3,0,+5},{-3,+5,+5}};
static a4[3][3]={{-3,-3,-3},{-3,0,-3},{+5,+5,+5}};
static a5[3][3]={{-3,-3,-3},{+5,0,-3},{+5,+5,-3}};
static a6[3][3]={{+5,-3,-3},{+5,0,-3},{+5,-3,-3}};
static a7[3][3]={{+5,+5,-3},{+5,0,-3},{-3,-3,-3}};
BYTE *pData1;
if(pData==NULL)
{
AfxMessageBox("映像資料為空白,請讀取映像資料!");
return FALSE;
}
pData1=(BYTE*)new char[Width*Height];
if(pData1==NULL)
{
AfxMessageBox("映像緩衝資料區申請失敗,請重新申請映像資料緩衝區!");
return FALSE ;
}
memcpy(pData1,pData, Width*8*Height);
//kirsch運算元處理,對每一像素點求取八個方向的導數;;
for(i=1;ifor(j=1;j{
sum[1]=sum[2]=sum[3]=sum[4]=sum[5]=sum[6]=sum[7]=sum[8]=0;
for(t=-1;t{
for(s=-1;s{ sum[1]+=*(pData+WIDTHBYTES(Width*8)*(i+t)+j+s)*a[1+t][1+s];
sum[2]+=*(pData+WIDTHBYTES(Width*8)*(i+t)+j+s)*a1[1+t][1+s]; sum[3]+=*(pData+WIDTHBYTES(Width*8)*(i+t)+j+s)*a2[1+t][1+s]; sum[4]+=*(pData+WIDTHBYTES(Width*8)*(i+t)+j+s)*a3[1+t][1+s]; sum[5]+=*(pData+WIDTHBYTES(Width*8)*(i+t)+j+s)*a4[1+t][1+s]; sum[6]+=*(pData+WIDTHBYTES(Width*8)*(i+t)+j+s)*a5[1+t][1+s]; sum[7]+=*(pData+WIDTHBYTES(Width*8)*(i+t)+j+s)*a6[1+t][1+s]; sum[8]+=*(pData+WIDTHBYTES(Width*8)*(i+t)+j+s)*a7[1+t][1+s];
}
}
//取最大方向的導數;
for(k=0;k{
max=0;
if(maxmax=sum[k];
}
if(maxmax=0;
if(max>255)
max=255;
*(pData1+ Width*8*i+j)=max;
}
memcpy(pData,pData1, Width*8*Height);
delete pData1;
return TRUE;
}
本文轉自
http://www.cublog.cn/u/11570/showart.php?id=335774