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

Nén Ảnh part 5 pptx

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 (225.78 KB, 12 trang )


357
Ch¬ng tr×nh 13.6 "IFCT2D.C". §¶o ngîc FCT. KÝch thíc khèi
sö dông trªn ¶nh gèc.
/*Program 13.6 "IFCT2D.C". Inverse FCT. Block
size used on the original image should be known
to the user.*/
/* This program is for carrying out
the inverse of the 2-D Fast Cosine Transform. */
#define PI 3.14159
#include <stdio.h>
#include <math.h>
#include <alloc.h>
#include <conio.h>
#include <io.h>
#include <process.h>

void IFCT(float *, unsigned int *, float *, int ,
int );
void bit_reversal(unsigned int *, int , int );
void WTSINV(float *,int, int);

void main()
{
int m,N,i,j,k1,k2,NB,NT,NS,k;
float *x,*C,**w;
unsigned int *L;
double nsq;
FILE *fptri, *fptro;
char file_name[14] ;
float *buffi ;


unsigned char *buffo;

clrscr();
printf ( "This program is for the inverse 2-D
FCT . \n" ) ;
printf ( "Enter name of input file >") ;
scanf ("%s " , file_name) ;
fptri=fopen(file_name, " rb");
if( fptri ==NULL)
{
printf("\nNo such file exists.\n");
exit(1);
}

358
nsq=(double)filelength(fileno(fptri));
/*
Assume image is square,
*/
N=(int)sqrt(nsq/sizeof(float));
m=(int)(log10((double)N)/log10((double)2.0));
k=1;
for(i=0; i<m;i++)
k<<=1;
if(k!=N)
{
printf("\nTransform image must have dimensions");
printf(" which are multiples of 2. \n ");
exit(1);
}

printf("Enter name for Output file - - -> " ) ;
scanf("%s", file_name);
fptro=fopen(file_name,"wb");
printf("\nEnter block size used (e.g.
8x8,16xI6,etc.) >");
scanf("%dx%d",&NB,&NB);
m=(int)(log10((double)NB)/log10((double)2.0));
NT=N*NB;
/*
Blocks of NBxNB were considered.
*/
/*
Assigning memory.
*/
buffi=(float *)malloc(NT*sizeof(float));
buffo=(unsigned char *)malloc(NT*sizeof(char));
x=(float *)malloc(NB*sizeof(float));
L=(unsigned int *)malloc(NB*sizeof(int));
C=(float *)malloc((NB-1)*sizeof(float));
w=(float **)malloc(NB*sizeof(float *));
for(i=0;i<NB;i++)
*(w+i)=(float *)malloc(NB*sizeof(float));

bit_reversal(L,m,NB);
WTSINV(C,m,NB);
NS=(N>>m) ;
/*

359
2-D inverse FCT using the row-column approach.

*/
for(i=0;i<NS;i++)
{
fread(buffi,NT,sizeof(float),fptri);
for(j=0;j<NS;j++)
{
for(k1=0;k1<NB;k1++)
for(k2=0;k2<NB;k2++)
w[k1][k2]=buffi[k1*N+k2+j*NB];
for(k1=0;k1<NB;k1++)
{
for(k2=0;k2<NB;k2++)
x[k2]=w[k1][k2];
IFCT(x,L,C,m,NB);
for(k2=0;k2<NB;k2++)
w[k1][k2]=x[k2];
}
for(k2=0;k2<NB;k2++)
{
for(k1=0;k1<NB;k1++)
x[k1]=w[k1][k2];
IFCT(x,L,C,m,NB);
for(k1=0;k1<NB;k1++)
w[k1][k2]=x[k1];
}
for(k1=0;k1<NB;k1++)
for(k2=0;k2<NB;k2++)

buffo[k1*N+k2+j*NB]=(char)fabs((double)w[k1][k2])
;

}
fwrite(buffo,NT,1,fptro);
}
fcloseall();
}
void bit_reversal(unsigned int *L, int m, int N)
{
unsigned int MASK,C,A,j,k,i;

for(k=0;k<N;k++)
{

360
MASK=1;
C=0;
for(i=0,j=m-1;i<m;i++,j )
{
A=(k&MASK)>>i;
A<<=j;
C|=A;
MASK<<=1;
}
L[k]=C;
}
}

void WTSINV(float *C,int m,int N)
{
int NK,k,i,iter,LK;
NK=4;

k=0;
LK=1;
for(iter=1;iter<=m;iter++)
{
for(i=0;i<LK;i++)
{

C[k]=1.0/(float)cos(PI*(float)(4*i+1)/(float)NK);
k++;
}
NK<<=1;
LK<<=1;
}
C[0]/=2.0;
for(i=1;i<(N-1);i++)
C[i]/=4.0;
}

void IFCT(float *x, unsigned int *L, float *C,
int m, int N)

{
int NK1,NK2,i,j,k,kk,ip,incr,L1,k1,k2,iter;
float T;


361
x[0]*=(float)N/1.414;
for(i=1;i<N;i++)
x[i]*=(float)N/2.0;


/*Recursive addition. */
NK1=1 ;
NK2=2 ;
kk=1;
for(i=0;i<m;i++)
kk*=2 ;
for(iter=1;iter<m;iter++)
{
kk>>=1;
L1=kk- 1 ;
for(k=L1;k>=1;k )
for(i=0;i<NK1;i++)
x[NK1+k*NK2+i]=x[NK1+k*NK2+i]+x[NK1+(k-
1)*NK2+i];
NK1<<=1;
NK2<<=1;
}
NK1=N>>1;
for(i=1;i<NK1;i++)
x[i]*=2.0;

/*Bit reversal. */
for(i=0;i<(N-1);i++)
{
if(i<=L[i]) continue;
else
{
T=x[i];
x[i]=x[L[i]];

x[L[i]]=T;
}
}

/*Forward operation. */
ip=1;
kk=0;
incr=2;
for(iter=0;iter<m;iter++)

362
{
for(k=0;k<ip;k++)
{
for(j=k;j<N;j+=incr)
{
i=j+ip;
T=x[j]*0.5;
x[i]=x[i]*C[kk];
x[j]=T+x[i];
x[i]=T-x[i];
}
kk++;
}
ip<<=1;
incr<<=1;
}

/*Rearranging the order of the input sequence.*/
kk=1;

for(i=1;i<(N>>1);i++)
{
T=x[N-1];
k=1;
for(j=kk;j<N;j++)
{
x[N-k]=x[N-k-1];
k++;
}
x[kk]=T;
kk+=2;
}
}


Bài tập 13.6
1. Chạy chơng trình 2-D FCT (Chơng trình 13.5) trên ảnh
IKRAM.IMG và dùng một khối có kích thớc 8 8. Tên của file xuất
ra đặt là IKRAMFCT.IMG.
2. Lấy 2-D FCT của IKRAMFCT.IMG bằng chơng trình 13.6. Chứa
kết quả trên ảnh IKRAMR.IMG.

363
3. Hiển thị IKRAMR.IMG. So sánh ảnh này với ảnh IKRAM.IMG.

Tiếp theo tôi sẽ giới thiệu một chơng trình mà sẽ hiển thị các khối 8
8 của kết quả đã chuyển đổi trên màn hình văn bản. Chú ý rằng giá trị sẽ
giảm xuống một cách nhanh chóng kể từ góc trái của màn hình, hay là
điểm tần số (0,0). Để thoát khỏi chơng trình này, bấm ESC.



Chơng trình 13.7 "DISPFCT.C". Chơng trình hiển thị khối 2-D
FCT.
/*Program 13.7 "DISPFCT.C". Program to display 2-
D FCT blocks.*/
/* This program displays the result of the 2-D
FCT. */

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

void main( )
{
int i,j,k1,k2,N,N1,NB,NS;
float *buff,nsq;
FILE *fptr;
char file_name[14];
int ch;

clrscr();
printf("Enter input file name containing 2-D
FCT result >");
scanf("%s",file_name);
fptr=fopen(file_name,"rb");
if(fptr==NULL)
{

printf("\nNo such file exists.\n");
exit(1);
}
nsq=(float)filelength(fileno(fptr));

364
N=sqrt((double)(nsq/sizeof(float)));
printf("\nEnter block size used (8x8, 16x16,
etc.) >");
scanf("%dx%d",&NB,&NB);
N1=N/NB;
NS=NB*N;
buff=(float *)malloc(NS*sizeof(float));
printf("\nPress ESC to exit.");
clrscr();
for(i=0;i<N1;i++)
{
fread(buff,sizeof(float),NS,fptr);
{
for(j=0;j<N1;j++)
{
for(k1=0;k1<NB;k1++)
{
for(k2=0;k2<NB;k2++)
printf(" %5.2f",buff[k1*N+k2+j*NB]);
printf("\n");
}
ch=getch();
if(ch==27) {
clrscr();

fclose(fptr);
exit(1);
}
clrscr();
gotoxy(1,1);
}
}
fclose(fptr) ;
}
}


Một mẫu của 2-D FCT từ một khối rút ra từ ảnh IKRAM.IMG giới
thiệu trong hình 13.13. Các thành phần một chiều (các khối có tần số 0,0)
có giá trị lớn nhất. Thành phần này biểu diễn giá trị trung bình của khối.
Các thành phần khác (đợc hiểu là các thành phần xoay chiều) phải có
các giá trị nhỏ hơn.

365
13.4.2 Sự phóng to và sự thu nhỏ của các ảnh
Một trong các ứng dụng của FCT là thay đổi kích thớc của ảnh, bao
gồm sự phóng to và sự thu nhỏ ảnh. Chúng ta sẽ chấp nhận rằng một ảnh
phóng to dùng FCT thì sẽ không có gì khác so với một ảnh phóng to dùng
FFT, trong đó ta dùng các thuộc tính vốn có của FFT của ảnh. Toàn bộ ý
tởng là ta sẽ chia tần số lấy mẫu bên trong với 2 và nh vậy sẽ tăng gấp
đôi khoảng cách tần số. Khi FCT của một khối giảm rất nhanh kể từ điểm
tần số (0,0), chúng ta có thể nhân đôi khoảng cách tần số bằng cách thêm
vào các điểm 0. Điều này làm cho chúng ta giảm đợc một nửa tần số lấy
mẫu bên trong và tăng gấp đôi kích thớc của ảnh. Vì vậy trong tơng
lai, kỹ thuật truyền hình phải sử dụng kỹ thuật ảnh số, thì tính chất này

có thể giúp cho ta tạo ra đợc các tivi có màn ảnh rộng. Sự thu nhỏ thực
hiện bằng cách bớt đi các giá trị của tần số cao.
Chơng trình sau đây sẽ phóng to của một ảnh. Chơng trình này yêu
cầu FCT của một ảnh và kích thớc của các khối trên ảnh gốc. Kết quả
chạy chơng trình này là một ảnh có kích thớc gấp đôi.


Chơng trình 13.8 "IFCT2DX.C". Chơng trình phóng to ảnh
gấp đôi dùng 2-D FCT.
/*Program 13.8 "IFCT2DX.C". Program for image
doubling using the 2-D FCT.
The 2-D FCT of the image is the input file.*/
/* This program is for carrying out the inverse
of the 2-D Fast Cosine Transform.
It doubles the size of the image at the same
time. */

#define PI 3.14159

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

void IFCT(float *, unsigned int *, float *, int ,
int );
void bit_reversal(unsigned int *, int , int );
void WTSINV(float *,int, int);



366
void main()
{
int m,N,i,j,k1,k2,NB,NS,N1,NB1,m1,NT,NT1,k;
float *x,*C,**w;
unsigned int *L;
double nsq;
FILE *fptri,*fptro;
char file_name[14];
float *buffi;
unsigned char *buffo;

clrscr();
printf ("This program is for the inverse 2-D
FCT. \n" ) ;
printf("It doubles the size of the image at the
same time.");
printf("\nEnter name of input file > " ) ;
scanf("%s",file_name);
fptri=fopen(file_name,"rb");
if(fptri==NULL)
{
printf("\nNo such file exists.\n");
exit(1);
}
nsq=(double)filelength(fileno(fptri));
/*
Assume image is square.

*/
N=(int)sqrt(nsq/sizeof(float));
m=(int)(log10((double)N)/log10((double)2.0));
k=1;
for(i=0;i<m;i++)
k<<=1;
if(k!=N)
{
printf ( "\nTransform Image must have dimensions"
) ;
printf ( " which are multiples of 2. \n" );
exit(1);
}
printf("Enter name for output file >");
scanf( "%s" , file_name);

367
fptro=fopen(file_name,"wb");
printf("\nEnter block size used (e.g. 8x8,16xl6)
>");
scanf ( "%dx%d" , &NB , &NB ) ;
m=(int)(log10((double)NB)/log10((double)2.0));
NT=N*NB;
/*
Blocks of NBxNB were considered by the
FCT. Output blocks are (2xNB)x(2xNB).
*/
m1=m+1; NB1=NB<<1;
N1=(N<<1);
NT1=N1*NB1;

/*
Assigning memory.
*/
buffi=(float *)malloc(NT*sizeof(float));
buffo=(unsigned char *)malloc(NT1*sizeof(char));
x=(float *)malloc(NB1*sizeof(float));
L=(unsigned int *)malloc(NB1*sizeof(int));
C=(float *)malloc((NB1-1)*sizeof(float));
w=(float **)malloc(NB1*sizeof(float *));
for(i=0;i<NB1;i++)
*(w+i)=(float *)malloc(NB1*sizeof(float));
bit_reversal(L,m1,NB1);
WTSINV(C,m1,NB1);
NS=(N>>m);
/*
2-D IFCT using the row-column approach.
*/
for(i=0;i<NS;i++)
{
fread(buffi,NT,sizeof(float),fptri);
for(j=0;j<NS;j++)
{
for(k1=0;k1<NB1;k1++)
for(k2=0;k2<NB1;k2++)
w[k1][k2]=0.0;
for(k1=0;k1<NB;k1++)
for(k2=0;k2<NB;k2++)
w[k1][k2]=buffi[k1*N+k2+j*NB];
for(k1=0;k1<NB1;k1++)


368
{
for(k2=0;k2<NB1;k1++)
x[k2]=w[k1][k2];
IFCT(x,L,C,m1,NB1);
for(k2=0;k2<NB1;k2++)
w[k1][k2]=x[k2];
}
for(k2=0;k2<NB1;k2++)
{
for(k1=0;k1<NB1;k1++)
x[k1]=w[k1][k2];
IFCT(x,L,C,m1,NB1);
for(k1=0;k1<NB1;k1++)
w[k1][k2]=x[k1];
}
for(k1=0;k1<NB1;k1++)
for(k2=0;k2<NB1;k2++)
buffo[k1*N1+k2+j*NB1]=(unsigned
char)fabs(w[k1][k2]);
}
fwrite(buffo,NT1,1,fptro);
}
fcloseall();
}

Bài tập 13.7
1. Sử dụng chơng trình 13.8 cho IKRAMFCT.IMG (ảnh mà bạn đã
thu đợc trớc đây). Hiển thị ảnh đã đợc phóng to.
2. Lặp lại công việc, sử dụng khối 16 16. Chú ý rằng bạn sẽ phải

chạy lại chơng trình 2-D FCT.
3. Thay đổi chơng trình trên cho ảnh thu nhỏ.
13.5 Lợng tử hoá
Trong toàn bộ cuốn sách này chúng ta làm việc với các ảnh đã lợng tử
hoá. Chúng ta coi rằng các ảnh đợc chia làm các mức xám dùng 8 bit
với các giá trị từ 0 đến 255. Tuy vậy, chúng ta không bao giờ để ý đến
các giá trị này. Lý do là các giá trị này bản thân nó không có ý nghĩa gì
cả. Cái mà chúng ta thật sự quan tâm là biểu diễn dới dạng nhị phân của
giá trị cờng độ sáng tín hiệu lấy mẫu. Tất cả các bít biểu diễn khoảng
của tín hiệu chói. Nếu tín hiệu chói có giá trị trong khoảng từ giá trị nhỏ
nhất y
L
cho đến giá trị lớn nhất y
u
, thì hàm lợng tử hoá sẽ chia tín hiệu ra

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×