Visual C++ 實現Kirsch運算元 – 影像處理 – appig

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.