72
{
y1=y2=(int)0;
for(k1=-1; k1<=1; k1++)
for(k2=-1; k2<=1; k2++)
{
if( ((n2+k2)<0) || ((n2+k2-1)>=image_width) ) continue;
tmp=(*((*(w+1+k1))+(n2+k2-1)));
y1+=S1[1+k1][1+k2]*tmp;
y2+=S2[1+k1][1+k2]*tmp;
}
y1=abs(y1);
y2=abs(y2);
zn2=(y1>y2)?y1:y2;
putc(zn2,fptr1);
/* The buffer is imPlicit in this last statement. */
}
/* Shift rows of w */
temp=*w;
for(j=0; j<2; j++)
*(w+j)=*(w+j+1);
*(w+2)=temp;
}
gotoxy(70,25);
textattr(WHITE+(BLACK<<4));
cputs ( " ");
gotoxy(1,8);
fclose(fptr); /* close input image file */
fclose(fptr1); /* close output-image file */
}
Chương trình cho các phép xKirsh cũng được trình y dưới
đây.
Chương trình 5.2 “KIRSH.C” Chương trình cho phát hiện đường
biên ảnh dùng toán tử Kirsh.
/*Program 5.2 "KIRSH.C". Program for edge detection using the Kirsh
operators.*/
/* This program is for obtaining the edges using
73
Kirsh Compass operator. */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <alloc.h>
#include <conio.h>
#include <io.h>
#include <ctype.h>
/* Kirsh operators. */
int T[8][3][3]={ { {5,5,5},
{-3,0,-3},
{-3,-3,-3},},
{{5,5,-3},
{5,0,-3},
{-3,-3,-3}, },
{{5,-3,-3},
{5,0,-3},
{5,-3,-3},},
{{-3,-3,-3},
{5,0,-3},
{5,5,-3},},
{{-3,-3,-3},
{-3,0,-3},
{5,5,5},},
{{-3,-3,-3},
{-3,0,5},
{-3,5,5},},
{{-3,5,5},
{-3,0,5},
{-3,-3,5},},
{{-3,5,5},
74
{-3,0,5},
{-3,-3,-3},} };
void main()
{
int i,j,n1,n2,image_width, image_length,k1,k2,ind;
char file_name[14],ch;
unsigned char **w;
unsigned char *temp,tmp;
int y[8] ;
float nsq;
unsigned int zn2;
FILE *fptr, *fptr1;
clrscr ();
printf("Enter file name for input image ->");
scanf("%s",file_name);
if((fptr=fopen(file_name,"rb"))==NULL)
{
printf("%s does not exist.", file_name);
printf("\nPress any key to exit.");
getch ();
exit(1);
}
nsq=filelength(fileno(fptr));
printf("Is this a square image ?");
printf
("\n i.e. Is image_length=image_width (y or n)? ->");
while(((ch=tolower(getch()))!='y')&&(ch!='n'));
putch(ch);
switch(ch)
{
case 'y':
image_length=image_width=sqrt(nsq);
printf("\n Image size = %d x %d",image_length, image_width);
break;
case 'n':
printf("\nEnter image_width-->");
scanf("%d",&image_width);
75
image_length=nsq/image_width;
printf("image length is %d", image_length);
break;
}
printf("\nEnter file name for filtered image ->");
scanf("%s",file_name);
ind=access(file_name,0);
while(!ind)
{
gotoxy(1,6);
printf("File exists. Wish to overwrite? (y or n)-->");
while(((ch=tolower(getch()))!='Y')&&(ch!='n'));
putch(ch);
switch(ch)
{
case 'y':
ind=1;
break;
case 'n':
gotoxy(1,6);
printf (" ");
gotoxy(1,5);
printf(" ");
gotoxy(1,5);
printf("Enter file name -->");
scanf("%s",file_name);
ind=access(file_name,0);
}
}
fptr1=fopen(file_name,"wb");
gotoxy(70,25);
textattr(WHITE+(GREEN<<4)+BLINK);
cputs("WAIT");
/* Allocating memory for Image Transfer Buffer, w. */
w=(unsigned char **)malloc(3*sizeof(char *));
for(i=0;i<3;i++)
*(w+i)=(char *)calloc(image_width,sizeof(char));
/* Clear Image Transfer Buffer. */
76
for(i=0;i<3;i++)
for(j=0;j<image_width;j++)
*((*(w+i))+j)=(unsigned char)0;
/* Algorithm */
for(n1=0; n1<image_length;n1++)
{
gotoxy(1,9);
printf(" Transfered line %-4d to image transfer buffer. ",n1);
/* Transfer row n2 of the image to the last row of w. */
for(j=0;j<image_width;j++)
{
ch=(char)fgetc(fptr);
*((*(w+2))+j) = (unsigned char)ch;
}
for(n2=0; n2<image_width; n2++)
{
for(j=0;j<8;j++)
y[j]=(int)0.0;
for(k1=-1; k1<=1; k1++)
for(k2=-1; k2<=1; k2++)
{
if( ((n2+k2)<0) || ((n2+k2-1)>=image_width) ) continue;
tmp=(*((*(w+1+k1))+(n2+k2-1)));
for(j=0;j<8;j++)
y[j]+=T[j][1+k1][1+k2]*tmp;
}
for(j=0;j<8;j++)
y[j]=abs(y[j]);
zn2=y[0];
for(j=1;j<8;j++)
if(y[j]>zn2) zn2=y[j];
putc(zn2,fptr1);
/* The buffer is implicit in this last statement. */
}
/* shift rows of w */
temp=*w;