Tải bản đầy đủ (.pdf) (8 trang)

Giáo trình xử lý ảnh y tế Tập 2 P3 pps

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (397.3 KB, 8 trang )


141


Hình 7.7 "CAMEL.IMG" và ảnh đã được phóng to.

N2=N<<1;

/* Allocating memory for bit reversal LUT.*/
L=(unsigned int *)malloc(N2*sizeof(unsigned int));

/* Generate Look-up table for bit reversal.*/
bit_reversal(L,m,N);

/* Allocating memory for twiddle factors. n2=N- 1 */
wr=(float *)malloc(n2*sizeof(float));
wi=(float *)malloc(n2*sizeof(float));

/*Generating LUT for twiddle factors.*/
WTS(wr,wi,N,-1);


142

/* Taking 2-D FFT. */
FFT2D(fptri,fptro,wr,wi,L,N,m,-1);
clrscr() ;
printf(" Adding zeros to FFT of image. \n");

/* Adding zeros to double the size of the FFT.*/
N4=N2<<1 ;


buffi=(float *)malloc(N2*sizeof(float));
buffo=(float *)malloc(N4*sizeof(float));
fptri=fopen("FFT1.img","rb");
fptrt=fopen("temp.img","wb+");
fptro=fopen("TFFT2.jmg","wb+");
for(i=0; i<N4; i++)
buffo[i]=(float)0.0;
/* store O's in the first N/2 lines.*/
for(i=0; i<(N>>1); i++)
fwrite(buffo,N4,sizeof(float),fptrt);
/* Store O's in the first and last N/2 rows.*/
for(i=0; i<N; i++)
fread(buffi,N2,sizeof(float),fptri);
for(j=0;j<N;j++)
buffo[j]=(float)0.0;
for(j=0;j<N2;j++)
buffo[j+N]=buffi[j];
for(j=(N2+N);j<N4;j++)
buffo[j]=(float)0.0;
fwrite(buffo,N4,sizeof(float),fptrt);
for(i=0; i<N4; i++)
buffo[i]=(float)0.0;
for(i=0;i<(N>>1);i++)
fwrite(buffo,N4,sizeof(float),fptrt);
fclose(fptri);
rewind(fptrt);

/* Taking the inverse FFT.*/
printf("Taking the inverse FFT.\n");
m2=m+1 ;


/* Generating bit reversal I(JT.*/
bit_reversal(L,m2,N2);
WTS(wr,wi,N2,1);
FFT2D(fptrt,fptro,wr,wi,L,N2,m2,1);
clrscr() ;

143

printf("Last stage in forming enlarged
image.");
fptri=fopen("IFFT2.img","rb");
fptro=fopen(file_name,"wb");
buffr=(unsigned char
*)malloc(N2*sizeof(char));
max=0; min=1.0e7;
for(i=0;i<N2;i++)
{
fread(buffo,N4,sizeof(float),fptri);
for(j=0;j<N2;j++)
{
scale=buffo[j<<1];
if(scale>max) max=scale;
if(scale<min) min=scale;
}
}
scale=(float)255.0/(max-min);
rewind(fptri);
for(i=0;i<N2;i++)
{

fread(buffo,N4,sizeof(float),fptri);
for(j=0;j<N2;j++)
buffr[j]=(unsigned char)((buffo[j<<1]-
min)*scale);
fwrite(buffr,N2,sizeof(char),fptro);
}

fcloseall();
remove("FFT1.img");
remove("temp.img");
remove("IFFT2.img");
printf("\nDone.");
}

7.5.2 Nhân đôi độ phân giải trên ảnh dùng phép nội suy không
gian
Trong phương pháp này ảnh đầu tiên được chứa lên một mảng có kích
thước 2N

2N với tất cả các hàng và các cột được xoá về không. ảnh được
chiếu lần lượt với các phép nội suy. Hình 7.8 liệt kê các phần của các phép nội
suy phổ biến nhất. Giải thuật nội suy vuông là thao tác sao chép trong đó
những điểm ảnh về cơ bản được sao chép vào những vị trí rỗng. Có thể thu
được toán tử nội suy tam giác nhờ sự kết hợp hai toán tử nội suy vuông, vì vậy

144

ta có thể thu được cùng kết quả nếu ta áp dụng liên tiếp hai phép nội suy
vuông. Giải thuật chuyển đổi những sao chép cơ bản theo bộ lọc thông thấp
sang ảnh để có được ảnh trơn hơn. Kết hợp thực hiện ba lần liên tục trên ảnh

với toán tử nội suy vuông tương đương với áp dụng toán tử nội suy Bell. Toán
tử nội suy bậc 3 có thể thu được nhờ kết hợp phép nội suy vuông và Bell hoặc
kết hợp toán tử nội suy vuông bốn lần. Sử dụng cùng khái niệm đó, chúng ta
có thể dễ dàng có được nội suy cao hơn.

















Hình 7.8 Các phép nội suy thông thường.
Những khái niệm trên có thể được thực hiện trong phần mềm tương tự với
việc thực hiện lọc FIR. Bộ đệm chuyển đổi ảnh kích thước N  (2  độ rộng
của ảnh) được tận dụng, ở đó N = 2 cho toán tử nội suy vuông, N = 3 cho toán
tử nội suy tam giác, v.v Một hàng của ảnh được chuyển thành hàng cuối
cùng của bộ đệm chuyển đổi ảnh và lưu trữ tại các vị trí khác với các vị trí ở
giữa đặt bằng không. Tương quan của phép nội suy với bộ đệm truyền tới ảnh
và dịch chuyển hàng như trong phương pháp trong bộ lọc FIR, một hàng của
các giá trị 0 được chuyển đổi thành hàng cuối cùng của bộ đệm chuyển đổi

