?? 二值圖像邊緣提取算法c語言實現.txt
字號:
二值圖像邊緣提取算法C語言實現[原創]
定義:每個像素的取值均為0或1,稱這樣的圖像為二值圖像。
算法:檢查所有像素,若該像素為物體上與背景接觸的像素(四連通像素中既有背景像素又有物體像素),則為邊界。
程序:
#define M 30
#define N 20
void edge(int image[M][N],int bianyuan[M][N])
{
int i,j;
int inner=1,outer=1;
for (i=0;i<M;i++)/*清除數據*/
for(j=0;j<N;j++)
bianyuan[i][j]=0;
for(i=1;i<M-1;i++)
for(j=1;j<N-1;j++)
{
inner=1;/*假設該像素或為物體,或為背景*/
outer=1;
if(image[i-1][j]==0||image[i+1][j]==0||image[i][j-1]==0||image[i][j+1]==0)
inner=0;
if(image[i-1][j]==1||image[i+1][j]==1||image[i][j-1]==1||image[i][j+1]==1)
outer=0;
if(inner==0&&outer==0&&image[i][j]==1)/*像素周圍既有物體又有背景*/ bianyuan[i][j]=1;/*,且該像素為物體上的像素(image[i][j]==1),則定義為邊界*/
}
}
void output(int array[M][N],int n)
{
int i,j;
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<N;j++)
if(array[i][j]==1)
printf("1");
else
printf(" ");
}
}
void main()
{
int image[M][N]={{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,1,1,1,0,0,0,1,1,1,1,0,0,0,1,1,1,0,0},
{0,1,1,1,1,0,0,1,1,1,1,1,1,0,0,1,1,1,0},
{0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,1,1,1,0},
{0,0,1,1,1,1,0,0,0,1,1,1,1,1,0,1,1,1,0},
{0,1,1,1,1,1,1,0,0,1,1,1,0,0,1,1,1,1,0},
{0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,0},
{0,0,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,0},
{0,0,0,0,1,1,1,1,0,0,0,0,0,1,1,1,1,1,0}};
int bianyuan[M][N]={0};
int i,j;
printf("\nThe origianl image is:\n");
output(image,10);
edge(image,bianyuan);
printf("\nIts edge is:\n");
output(bianyuan,10);
}
寫完了,又看一下,感覺edge函數太羅嗦了,不夠簡練,想了一下,改成了下面的樣子,函數接口不變:
void edge(int image[M][N],int bianyuan[M][N])
{
int i,j;
for (i=0;i<M;i++)
for(j=0;j<N;j++)
bianyuan[i][j]=0;
for(i=1;i<M-1;i++)
for(j=1;j<N-1;j++)
{
int t=image[i-1][j]+image[i+1][j]+image[i][j-1]+image[i][j+1];
if(t>0&&t<4&&image[i][j]==1)/*周圍4個像素值介于1~3之間,*/
bianyuan[i][j]=1; /*且當前像素為物體,則其必為邊界*/
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -