You are on page 1of 22

Chng

7
Cc thuc tnh ca nh s
7.1 Ch dn
Trong chng ny chng ta s nghin cu cc vn sau:
Tm quan trng ca pha trong cc nh s.
Cc gi thit ly mu 2-D vi cc ng dng trn cc nh.
Nhn i phn gii trn nh.
7.2 Tm quan trng ca pha
Trong chng 6, phn 6.4.2, tm quan trng ca c tnh tuyn tnh hoc
c tnh pha zero cho cc b lc 2-D c cp. Tuy nhin, chng ta cha
kim tra tc dng phn b c tnh pha ca cc nh s i vi cc ni dung
thng tin c trn nh. lm vy, chng ta s a ra hai th nghim.
Th nghim 1:
1. Rt ra 2-D FFT ca mt nh c cho.
2. Tnh c tuyn pha:

,
_


) (
) (
tan
k x
k x
r
i
k

y x
i
(k) biu din cho cc phn gi tr o v x
r
(k) biu din cc gi tr
thc ca FFT.
3. Tnh ton v lu trong mt file cc gi tr phc
cos( ) sin( ),
k k
i + i = - 1
4. Rt ra bin i ngc FFT ca file cui cng.
a cc bc trn, chng trnh 7.1 c cung cp. Chng trnh thc hin
trn nh IKRAM.IMG ca hnh 3.2a (Chng 3). Kt qu c a ra trn hnh
7.1.
125
Chng trnh 7.1 "PHASE.C". Kim tra tm quan trng ca pha.
/* Program for testing the importance of phase in digital images.*/
#define pi 3.141592654
#include <stdio.h>
#include <math.h>
#include <alloc.h>
#include <stdlib.h>
#include <io.h>
#include <string.h>
#include <conio.h>
void bit_reversal(unsigned int *, int , int);
void WTS(float *, float *, int, int);
void FFT(float *xr, float *xi, float *, float *,int, int);
void transpose(FILE *, int, int);
void FFT2D(FILE *, FILE *, float *, float *, unsigned int *,
int,int,int);
Hnh 7.1 Tch ring pha i vi nh "IKRAM.IMG".
void main()
{
int N,n2,m,i,j,NT;
unsigned int *L;
float *wr,*wi ;
double nsq,xr,xi,theta;
FILE *fptri,*fptro,*fptrt,*fptrr;
float *buffi,*buffo, max,min,scale;
126
unsigned char file_name[14], *buff,file_name2[14];
clrscr() ;
printf("Enter name of file containing FFT data-->");
scanf("%s",file_name);
fptri=fopen(file_name,"rb");
if(fptri==NULL)
{
printf("\nFile does not exist.");
exit(1);
}
fptrt=fopen("temp.img","wb+");
again :
gotoxy(1,2);
printf(" ");
gotoxy(1,2);
printf("Enter File for storing display IFFT data->");
scanf("%s",file_name);
if(((stricmp("temp.img",file_name2))==0)||
((stricmp("temp2.img",file_name2))==0))
printf("This is a reserved file name. Use some other name.");
goto again;
fptrr=fopen(file_name,"wb");
nsq=(double)filelength(fileno(fptri))/(2*sizeof(float));
N=(int)sqrt(nsq);
m=(int)(log10((double)N)/log10((double)2));
clrscr( ) ;
NT=2*N*sizeof(float);
buffi=(float *)malloc(NT*sizeof(float));
buffo=(float *)malloc(NT*sizeof(float));
buff=(char *)malloc(N*sizeof(char));
for(i=0;i<N;i++)
{
fread(buffi,NT,1,fptri);
for(j=0;j<N;j++)
{
xr=(double)buffi[2*j];
xi=(double)buffi[2*j+1];
theta=atan2(xi,xr);
buffo[2*j]=100.0*(float)cos(theta);
buffo[2*j+1]=100.0*(float)sin(theta);
}
fwrite(buffo,NT,1,fptrt);
}
fclose(fptri);
rewind(fptrt);
/* Allocating memory for bit reversal LUT.*/
L=(unsigned int *)malloc(N*sizeof(unsigned int));
/* Generate Look-up table for bit reversal.*/
bit_reversal(L,m,N);
/* Allocating memory for twiddle factors.*/
n2=(N>>1)-1;
wr=(float *)malloc(n2*sizeof(float));
127
wi=(float *)malloc(n2*sizeof(float));
fptro=fopen("temp2.img","wb+"),
WTS(wr,wi,N,1);
FFT2D(fptrt,fptro,wr,wi,L,N,m,1);
fptro=fopen("temp2.img","rb");
max=0.0; min=1.e10;
for(i=0;i<(N-30);i++)
{
fread(buffi,NT,1,fptro);
if(i<11) continue;
for(j=0;j<N;j++)
Hnh 7.2 Tch ring bin ca nh "IKRAM.IMG".
{
if(buffi[2*j]>max) max=buffi[2*j];
if(buffi[2*j]<min) min=buffi[2*j];
}
}
rewind(fptro);
scale=255.0/(max-min);
for(i=0;i<N;i++)
{
fread(buffi,NT,1,fptro);
for(j=0;j<N;j++)
buff[j]=(char)((buffi[2*j]-min)*scale);
fwrite(buff,N,1,fptrr);
}
fcloseall();
remove("temp.jmq");
remove("tempLimg");
}
128
Th nghim 2:
1. Rt ra FFT ca mt nh.
2. Rt ra c tnh bin
| | ( ) ( ) H x k x k
k
r i
+
2 2
3. Cha trong mt file d liu phc
(| | . ) H i
k
+ 0 0
4. Rt ra bin i ngc FFT ca file cui cng.
Chng trnh 7.1 c th d dng thay i li kt hp vi cc bc trn. Kt
qu chy th nghim 2 trn nh IKRAM.IMG c cho hnh 7.2.
C th thy r rng t hai th nghim trn rng c tnh pha mang gn
ht cc thng tin trong nh. iu ny ng vi hu ht cc nh, bi vy khi thc
hin cc php ton nh lc 2-D vi mc ch tng cng nh ta nn trnh lm bin
dng pha. iu ny cho thy s cn thit ca cc ton t 2-D tuyn tnh hoc
c pha zero.
7.3 nh l ly mu Whittaker-Shannon
Chng ta s bt u bng xem xt nh l ly mu trong trng hp 1-D. Sau
y chng ta s nghin cu trng hp cc tn hiu 2-D.
nh l: Nu mt hm x(t) khng cha tn s cao hn W, c th xc nh
mt cch hon ton bng mt to m dy cc im chia cch nhau 1/(2W).
Chu k ly mu c cho bi
T
W

1
2

y T tnh theo giy v W tnh theo herzt.
Chng minh. Xem xt biu din Fourier ca mt dy cc tn hiu lin tc
x
a
(t)

d e j X t x
t j
a a
) (
2
1
) (

(7.1a)



dt e t x j X
t j
a a
) ( ) (
(7.1b)
Nu x(n) biu din mt dy c rt ra t vic ly mu x
a
(t) ti cc khong bng
nhau T, chng ta c th dng biu thc (7.1a) vit:

d e j X nT x n x
nT j
a a
) (
2
1
) ( ) (

(7.2)
T bin i ri rc Fourier chng ta cng rt ra

d e e X n x
n j j
) (
2
1
) ( (7.3)
129
y X e
j
( )

l bin i Fourier ri rc ca x(n). By gi cn tnh mi quan


h ) ( j X
a
theo X e
j
( )

.
xem xt mi quan h gia cc biu thc (7.2) v (7.3) ta cn xem xt
biu thc (7.2) nh mt tng ca cc tch phn trong cc khong c di
2/T.


r
T
r
T
r
nT j
a
d e j X n x

) 1 2 (
) 1 2 (
) (
2
1
) ( (7.4)
Mi phn trong tng c th quy v tch phn trong khong t
T

n
T

bng cch thay i bin rt ra

+
r
T
T
rn j nT j
a
d e e
T
r
j j X n x

2
)
2
(
2
1
) ( (7.5)
Nu thay i th t ca tch phn v tnh tng v ch rng
1
2

rn j
e


vi mi gi tr nguyn ca r v n, th chng ta rt ra

1
1
]
1