ảnh. Những bước này được lặp đi lặp lại cho những phần còn lại của ảnh, bằng
cách lựa chọn sự chuyển đổi một hàng từ ảnh theo một hàng của các giá trị 0.
Thủ tục thực hiện thuật toán này được cho trong chương trình 7.3.

Chương trình 7.3 “ENLARGE.C”.

/* This program doubles the dimensions of an
image by interpolation. */
Hình vuông: 1 1
1 1

Tam giác: 1 2 1
2 4 2
1 2 1

Bell 1 3 3 1
3 9 9 3
3 9 9 3
1 3 3 1

Cubic B-Spline 1 4 6 4 1
4 16 24 16 4
6 24 36 24 6
4 16 24 16 4



1 4 6 4 1



145


#include <stdio.h>
#include <math.h>
#include <alloc.h>
#include <conio.h>
#include <io.h>
#include <stdlib.h>

#include <ctype.h>
#include <string.h>

int SQUARE[2][2]={ { 1, 1 },
{ 1, 1 }};

int TRIANGLE[3][3]= { { 1, 2, 1 },
{ 2, 4, 2 },
{ 1, 2, 1 } };

int BELL[4][4]={ { 1, 3, 3, 1 },
{ 3, 9, 9, 3},
{ 3, 9, 9, 3},
{ 1, 3, 3, 1} } ;
int CUBIC_B_SPLINE[5][5]={ { 1, 4, 6, 4, 1 },
{ 4, 16, 24, 16, 4 },
{ 6, 24, 36, 24, 6 },
{ 4, 16, 24, 16, 4 } };
void main()
{

char file_name[14],ch;
FILE *fptri,*fptro,*fptrt;
double nsq;
int image_length,image_width, N, N1,image_length2,
image_width2,i,j,ind,xt,yt;
unsigned int **a,n1,n2,k1,k2,sum,max,min,*bufft;
unsigned char **w,*buffi,*buffo,*temp;
float scale;

clrscr();
printf("Enter file name for input image >");
scanf("%s",file_name);
fptri=fopen(file_name,"rb");
if(fptri==NULL)
{
printf("%s does not exist.",file_name);

146

printf("\nPress any key to exit. ");
getch() ;
exit(1);
}
nsq=filelength(fileno(fptri));
printf("Is this a square image ?");
printf("\n i.e. Is image length =image width (y or
no) ? >");
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);
image_length=nsq/image_width;
printf("Image length is %d", image_length);
break;
again :
gotoxy(1,5);
printf("
");
gotoxy(1,5);
printf("Enter file name for enlarged 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,6);
printf ( "

");
ind=access(file_name,0);
while(!ind)

147

{
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);
}
}

fptro=fopen(file_name,"wb");
printf("\nEnter choice for interpolation \n");
printf(" 1.SQUARE. \n");
printf(" 2.TRIANGLE. \n");
printf(" 3.BELL. \n");
printf(" 4.CUBIC B-SPLINE. \n");
printf(" Enter choice (1,2,3 or 4) ");
while(((ch=getche())!='l')&&(ch!='2')
&&(ch!='3')&&(ch!='4'));
N1=ch-48;
N=N1+1 ;
a=(unsigned int **)malloc(N*sizeof(int *));
for(i=0;i<N;i++)
*(a+i)=(unsigned int *)malloc(N*sizeof(int));

switch(N1)
{
case 1: for(i=0;j<N;i++)
for(j=0;j<N;j++)
a[i][j]=SQUARE[i][j];

148

break;
case 2: for(i=0;i<N;i++)
for(j=0;j<N;j++)
a[i][j]=TRIANGLE[i][j];
break;
case 3: for(i=0;i<N;i++)
for(j=0;j<N;j++)

a[i][j]=BELL[i][j];
break ;
case 4: for(i=0;i<N;i++)
for(j=0;j<N;j++)
a[i][j]=CUBIC_B_SPLINE[i][j];
}
image_width2=2*image_width;
image_length2=2*image_length;

/*Allocating memory for image transfer buffer.*/
w=(unsigned char **)malloc(N*sizeof(char *));
for(i=0;i<N;i++)
*(w+i)=(char *)calloc(image_width2,sizeof(char));
/*Clear image transfer buffer.*/
for(i=0;i<N;i++)
for(j=0;j<image_width2;j++)
w[i][j]=(unsigned char)0;

/*Allocating memory for input and output buffers.*/
buffi=(char *)malloc(image_width*sizeof(char));
buffo=(char *)malloc(image_width2*sizeof(char));
bufft=(unsigned int
*)malloc(image_width2*sizeof(int));
fptrt=fopen("temp.img","wb+");
max=0; min=20000;
xt=wherex();
yt=wherey();
gotoxy(70,25);
textattr(WHITE+(GREEN<<4)+BLINK);
cputs("WAIT");


/*Interpolation.*/
for(n1=0;n1<image_length2;n1++)
{
if((n1%2)==0)
fread(buffi,image_width,sizeof(char),fptri);
else /* Every other row is set to zero. */

×