
193
char file_name[14];
clrscr();
printf("Enter file name for image-->");
scanf("%s",file_name);
fptri=fopen(file_name,"rb");
if(fptri==NULL)
{
printf("No such image file exists.\n");
exit(1);
}
nsq=(double)filelength(fileno(fptri));
printf("Is image length equal to image width ? (y or n)-->");
while(((ch=getche())!='y')&&(ch!='n'));
switch(ch)
{
case 'y':
image_length=image_width=(int)sqrt(nsq);
break;
case 'n':
printf("\nEnter image_width-->");
scanf("%d",&image_width);
image_length=(int)(nsq/(float)image_width);
}
printf("\n Image size %dx%d.",image_length,image_width);
printf("\nEnter file name for filter's coefficients-->");
scanf("%s",file_name);
fptro=fopen(file_name,"r");
if(fptro==NULL)
{
printf("\nNo such file exists.\n");
exit(1);
}
fscanf(fptro," %d",&N);
printf("IIR filter has an order of %dx%d.",N,N);
N1=N+1;
/* Allocating memory space. */
a=(float **)malloc(N1*sizeof(float *));
for(i=0;i<N1;i++)
*(a+i)=(float *)malloc(N1*sizeof(float));
b=(float **)malloc(N1*sizeof(float *));
for(i=0;i<N1;i++)
*(b+i)=(float *)malloc(N1*sizeof(float));
wx=(unsigned char **)malloc(N1*sizeof(char *));

194
for(i=0;i<N1;i++)
*(wx+i)=(unsigned char *)malloc(image_width*sizeof(char));
wy=(float **)malloc(N1*sizeof(float *));
for(i=0;i<N1;i++)
*(wy+i)=(float *)malloc(image_width*sizeof(float));
buff=(unsigned char *)malloc(image_width*sizeof(char));
bufft=(float *)malloc(image_width*sizeof(float));
for(i=0;i<=N;i++)
for(j=0;j<=N;j++)
fscanf(fptro," %f %f",&a[i][j],&b[i][j]);
fclose(fptro);
yt=wherey()+1;
again:
gotoxy(1,yt);
printf( " ");
gotoxy(1,yt);
printf("Enter file name for filtered image ->");
scanf("%s",file_name);
if((stricmp("temp.img",file_name))==0)
{
printf("This is a reserved file name. Use some other name.");
goto again;
}
gotoxy(1,yt+1);
printf( " ");
ind=access(file_name,0);
while(!ind)
{
gotoxy(1,yt+1);
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,yt+1);
printf (" ");
gotoxy(1,yt);
printf(" ");
gotoxy(1,yt);

195
printf("Enter file name -->");
scanf("%s",file_name);
ind=access(file_name,0);
}
}
fptro=fopen(file_name, "wb");
fptrt=fopen( "temp.img","wb+");
/* Clearing input and output image transfer buffers. */
for(i=0;i<N1;i++)
for(j=0;j<image_width;j++)
{
wx[i][j]=(char)0;
wy[i][j]=(float)0.0;
}
max=(float)0.0;
min=(float)1000.0;
true_width=image_width-N1;
true_length=image_length*0.9;
for(n=0; n<image_length;n++)
{
fread(buff,image_width,1,fptri);
gotoxy(1,yt+4);
printf("Transferred line %d to input buffer.",n);
for(j=0;j<image_width;j++)
wx[0][j]=buff[j];
for(m=0;m<image_width;m++)
{
wy[0][m]=(float)0.0;
for(i=0;i<=N;i++)
for(j=0;j<=N;j++)
{
if((m-j)<0) continue;
wy[0][m]+=a[i][j]*(float)wx[i][m-j];
if((i+j)==0) continue;
wy[0][m]-=b[i][j]*(float)wy[i][m-i];
}
bufft[m]=wy[0][m];
if((m>N1)&&(m<true_width)&&(n>N1)&&(n<true_length))
{
if(bufft[m]>max) max=wy[0][m];
if(bufft[m]<min) min=wy[0][m];
}
}
fwrite(bufft,image_width,sizeof(float),fptrt);

196
/* Shifting. */
temp1=*(wx+N);
for(i=0;i<N;i++)
*(wx+N-i)=*(wx+(N-1)-i);
*wx=temp1;
temp2=*(wy+N);
for(i=0;i<N;i++)
*(wy+N-i)=*(wy+(N-1)-i);
*wy=temp2;
}
printf("\n\n Scaling...");
yt=wherey();
rewind(fptrt);
scale=(float)250.0/(max-min);
for(n=0;n<image_length;n++)
{
gotoxy(1,yt+2);
printf("Scaling line %d.",n);
fread(bufft,image_width,sizeof(float),fptrt);
for(m=0;m<image_width;m++)
{
tt=(bufft[m]-min)*scale;
if(tt>255.0) tt=255.0;
if(tt<0.0) tt=0.0;
buff[m]=(unsigned char)tt;
}
fwrite(buff,image_width,1,fptro);
}
fclose(fptri);
fclose(fptro);
fclose(fptrt);
remove("temp.img");
}
Bây giờ chúng ta hãy kiểm tra chương trình lọc IIR. Lần này chúng ta sẽ
xem xét ảnh x-quang của lồng ngực trong hình 9.9. Bằng cách sử dụng chương
trình 9.1 và 9.2 chúng ta có thể thiết kế một bộ lọc thông cao sử dụng kết quả
đáp ứng biên độ nhận được từ hàm Butterworth với n = 1 và c = 1.0. Bậc của
bộ lọc được thiết kế là 2 2. Hình 9.10 chỉ ra ảnh kết quả sau khi lọc. Trên
ảnh đã lọc có nhiều chi tiết được làm rõ. ảnh được cho sẵn trên đĩa với tên file
là "RIBS.IMG".

197
Hình 9.9 ảnh chụp x quang lồng ngực.
Hình 9.10 ảnh lồng ngực sau khi được làm nổi.