d e
T
r
j j X n x
nT j
T
T
r
a

)
2
(
2
1
) ( (7.6)
Vi thay th , biu thc (7.6) tr thnh

d e
T
r
j
T
j X
T
n x
n j
r
a


1
1
]
1

+ )
2
(
1
2
1
) ( (7.7)
c cng dng vi biu thc (7.3). V vy, chng ta c th xc nh


+
r
a
j
T
r
j
T
j X
T
e X )
2
(
1
) (

(7.8)
Tng t, chng ta c th biu din biu thc (7.8) theo bin tn s tng t
nh

+
r
a
T j
T
r
j j X
T
e X )
2
(
1
) (

(7.9)
Biu thc (7.8) v (7.9) cung cp mi quan h gia bin i Fourier thi
gian lin tc v bin i Fourier ca mt dy cc mu. Cho v d, nu
X j
a
( ) c gii thiu trong hnh 7.3a, th X e
j
( )

s c gii thiu trong


hnh 7.3b nu W (/T) (hoc T (2/2W)), c th, nu W tnh theo hezt
) 2 / 1 ( W T . V th, nu ly mu ti tc ti thiu gp i tn s cao nht
trong x j
a
( ) , th X e
j
( )

c xc nh thnh X j
a
( ) trong khong
130
T T

. Tn s ly mu ny thng c gi l tn s Nyquist. Nu T 1/
(2W), th cc bn dch ca X j
a
( ) s b chng ln nhau nh trong hnh 7.3c.
Vn ny gi l hin tng trm ph (aliasing).
Nu T 1/(2W) (W tnh theo hezt), th c kh nng khi phc x
a
(t) t x(nT)
bi mt php ni suy xp x, m s c chng ta cp n phn tip theo.
T php bin i Fourier thi gian lin tc:


T
T
t j
a a
d e j X t x

) (
2
1
) ( (7.10)
Hnh 7.3 Ph tn s ca trng thi lin tc v trng thi ly mu ca mt
tn hiu.
Nu
T T
- ) (
1
) (

j X
T
e X
a
T j
(7.11)
v

k
kT j
a
T j
e kT x e X ) ( ) (
Kt hp biu thc (7.10) v (7.11)
131

w
-w
X
a
(j)
Bin i Fourier ca
tn hiu lin tc
(a)
w
T
>

-w w

X(e
j
)
(b)
T

w

w
T
<

-w
(c)
T


T
T
t j
a
d e j X
T
t x

) (
2
) (
V vy

1
]
1


d e e kT x
T
t x
t j
T
T
k
Tk j
a a

) (
2
) (
Thay i th t tnh tng v tch phn,




1
1
]
1


k
T
T
kT t j
a a
d e
T
kT x t x
/
/
) (
2
) ( ) (

Tnh gi tr ca tch phn chng ta c

k
a a
kT t
T
kT t
T
kT x t x
) (
) ( sin
) ( ) (

(7.12)
Biu thc (7.12) l php ni suy cho php khi phc cc tn hiu lin tc theo
thi gian x
a
(t) t cc mu ca n.
Trng hp 2-D: Cc nh l ly mu 2-D gi thit rng hm gii hn bng
f
a
(x,y) c th khi phc mt cch hon ton t cc mu m tho mn
T
W
H
H

1
2
T
W
V
V

1
2
(7.13)
y W
H
v W
V
biu din gii thng theo hezt ca tn hiu 2-D theo chiu
dc v theo chiu ngang. Nu biu thc (7.13) c tho mn, v f
a
(k
1
T
V
,k
2
T
H
)
biu din tn hiu ly mu 2-D, th f
a
(x,y) c th khi phc t f
a
(k
1
T
V
,k
2
T
H
) dng
biu thc ni suy:

(7.14)
Chng minh ca cc biu thc (7.13) v (7.14) tng t nh trng hp 1-D v -
c li nh mt bi tp. Mt ch l ph tn s ca mt tn hiu ly mu 2-D
tun hon trong min tn s, nh trong hnh 7.4. Tn hiu tng t c th c
khi phc bng cch tch ra mt chu k t ph tn hiu mu.
132

) (
) ( sin
2
2
H
H
H
H
T k y
T
T k y
T

7.4 nh l ly mu p dng ln cc nh
Mt nh c to nn bng cch chiu mt cnh 3-D ln mt phng 2-D. Php
chiu ny biu din mt php bin i t nhiu vo mt. C ngha l mt
im nh khng tng ng duy nht vi mt im trong cnh 3-D. iu ny c
minh ho qua hnh 7.5. Gi s rng nh ny cha N im nh theo hng x. Sau
cho cc vt th S
1
v S
2
trong hnh 7.5 chng ta c
x
N
1
10
mt (7.15)
v
N
x
3
2
mt (7.16)
nh l ly mu i hi:
1
1
2
1
W
x
(7.17)
Hnh 7.4 Ph tn s ca mt tn hiu ly mu 2-D.
v
2
2
2
1
W
x
(7.18)
y, W
1
l tn s cao nht theo hng x cho nh c to bi mt mnh vt th
S
1
, v W
2
l tn s cao nht theo hng x cho nh c to bi mt mnh vt th
S
2
. Bi v chng ta ch c mt nh bao gm S
1
v S
2
nn W
1
= W
2
v N c xc
nh theo
133
1

H
T

H
T

V
T

V
T

H
W
V
W
x
N W
2
2
3 1
2
(7.19)
V th N = 6W
2
(7.20)
Hnh 7.5 nh x mt cnh 3-D ln mt phng nh 2-D.
Thay N trong biu thc (7.15) chng ta c
1 1 2
1
2
1
6
10
6
10
W W W
x >
V th, gi thit ly mu khng tho mn cho vt th S
1
, v y cc thng
tin ca min ny trn nh khng th khi phc qua php ni suy. Ni mt cch
khc, cc vt th gn camera c kh nng tt hn khi ly mu, trong khi cc vt
th xa camera th d dng khi khi phc mu.
7.5 Nhn i phn gii trn nh
Kt qu ca nh l ly mu c th dng tng phn gii trn nh. D
sao chng na, da vo kt qu t c chng ta c th kt lun rng ni
chung th khng th tng phn gii ca nh ln c. Trong cc nh, cc vt
th ng quan tm thng thng c che ht b mt ca nh. V th, nu
cc i tng trong nn khng c cho phn gii d tha, cc vt th cn
cnh thng thng c mu cho php dng cc nh l ly mu.
7.5.1 Nhn i phn gii dng s th hin tn s ca l
thuyt ly mu
Chng ta ch trong chng 6 rng ph tn s ca mt nh gim nhanh
cng vi s tng ca tn s. Chng ta c th dng nhn xt ny v nh l ly
mu Whittaker-Shannon tng phn gii. Gii thut ny theo cc bc sau:
134
1. Rt ra FFT ca nh c kch thc N N, c th
I k k FFT i n n
n n
( , ) { ( , )( ) }
1 2 1 2
1
1 2

+
ph tn s s phi c gc to trung tm ca mng FFT.
2. Thm cc im 0 vo FFT nh gii thiu trong hnh 7.6 tng kch thc
ca n ln 2N 2N.
3. Rt ra bin i ngc FFT ca bin i tn s m rng. Kt qu thu c l
nh gc vi phn gii tng gp i. Cn ch l cc vt th khng ly
mu trong nh gc th khng cung cp mt s pht trin trong
phn gii.
thc hin cc phng php trn chng ta s bt u vi nh kch thc 128
128 im v 256 mc xm trong hnh 7.7a. nh c sn trn a vi file
c tn l CAMEL.IMG. Thc hin thut ton trn c cho trong Chng trnh 7.2.
Kt qu p dng trn nh c cho trong hnh 7.7b. R rng l rt kh khn
nhn ra ngi ci trn lc , nu nh gc mt ngi cha c ly mu.
Chng trnh 7.2 ENLQFFT.C nhn i phn gii ca mt nh
qua FFT.
/*This program utilizes 2-D FFT to double the size of an image.*/
#define pi 3.141592654
#include <stdio.h>
#include <math.h>
#include <alloc.h>
#include <stdlib.h>
#include <io.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>
void bit_reversal(unsigned int *, int , int);
void WTS(float *, float *, int, int);
void FFT(float *xr , float *xi, float *, float *, int , int) ;
void transpose(FILE *, int, int);
void FFT2D(FILE *, FILE *, float *, float *, unsigned int *,
int,int,int);
void main()
{
int N,m,i,j,N2,N4,m2,n2,ind;
unsigned int *L;
float *wr,*wi;
FILE *fptri,*fptro,*fptrt;
double nsq;
float *buffo,*buffi;
unsigned char file_name[14],ch,*buffr;
float max,min,scale;
clrscr();
printf("Enter file name for image to be enlarged -> ");
scanf("%s", file_name);
fptri=fopen(file_name,"rb");
135
if(fptri==NULL)
{
printf("\n File does not exist.");
exit(1);
}
nsq=filelength(fileno(fptri));
N=(int)sqrt(nsq);
m=(int)(log10((double)N)/log10((double)2));
fptro=fopen("FFT1.img","wb+");
/* file for storing FFT of image.*/
again :
gotoxy(1,2);
printf ( " ");
gotoxy(1,2);
printf("Enter file-name for enlarged image -> ");
scanf("%s",file_name);
if(((stricmp("FFT1.img",file_name))==0)||
((stricmp("temp.img",file_name))==0)||
((stricmp("IFFT2.img",file_name))==0))
{
printf("This is a reserved file name. Use some other name.");
Hnh 7.6 Thm cc s 0 vo FFT thu c nh c tn s m rng.
goto again;
}
gotoxy(1,2);
printf ( " ");
ind=access(file_name,0);
while(!ind)
{
gotoxy(1,3);
printf("File exists. Wish to overwrite? (y or n)-->");
136
FFT ca nh N
N
2

2
2
T
T

0s 0s
0s
0s
2
N
2
N
while(((ch=tolower(getch()))!='y')&&(ch!='n'));
putch(ch);
switch(ch)
{
case 'y' :
ind=1 ;
break ;
case 'n' :
gotoxy(1,3);
printf(" ");
gotoxy(1,2);
printf (" ");
gotoxy(1,2);
printf("Enter file name -->");
scanf("%s",file_name);
ind=access(file_name,0);
}
}


Hnh 7.7 "CAMEL.IMG" v nh c phng to.
137
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);
/* 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);
138
clrscr() ;
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 Nhn i phn gii trn nh dng php ni suy khng
gian
Trong phng php ny nh u tin c cha ln mt mng c kch thc 2N
2N vi tt c cc hng v cc ct c xo v khng. nh c chiu ln lt vi cc
php ni suy. Hnh 7.8 lit k cc phn ca cc php ni suy ph bin nht.
Gii thut ni suy vung l thao tc sao chp trong nhng im nh v c
bn c sao chp vo nhng v tr rng. C th thu c ton t ni suy tam
gic nh s kt hp hai ton t ni suy vung, v vy ta c th thu c cng kt
qu nu ta p dng lin tip hai php ni suy vung. Gii thut chuyn i
nhng sao chp c bn theo b lc thng thp sang nh c c nh trn
hn. Kt hp thc hin ba ln lin tc trn nh vi ton t ni suy vung tng -
ng vi p dng ton t ni suy Bell. Ton t ni suy bc 3 c th thu c nh
kt hp php ni suy vung v Bell hoc kt hp ton t ni suy vung bn ln.
S dng cng khi nim , chng ta c th d dng c c ni suy cao hn.
139
Hnh vung: 1 1
1 1
Tam gic: 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
Hnh 7.8 Cc php ni suy thng thng.
Nhng khi nim trn c th c thc hin trong phn mm tng t vi vic
thc hin lc FIR. B m chuyn i nh kch thc N (2 rng ca nh)
c tn dng, N = 2 cho ton t ni suy vung, N = 3 cho ton t ni suy
tam gic, v.v... Mt hng ca nh c chuyn thnh hng cui cng ca b m
chuyn i nh v lu tr ti cc v tr khc vi cc v tr gia t bng
khng. Tng quan ca php ni suy vi b m truyn ti nh v dch chuyn
hng nh trong phng php trong b lc FIR, mt hng ca cc gi tr 0 c
chuyn i thnh hng cui cng ca b m chuyn i nh. Nhng bc
ny c lp i lp li cho nhng phn cn li ca nh, bng cch la chn s
chuyn i mt hng t nh theo mt hng ca cc gi tr 0. Th tc thc
hin thut ton ny c cho trong chng trnh 7.3.
Chng trnh 7.3 ENLARGE.C.
/* This program doubles the dimensions of an
image by interpolation. */
#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 } };
140
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);
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);
141
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);
}
}
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];
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;
142
/*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. */
for(j=0;j<image_width;j++)
buffi[j]=(unsigned char)0;
for(j=0;j<image_width;j++)
w[N1][j<<1]=buffi[j];/*Every other location is set to zero. */
for(n2=0;n2<image_width2;n2++)
{
sum=(int)0;
for(k1=0;k1<N;k1++)
for(k2=0;k2<N;k2++)
{
if((n2+k2-N1)<(int)0) continue;
sum+=a[k1][k2]*w[k1][n2+k2-N1];
}
bufft[n2]=sum;
if(sum>max) max=sum;
if(sum<min) min=sum;
}
fwrite(bufft,image_width2,sizeof(int),fptrt);
/* Shift rows of w. */
temp=*w;
for(j=0;j<N1;j++)
*(w+j)=*(w+j+1);
*(w+N1)=temp;
}
rewind(fptrt);
scale=(float)255.0/((float)(max-min));
for(i=0;i<image_length2;i++)
{
fread(bufft,image_width2,sizeof(int),fptrt);
143
for(j=0;j<image_width2;j++)
buffo[j]=(unsigned char)((float)(bufft[j]-min)*scale);
fwrite(buffo,image_width2,sizeof(char),fptro);
}
fclose(fptrt);
fclose(fptri);
fclose(fptro);
remove("temp.img");
gotoxy(70,25);
textattr(WHITE+(BLACK<<4));
cputs(" ");
gotoxy(xt,yt);
}
kim tra chng trnh 7.3 chng ta s s dng nh "CAMEL.IMG" m ta
s dng kim tra trong phng php tn s. Kt qu s dng ni suy bc 3
c th hin trn hnh 7.9. Nh mong i, nh hi m, v cht lng thp hn
nh thu c qua vic ng dng nh l ly mu. Tuy nhin, c th khi phc
nh m bng cch s dng k thut khng gian thng qua vic s dng trc
tip b lc thng cao. Chng ta s cp nhiu hn v vn ny trong ch-
ng 8. Tng quan v nh m s c tho lun trong chng 10.
7.6 B lc sai phn thng k Wallis
Bi v phng i nh l mt dng ca tng cng nh, s l thch hp khi gii
thiu b lc sai phn thng k ti thi im ny. Kiu lc ny c pht trin
bi Wallis l c bit hu ch trong vic tng cng cc chi tit trong vng ti.
Vn ny ny sinh trong nh x-quang nh ch trn hnh 7.10. Trong gii
thut lc Wallis mi im c iu chnh bng php tr gi tr trung bnh
ca cc im ln cn v chia vi lch chun ca cc im ln cn ca n.
nh lc Wallis thng c trung bnh vi nh gc. Nguyn nhn phi thc hin
vic trung bnh nh ny l ngn cn vic loi b ton b nn. Kch thc
ca vng ln cn thng c ni n nh bc ca b lc. Kt qu ca p dng b
lc Wallis bc 5 c ch trn hnh 7.11.
144
Hnh 7.9 Phng to nh "CAMEL.IMG" dng phng php gn ng.
Bi tp 7.1
1.Tm kim v phn tch ti liu ca H.S.Hou v H.C.Andrews. Trn c s
, vit chng trnh C cho php phng to, thu nh nh dng cc ton t bc 3.
2.Vn v min tn s cng c cp bi T.C.Chen v R.J.P.De
Figueiredo. c v phn tch ti liu .
Hnh 7.10 nh ca tia x.
145
Hnh 7.11 X l nh hnh 7.10 vi b lc Wallis.
Bi tp 7.2
Vit chng trnh C cho b lc thng k Wallis. Cho php ngi s dng c th
trung bnh nh u ra vi nh gc. Cho php ngi s dng nhp bc ca b
lc.
Kim tra chng trnh trn nh XRAY.IMG v RIBS.IMG c sn trn a km
theo.
146

You might also like