Você está na página 1de 71

n tt nghip

Tm hiu mt s
phng php nn nh






n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 1

Li cm n !

Em xin by t lng bit n su sc nht ti PGS. TS. Ng Quc To,
thy tn tnh h- ng dn v gip em rt nhiu trong qu trnh thc tp
v tm hiu nghin cu ti - c giao em c th hon thnh tt bo co
tt nghip ca mnh.
Em xin chn thnh cm n s dy bo ca cc thy gio, c gio Khoa
Cng Ngh Thng Tin - Tr- ng i hc Dn Lp Hi Phng trang b cho
em nhng kin thc c bn em c th hon thnh bo co tt nghip v
ti - c giao.
Trong qu trnh nghin cu ti mc d - c cc thy c gio
h- ng dn tn tnh nh- ng do nhiu nguyn nhn ch quan v khch quan
nn ti khng trnh khi sai st .Em rt mong - c cc thy c gio ch
dn ,ng gp nhng kin qu bu gip em hon thin hn bo co tt
nghip , pht trin v m rng ti oc giao .
Em xin chn thnh cm n !

Hi Phng, ngy thng nm 2007
Sinh vin

T Minh Thng





n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 2
Mc lc
M u ............................................................................................................................ 3
CH-ng I: Gii thiu tng quan v nn nh ........................................... 4
I.1.Gii thiu v nh s v x l nh s ....................................................................... 4
I.1.1.nh s ................................................................................................................ 4
I.1.2.X l nh s....................................................................................................... 5
I.2.Mc ch v s cn thit ca nn nh .................................................................. 6
I.3.Cc khi nim c bn .............................................................................................. 7
Ch-ng II: Cc ph-ng php nn nh ......................................................... 9
II.1.Cch phn loi cc ph-ng php nn nh ........................................................... 9
II.1.1.Cch phn loi da vo nguyn l nn ......................................................... 9
II.1.2.Cch phn loi da vo cch thc thc hin nn......................................... 9
II.1.3.Cch phn loi da vo l thuyt m ho .................................................. 10
II.1.4.Qu trnh nn v gii nn ............................................................................ 10
II.2.Ph-ng php m ho di lot RLE ............................................................... 11
II.2.1.Nguyn tc .................................................................................................... 11
II.2.2.Thut ton ..................................................................................................... 13
II.2.3.Mt s th tc ch-ng trnh ....................................................................... 14
II.3.Ph-ng php m ho Huffman .......................................................................... 19
II.3.1. Nguyn tc .................................................................................................... 19
II.3.2. Thut ton .................................................................................................... 19
II.3.3.Mt s th tc ch-ng trnh ....................................................................... 24
II.4.Ph-ng php m ho LZW................................................................................. 27
II.4.1.Nguyn tc ..................................................................................................... 27
II.4.2. Thut ton .................................................................................................... 31
II.4.3.Mt s th tc ch-ng trnh ....................................................................... 34
II.5.Ph-ng php m ho JPEG ................................................................................ 38
II.5.1.Nguyn tc ..................................................................................................... 38
II.5.2.Thut ton ..................................................................................................... 38
II.5.3.Mt s th tc ch-ng trnh ....................................................................... 48
II.6.Ph-ng php m ho JPEG2000 ........................................................................ 54
II.6.1. Lch s ra i v pht trin chun J PEG2000 .......................................... 54
II.6.2.Cc tnh nng ca J PEG2000 ...................................................................... 54
II.6.3.Cc bc thc hin nn nh theo chun JPEG2000 ................................. 55
II.6.3.1.X l trc bin i ............................................................................... 55
II.6.3.2. Bin i lin thnh phn ..................................................................... 55
II.6.3.3. Bin i ring thnh phn (bin i Wavelet) .................................... 56
II.6.3.4. Lng t ho - Gii lng t ho ...................................................... 57
II.6.3.5. M ho v kt hp dng d liu sau m ho ...................................... 58
II.6.3.6. Phng php m ho SPI HT ............................................................... 59
II.6.3.7. Phng php m ho EZW .................................................................. 60
II.6.4.So snh chun J PEG2000 vi J PEG v cc chun nn nh tnh khc .... 62
Ch-ng III: Ci t ch-ng trnh v th nghim ............................ 66
Kt lun...................................................................................................................... 69

n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 3
M u

Ngy nay, cng vi s pht trin khng ngng ca khoa hc v cng
ngh th my tnh ng vai tr ngy cng quan trng v khng th thiu
trong cuc sng x hi loi ng- i. Vic trao i thng tin ca con ng- i
trong tt c cc ngnh, cc lnh vc ca i sng ngy cng tr nn cn thit
cng vi s ra i v pht trin ca mng Internet.
X l nh l mt ngnh khoa hc cn t- ng i mi m so vi nhiu
ngnh khoa hc khc nh- ng n ang - c tp trung nghin cu v pht trin
v nhng ng dng thc tin ca n trong nhiu ngnh , lnh vc khc nhau.
Trong Nn nh l mt phn ca x l nh c ng dng to ln trong
truyn thng v trong l- u tr, c rt nhiu ph- ng php nn nh - c ra
i v khng ngng - c ci tin ngy cng hon thin em li hiu qu
nn cao v cho cht lng nh tt nht. Trong n tt nghip TM hiu
mt s ph-ng php nn nh c s hng dn ca PGS .TS .
Ng Quc To em i su nghin cu mt s ph- ng php nn nh ph
bin nh- : m lot di RLE, HUFFMAN, LZW, JPEG v ph- ng php nn
nh JPEG2000 da trn bin i Wavelet vi nhng c tnh v- t tri so vi
cc chun nn tr- c em li hiu qu nn cao , cho nh nn cht l- ng
tt v nhiu nhng - u im khc m cc chun nn tr- c khng th c.
Ni dung n tt nghip bao gm cc phn chnh nh- : ch- ng mt
gii thiu tng quan v x l nh, mc ch ch- ng ny l gii thiu mt s
khi nim cn bit v nh s v x l nh s. Ch- ng hai s gii thiu mt
s ph- ng php nn nh v cch phn loi cc ph- ng php nn nh.
Ch- ng ba s gii thiu v ch- ng trnh th nghim v kt qu t c ca
ch- ng trnh. Cui cng s l phn kt lun nh gi kt qu nghin cu thu
- c v h- ng pht trin ca ti.

n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 4

CH-ng I:
Gii thiu tng quan v nn nh

I.1.Gii thiu v nh s v x l nh s:

I.1.1.nh s:
nh c th biu din d- i dng tn hiu t- ng t hoc tn hiu s. Trong
biu din s ca cc nh a mc xm, mt nh - c biu din d- i dng
mt ma trn hai chiu. Mi phn t ca ma trn biu din cho mc xm hay
c- ng ca nh ti v tr . Mi phn t trong ma trn - c gi l mt
phn t nh, thng th- ng k hiu l PEL (Picture Element) hoc l im
nh (Pixel).

- Vi nh a cp xm: Nu dng 8 bit (1 byte) biu din mc xm, th
s cc mc xm c th biu din - c l 2
8
hay 256. Mi mc xm - c
biu din d- i dng l mt s nguyn nm trong khong t 0 n 255, vi
mc 0 biu din cho mc c- ng en nht v 255 biu din cho mc
c- ng sng nht.

- Vi nh mu: Cch biu din cng t- ng t nh- vi nh en trng, ch
khc l cc s ti mi phn t ca ma trn biu din cho ba mu ring r
gm: (red), lc (green) v lam (blue). biu din cho mt im nh
mu cn 24 bit, 24 bit ny - c chia thnh ba khong 8 bit. Mi khong ny
biu din cho c- ng sng ca mt trong cc mu chnh.

n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 5










Hnh 1.1 Biu din ca mt mc xm ca nh s.
I.1.2.X l nh s:

X l nh l mt khoa hc mc d cn t- ng i mi so vi nhiu
ngnh khoa hc khc ,nht l trn quy m cng nghip. X l nh s c rt
nhiu ng dng nh- lm ni cc nh trong y hc, khi phc li nh do tc
ng ca kh quyn trong thin vn hc, tng c- ng phn gii ca nh
truyn hnh m khng cn thay i cu trc bn trong ca h thng chuyn
ti, nn nh trong khi truyn i xa hoc l- u tr.
Cc giai on chnh trong x l nh c th - c m t trong hnh sau:









sng trung bnh trong mi hnh
ch nht = gi tr mt im nh.
Pixel
or PEL
CAMERA
SENSOR
Thu nhn
nh
S ho
Phn
tch nh
H quyt
nh
Nhn dng
ddngng
L- u tr
L- u tr
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 6
I.2.Mc ch v s cn thit ca nn nh :

Nn nh l mt k thut m ho cc nh s ho nhm gim s l- ng
cc bit d liu cn thit biu din nh. Mc ch l gim i nhng chi ph
trong vic l- u tr nh v chi ph thi gian truyn nh i xa trong truyn
thng nh- ng vn m bo - c cht l- ng ca nh. Nn nh thc hin - c
l do mt thc t: thng tin trong bc nh khng phi l ngu nhin m c
trt t , t chc.V th nu bc tch - c tnh trt t, cu trc th s bit
phn thng tin no quan trng nht trong bc nh biu din v truyn i
vi s l- ng t bit hn so vi nh gc m vn m bo tnh y ca thng
tin. bn nhn qu trnh gii m s t chc, sp xp li - c bc nh xp x
gn chnh xc so vi nh gc nh- ng vn tha mn cht l- ng yu cu. D- i
y l v d v l- u tr nh s v truyn i xa vi - ng truyn 9600 baud
(9600 bps) thy r s cn thit ca vic nn nh:
nh a cp xm hay nh 256 mu c kch th- c 800 x 600, 8 bit/im
nh, cn 3.840.000 bit l- u tr v mt 6.67 pht truyn.
nh mu RGB (24 bit/im nh ) cng phn gii nh- vy cn hn 10
triu bit l- u tr v 20 pht truyn.
Mt phim m bn c kch th- c 24 36 mm (35 mm) chia bng cc
khong cch nhau 12 m, vo khong 3000 2000 im, 8 bit / pixel, yu
cu 48 triu bit cho l- u gi nh v 83 pht truyn.
Qua v d trn ta thy nhiu vn trong vic l- u tr v truyn ti nh
s ho. Nn nh c nhiu ng dng trong thc t nh- : truyn cc vn bn
ho qua - ng in thoi (Fax), nn nh trong y t v truyn hnh
cp.Chnh s ng dng trong nhiu lnh vc ca nn nh cng vi s tin
b trong lnh vc vi in t dn n s ra i cc chun nn nh.
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 7
Nn nh t - c bng cch loi b cc phn d- tha trong nh
- c s ho. D- tha c th l d- tha thng tin v khng gian, d- tha v
cp xm hay d- tha v thi gian:
D- tha thng tin v khng gian : trong mt bc nh lun tn ti s
t- ng quan gia cc im nh cnh nhau.
D- tha thng tin v cp xm :l d- tha da vo s t- ng quan gia
cc mu sc cnh nhau.
D- tha thng tin v thi gian : Trong mt chui nh video, tn ti s
t- ng quan gia cc im nh ca cc frame khc nhau .

I.3.Cc khi nim c bn:
Pixel (picture element) : phn t nh
nh trong thc t l mt nh lin tc v khng gian v v gi tr sng.
c th x l nh bng my tnh cn thit phi tin hnh s ho nh. Nh-
vy mt nh l mt tp hp cc pixel. Mi pixel l gm mt cp to x, y
v mu. Cp to x,y to nn phn gii (resolution). Mn hnh my tnh
c nhiu loi vi phn gii khc nhau: 320 x 200, 640x350, 800x600,
1024x768,

Mc xm (Graylevel)
Mc xm l kt qu s m ho t- ng ng ca mi c- ng sng ca
mi im nh vi mt gi tr s kt qu ca qu trnh l- ng ho .

D liu
Trong mt bi ton, d liu bao gm mt tp cc phn t c s m ta gi
l d liu nguyn t. N c th l mt ch s, mt k t, ... nh- ng cng c
th l mt con s, mt t, ... iu ph thuc vo tng bi ton.

n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 8
Nn d liu
Nn d liu l qu trnh gim dung lng thng tin d tha trong d
liu gc v lm cho l- ng thng tin thu - c sau nn th- ng nh hn d liu
gc rt nhiu. Do vy, tit kim - c b nh v gim thi gian trao i d
liu trn mng thng tin m li cho php chng ta khi phc li d liu ban
u.

T l nn
T l nn l mt trong cc c tr- ng quan trng ca mi ph- ng php
nn. T l nn - c nh ngha nh- sau:
T l nn = 1/r*%
vi r l t s nn - c nh ngha:
r = kch th-c d liu gc / kch th-c d liu nn.
Nh- vy hiu sut nn = (1- t l nn)*100%.
i vi nh tnh, kch th-c chnh l s bit biu din ton b bc nh.
i vi nh video, kch th-c chnh l s bit biu din mt khung
hnh video (video frame).












n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 9
Ch-ng II:
Cc ph-ng php nn nh

II.1.Cch phn loi cc ph-ng php nn nh:

II.1.1.Cch phn loi da vo nguyn l nn:

Nn bo ton thng tin (losses compression): bao gm cc ph- ng php
nn m sau khi gii nn s thu c chnh xc d liu gc.Tuy nhin nn
bo ton thng tin ch t hiu qu nh so vi ph- ng php nn khng bo
ton thng tin.
Nn khng bo ton thng tin (lossy compression): bao gm cc ph- ng
php nn sau khi gii nn s khng thu - c d liu nh- bn gc. Cc
ph- ng php ny c gi l tm l th gic l li dng tnh cht ca
mt ng- i chp nhn mt s vn xon trong nh khi khi phc li.Ph- ng
php ny lun em li hiu qu cao do loi b i nhng thng tin d- tha
khng cn thit.

II.1.2.Cch phn loi da vo cch thc thc hin nn:

Ph- ng php khng gian (Spatial Data Compression ): cc ph- ng php
ny thc hin nn bng cch tc ng trc tip ln vic ly mu ca nh
trong min khng gian.
Ph- ng php s dng bin i (Transform Coding): gm cc ph- ng
php tc ng ln s bin i ca nh gc ch khng tc ng trc tip.


n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 10
II.1.3.Cch phn loi da vo l thuyt m ho:

Cc ph- ng php nn th h th nht: gm cc ph- ng php c mc
tnh ton n gin nh- ly mu , gn t m,.
Cc ph- ng php nn th h th hai: gm cc ph- ng php da vo mc
bo ho ca t l nn bng cch s dng cc php ton t hp u ra mt
cch hp l hoc s dng biu din nh nh- : ph- ng php kim t thp
Laplace, ph- ng php da vo vng gia tng, ph- ng php tch hp.

II.1.4.Qu trnh nn v gii nn :
Gm 2 cng on :
Nn : d liu gc qua b m ho d liu , b m ho ny thc hin nn
d liu n mt mc thch hp cho vic l- u tr v truyn dn thng tin. Qu
trnh ny s thc hin vic loi b hay ct bt nhng d- tha ca nh thu
- c thng tin cn thit nh- ng vn m bo - c cht l- ng nh.
Gii nn : d liu nn i qua b gii m d liu, b gii m s thc
hin gii nn thu - c d liu gc ban u.Vic gii nn ny th- ng phi
da vo cc thng tin i km theo d liu nn ,tu thuc vo kiu nn hay
ph- ng php nn m d liu gii nn - c c hon ton ging vi d liu
gc ban u hay khng.
Tm li qu trnh nn v gii nn d liu c th m t mt cch tm
tt theo s d- i y:

Hnh 2.1 : Qu trnh nn v gii nn
Qu trnh nn
Qu trnh gii nn
D liu gc D liu nn
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 11
II.2.Ph-ng php m ho di lot RLE:

M ho theo di lot RLE (Run Length Encoding) l mt ph- ng
php nn nh da trn s ct bt cc d- tha v khng gian (mt vi hnh
nh c vng mu ln khng i c bit i vi nh nh phn). Lot - c
nh ngha l dy cc phn t im nh (pixel) lin tip c cng chung mt
gi tr.
II.2.1.Nguyn tc :
Nguyn tc ca ph- ng php ny l pht hin mt lot cc im nh lp
li lin tip, v d :110000000000000011 .Ta thy im nh c gi tr 0 xut
hin nhiu ln lin tip thay v phi l- u tr ton b cc im nh c gi tr 0
ta ch cn l- u tr chng bng cch s dng cc cp ( di lot, gi tr).
V d:
Cho mt chui ngun d :
d =5 5 5 5 5 5 5 5 5 5 19 19 19 19 19 0 0 0 0 0 0 0 23 23 23 23 23 23 23 23

Ta s c chui mi :
(10 5) (5 19) (7 0) (8 23)
T s nn = 20/ 8 = 2.5
i vi nh en trng ch s dng 1 bit biu din 1 im nh th
ph- ng php ny t ra rt hiu qu, ta thy iu qua v d sau :
Cho mt chui ngun d:
000000000000000111111111100000000001111111111000000000000000

Ta c chui mi :
(15, 10, 10, 10, 15)
T s nn = 60 bit / (5*4 bit) = 3 ( ch s dng 4 bit th hin di
lot v khng th hin gi tr lot v nh en trng ch c 2 gi tr bit l 0
hoc l 1)
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 12
Ch :
i vi nh chiu di ca mt dy lp c th ln hn 255, nu ta dng
1 byte l- u tr chiu di th s khng . Gii php - c dng l tch
chui thnh 2 chui: mt chui c chiu di l 255, chui kia c chiu
di cn li.
Ph- ng php nn RLE ch t hiu qu khi chui lp ln hn 1 ng- ng
nht nh no hay ni cc khc trong nh cn nn phi c nhiu im nh
k nhau c cng gi tr mu.Do ph- ng php ny khng em li cho ta
kt qu mt cch n nh v n ph thuc hon ton vo nh nn ch thch
hp cho nhng nh en trng hay nh a cp xm.

V d:
Ta c mt chui ngun: d=5 7 9 11 13 18 28 38 48 58 30 35 40 45
Chui kt qu sau khi m ho :
1 5 1 7 1 9 1 11 1 3 1 18 1 28 1 38 1 48 1 58 1 30 1 35 1 40 1 45
T s nn = 14 / 28 = 0.2

Nh- vy chui sau khi m ho ln hn nhiu chui ngun ban u.
Do cn ph- ng php ci tin x l nhng tr- ng hp nh- trn trnh
lm m rng chui d liu ngun ngha l ch m ho di lot d liu lp
li. Ng- i ta - a ra cch l thm k t tin t vo tr- c di lot,
vic gii m - c thc hin nu gp k t tin t vi di lot v gi tr
im nh theo sau.

V d:
Ta c chui ngun :
d = 5 8 4 8 8 8 8 8 8 8 8 10 10 10 10 10 10 10 10 10
Gi s k t tin t l + ta c : 5 8 4 +7 8 + 9 10
T s nn = 19 / 9 = 2.1
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 13
Tuy nhin trong mt s tr- ng hp cc im nh c t- ng quan vi
nhau v gi tr mc xm nh- trong v d d- i y ta c th tin hnh x l
nh- sau.

V d:
Ta c mt chui ngun:
d = 5 7 9 11 13 18 28 38 48 58 55 60 65 70 75 80 85 90 95 100
Ta c da vo t- ng quan ny c - c hiu qu nn cao , bng
vic p dng e(i) = d(i) d(i-1) s thu - c :
5 2 2 2 2 5 10 10 10 10 -3 5 5 5 5 5 5 5 5 5
p dng ph- ng php nn lot di ta d dng thu - c :
(5 1)( 2 4)(5 1)(10 5)(-3 1)(5 9)

II.2.2.Thut ton:

Thut ton nh- sau :
Tin hnh duyt trn tng hng cho n khi kt thc vng d liu
nh, trong qu trnh duyt tin hnh kim tra tm ra nhng lot c cng
gi tr ng thi ch nhng k hiu xung dng (hay kt thc dng) ,kt
thc nh Bitmap,
Khi gp lot c di > 3 th nhy n ch nn ng- c li nhy
n ch khng nn tuy nhin nu lot > 255 th s tch ra ch m < 255
sau m tip phn cn li. Ngoi ra cn cc ch khc nh- : bt u , kt
thc 1 dng.
Kt thc khi gp k hiu kt thc bitmap ( end o f- bitmap)



n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 14
II.2.3.Mt s th tc ch-ng trnh :
Ch-ng trnh nn theo ph-ng php RLE :
void CRLE::CompressInRLE8(BYTE*pSrcBits,CByteArray& pRLEBits, int&
RLE_size)
{
int line;
int src_index = 0, dst_index = 0, counter, i;
for ( line = 0; line < m_dib.dsBmih.biHeight; line++)
{
state_start:
if ( EndOfLine(src_index))
{
pRLEBits[dst_index++] = 1;
pRLEBits[dst_index++] = pSrcBits[src_index];
src_index++;
goto end_of_line;
}
if(pSrcBits[src_index]==pSrcBits[src_index+1]) goto tate_compress;
if ( EndOfLine(src_index+1))
{
pRLEBits[dst_index++] = 1;
pRLEBits[dst_index++] = pSrcBits[src_index++];
pRLEBits[dst_index++] = 1;
pRLEBits[dst_index++] = pSrcBits[src_index++];
goto end_of_line;
}
if (pSrcBits[src_index+1] == pSrcBits[src_index+2])
{
pRLEBits[dst_index++] = 1;
pRLEBits[dst_index++] = pSrcBits[src_index++];
goto state_compress;
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 15
}
else
goto state_no_compress;
state_compress:
for ( counter = 1; counter <= 254; counter++)
{
if ( pSrcBits[src_index+counter] != pSrcBits[src_index] )
break;
if ( EndOfLine(src_index+counter) )
{
pRLEBits[dst_index++] = counter+1;
pRLEBits[dst_index++] = pSrcBits[src_index];
src_index += counter +1;
goto end_of_line;
}
}
pRLEBits[dst_index++] = counter;
pRLEBits[dst_index++] = pSrcBits[src_index];
src_index += counter;
goto state_start;
state_no_compress:
for (counter = 2; counter <= 254; counter++)
{
if ( EndOfLine(src_index+counter) ) {
pRLEBits[dst_index++] = 0;
pRLEBits[dst_index++] = counter + 1;
for (i = counter + 1; i > 0; i--)
pRLEBits[dst_index++]=pSrcBits[src_index++];
if ( 0 != ((counter+1) % 2) ) pRLEBits[dst_index++];
goto end_of_line;
}
if(EndOfLine(src_index+counter+1) ||
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 16
pSrcBits[src_index+counter] != pSrcBits[src_index+counter+1] )
continue;
if(EndOfLine(src_index+counter+2) ||
SrcBits[src_index+counter+1] != pSrcBits[src_index+counter+2] )
continue;
else
{
if ( counter > 2) counter--;
pRLEBits[dst_index++] = 0;
pRLEBits[dst_index++] = counter+1;
for (i = counter+1; i > 0; i--)
pRLEBits[dst_index++] = pSrcBits[src_index++];
if ( 0 != ((counter+1) % 2) ) pRLEBits[dst_index++];
goto state_compress;
}
}
pRLEBits[dst_index++] = 0;
pRLEBits[dst_index++] = counter;
for (i = counter; i > 0; i--)
pRLEBits[dst_index++] = pSrcBits[src_index++];
if ( 0 != ((counter) % 2) )
pRLEBits[dst_index++];
goto state_start;
end_of_line:
if ( 0 != (src_index % 4 ))
{
int pad = 4 - (src_index%4);
src_index += pad;
}
pRLEBits[dst_index++] = 0;
pRLEBits[dst_index++] = 0;
}
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 17

pRLEBits[dst_index++] = 0;
pRLEBits[dst_index++] = 1;
RLE_size = dst_index;
}

Ch-ng trnh gii nn ph-ng php RLE :
BOOL CRLE::DecRLE8(ifstream &fil, BYTE *pDest)
{
DWORD x, y, paddedwidth;
paddedwidth =BMPWIDTHBYTES(pInfo->biWidth*pInfo-> biBitCount);
BYTE FirstByte, SecondByte;
WORD data;
x = y = 0;
while (!fil.eof())
{
if (!fil.read((char*)&data, 2)) return FALSE;
FirstByte = LOBYTE(data);
SecondByte = HIBYTE(data);
if (FirstByte == 0)
{
switch (SecondByte)
{
case 0:
x = 0;
y++;
break;
case 1:
return TRUE;
case 2:
if (!fil.read((char*)&data, 2)) return FALSE;
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 18
x += LOBYTE(data);
y += HIBYTE(data);
break;
default:
char ch;
for (BYTE i = 0; i < SecondByte; i++)
{
if (!fil.get(ch)) return FALSE;
pDest[paddedwidth * y + x++] = (BYTE)ch;
}
if ((SecondByte % 2) == 1)
if (!fil.get(ch)) return FALSE;
}
}
else
{
for (BYTE i = 0; i < FirstByte; i++)
pDest[paddedwidth * y + x++] = SecondByte;
}
}
return FALSE;
}










n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 19
II.3.Ph-ng php m ho Huffman:

II.3.1. Nguyn tc:
Ph- ng php m ho Huffman l ph- ng php da vo m hnh thng
k. Ng- i ta tnh tn sut xut hin ca cc k t bng cch duyt tun t t
u tp gc n cui tp. Vic x l y tnh theo bit. Trong ph- ng php
ny cc k t c tn sut cao mt t m ngn, cc k t c tn sut thp mt
t m di. Nh- vy vi cch thc ny ta lm gim chiu di trung bnh
ca t m ho bng cch dng chiu di bin i tuy nhin cng c tr- ng
hp b thit 1 t bit khi tn sut l rt thp.

II.3.2. Thut ton:
Thut ton m ho Huffman gm 2 b-c chnh:
B- c mt:
Tnh tn sut ca cc k t trong d liu gc bng cch duyt tp
gc mt cch tun t t u n cui xy dng bng m v tnh ton tn
sut. Tip theo sau l sp xp li bng m theo th t tn sut gim dn.
B- c hai:
Duyt bng tn sut t cui ln u thc hin ghp hai phn t
c tn sut thp thnh mt phn t duy nht c tn sut bng tng hai tn
sut thnh phn. Cp nht phn t ny vo v tr ph hp trong bng v loi
b hai phn t xt. Thc hin cho n khi bng ch c mt phn t. y
l qu trnh to cy nh phn Huffman ,phn t c tn sut thp bn phi,
phn t kia bn tri. Sau khi cy to xong ng- i ta tin hnh gn m
cho cc nt l. Vic m ho thc hin theo quy nh : mi ln xung bn
phi ta thm mt bit 1 vo t m, mi ln xung bn tri ta thm mt bit
0 vo t m.
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 20
Qu trnh gii nn cng kh n gin - c tin hnh theo chiu ng- c
li. Ng- i ta cng phi da vo bng m to ra trong giai on nn (bng
ny - c l- u tr trong cu trc u ca tp nn cng vi d liu nn).
V d: Mt tp bt k c tn sut xut hin ca cc k t s nh- bng sau.
K t Tn sut
0
1
2
3
4
5
6
7
8
9
1532
152
323
412
226
385
602
92
112
87

Bng tn sut sp xp gim dn












K t Xc sut (%)
0
6
3
5
2
4
1
8
7
9
0.3906
0.1535
0.1051
0.0982
0.0824
0.0577
0.0388
0.0286
0.0235
0.0222
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 21










M t : Ta tin hnh hp nht hay cng 2 tn sut nh nht cui
bng thu - c gi tr tn sut mi sau - a gi tr ny tr li bng tn
sut ban u b i 2 tn sut thnh phn to thnh n. Sau khi - a gi tr
mi vo bng ta phi tin hnh sp xp li ton b bng , lc ny s l- ng
tn sut ch cn l n-1 nu ban u s l- ng tn sut l n. Tip tc thc hin
ln l- t theo th t nh- trn cho n khi no s l- ng tn sut ch cn li
duy nht 1 gi tr.

Vic to cy nh phn c th - c thc hin theo mt thut ton sau:
1. Tt c nhng k t ban u - c xem nh- l nhng k t giao im
t do.
2. Hai nt t do vi tn s xut hin thp nht - c phn cng ti mt
nt gc vi gi tr bng vi tng ca hai nt con t do.
3. Hai nt con - c chuyn khi danh sch nt t do. Chuyn nt gc
mi to thnh cng vo danh sch.
4. B- c hai sang b- c ba - c lp cho n khi ch c 1 nt t do v pha
tri. Nt t do ny l gc ca cy.
Qu trnh xy dng cy nh phn Huffman - c th hin chi tit nh-
trong hnh sau :
0.3905 0.6100

0.3905

0
6
5
2
4
1
8
7
9
3
0.3905

0.1535

0.1051

0.0982

0.0824

0.0577

0.0457

0.0388

0.0286

0.3905

0.1535

0.1051

0.0982

0.0824

0.0674

0.0577

0.0457

0.3906

0.1051

0.1034
0.0982

0.0824

0.0674


0.3905

0.1535

0.1498
0.1051

0.1034

0.0982



0.3905

0.2016

0.1535

0.1498
0.1051


0.3905

0.2549

0.2016
0.1535


0.3905

0.3551
0.2549


0.0286

0.0388
0.1535

0.0235

0.0222

0.0577

0.0824

0.0982

0.1051

0.1535

n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 22




























Hnh 2.3 : Qu trnh to cy nh phn

B0
0 6 3 5 2 4 1 8 7 9
B1
0 6 3 5 2 4 1 8
B2
0 6 3 5 2 4
B3
0 6 3 5 2
B4
0 6 3 5
B5
0 6 3
B6
0 6
B7
0
B8
0
7
9
1
8
7
9
4
1 8
2
7
9
4
5
1 8
2
3
7
9
4
5
6
7
9
4
5
6
1
2
3
8
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 23
Ta c cy m Huffman t- ng ng nh- sau :













Bng t m gn cho cc k t s nh- sau:

K t M
0
6
3
5
2
4
1
8
7
9
1
001
011
0001
0100
00000
01010
01011
000010
000011


0 1
3
7
9
4
5
6
1 8
2
0
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 24
Nh- vy vi v d sau y ta c th tin hnh m ho nh- sau:
Chui ngun : 00000000006666693333 kch th- c = 20*8=160 bit
S dng m Huffman theo bng trn
kch th- c =10*1+5*3+1*6+4*3= 43 bit
V ga tr tr 0 xut hin 10 ln nh- ng ch dng 1 bit th hin, gi tr 6
xut hin 5 ln dng 3 bit th hin ,gi tr 9 dng 6 bit v gi tr 3 xut
hin 4 ln dng 3 bit th hin.
T s nn = 160 / 43 = 3.7

Trong ph- ng php m Huffman m ca k t l duy nht v khng m
no l phn bt u ca m tr- c.V vy khi c theo tng bit t u n
cui tp nn ta c th duyt cy m cho n mt l, tc l k t - c gii
m. Vic gii m chc chn phi s dng cy nh phn ging nh- trong m
ho. c, gii m - c yu cu phi s dng theo ng tiu chun nht
nh .

II.3.3.Mt s th tc ch-ng trnh :
Ch-ng trnh nn ph-ng php HUFFMAN :

bool CompressHuffman(BYTE*pSrc, int nSrcLen, BYTE *&pDes, int &nDesLen)
{
CHuffmanNode nodes[511];
for(int nCount = 0; nCount < 256; nCount++)
nodes[nCount].byAscii = nCount;
for(nCount = 0; nCount < nSrcLen; nCount++)
nodes[pSrc[nCount]].nFrequency++;
qsort(nodes, 256, sizeof(CHuffmanNode), frequencyCompare);
int nNodeCount = GetHuffmanTree(nodes);
int nNodeSize = sizeof(DWORD)+sizeof(BYTE);
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 25
nDesLen = nSrcLen+nNodeCount*nNodeSize;
pDes = (BYTE*)malloc(nDesLen);
BYTE *pDesPtr = pDes;
memset(pDesPtr, 0, nDesLen);
*(DWORD*)pDesPtr = nSrcLen;
pDesPtr += sizeof(DWORD);
*pDesPtr = nNodeCount-1;
pDesPtr += sizeof(BYTE);
for(nCount = 0; nCount < nNodeCount; nCount++)
{
memcpy(pDesPtr, &nodes[nCount], nNodeSize);
pDesPtr += nNodeSize;
}
qsort(nodes, 256, sizeof(CHuffmanNode), asciiCompare);
int nDesIndex = 0;
for(nCount = 0; nCount < nSrcLen; nCount++)
{
*(DWORD*)(pDesPtr+(nDesIndex>>3))|=nodes[pSrc[nCount]].dwCode<<
(nDesIndex&7);
nDesIndex += nodes[pSrc[nCount]].nCodeLength;
}
nDesLen = (pDesPtr-pDes)+(nDesIndex+7)/8;
pDes = (BYTE*)realloc(pDes, nDesLen);

return true;
}




n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 26
Ch-ng trnh gii nn ph-ng php HUFFMAN :
bool DecompressHuffman(BYTE*pSrc,int nSrcLen,BYTE*&pDes, int &nDesLen)
{
nDesLen = *(DWORD*)pSrc;
pDes = (BYTE*)malloc(nDesLen+1);
int nNodeCount = *(pSrc+sizeof(DWORD))+1;
CHuffmanNode nodes[511], *pNode;
int nNodeSize = sizeof(DWORD)+sizeof(BYTE), nSrcIndex = nNodeSize;
for(int nCount = 0; nCount < nNodeCount; nCount++)
{ memcpy(&nodes[nCount], pSrc+nSrcIndex, nNodeSize);
nSrcIndex += nNodeSize;
}
GetHuffmanTree(nodes, false);
CHuffmanNode *pRoot = &nodes[0];
while(pRoot->pParent)
pRoot = pRoot->pParent;
int nDesIndex = 0;
DWORD nCode;
nSrcIndex <<= 3;
while(nDesIndex < nDesLen)
{ nCode = (*(DWORD*)(pSrc+(nSrcIndex>>3)))>>(nSrcIndex&7);
pNode = pRoot;
while(pNode->pLeft)
{ pNode = (nCode&1) ? pNode->pRight : pNode->pLeft;
nCode >>= 1;
nSrcIndex++;
}
pDes[nDesIndex++] = pNode->byAscii;
}
return true;
}
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 27
II.4.Ph-ng php m ho LZW:

Khi nim nn t in - c Jocob Lempe v Abraham Ziv - a ra ln
u tin nm 1977 sau pht trin thnh mt h gii thut nn t in LZ.
Nm 1984 Welch ci tin gii thut LZ thnh gii thut mi hiu qu hn
v - c t tn l LZW ( Lempe Ziv - Welch). Ph- ng php ny xy
dng t in l- u cc chui k t c tn sut lp li cao v thay th bng t
m t- ng ng mi khi gp li chng, n hay hn cc ph- ng php tr- c
k thut t chc t in cho php nng cao t l nn.
Gii thut LZW - c dng cho tt c cc loi file nh phn, th- ng
- c dng nn cc loi d liu nh- : vn bn, nh en trng, nh mu,
nh a cp smv l chun nn cho cc dng nh GIF v TIFF. S bit /
pixel khng nh h- ng n hiu qu ca LZW.
II.4.1.Nguyn tc:

Gii thut nn LZW xy dng mt t in l- u cc mu c tn sut xut
hin cao trong nh. T in l tp hp nhng cp (t vng v ngha ca t
vng). Trong t vng s l cc t m - c sp xp theo th t nht nh.
Ngha l mt chui con trong d liu nh. T in - c xy dng song song
vi qu trnh c d liu. S xut hin ca chui con trong t in khng
nh rng chui tng xut hin trong phn d liu oc c qua.
Thut ton lin tc tra cu v sau mi ln c mt k t d liu u vo th
tin hnh cp nht li t in.

Do gii hn ca b nh v m bo tc tm kim nhanh, t in
ch gii hn 4096 phn t dng l- u tr gi tr ca cc t m. Nh- vy
di ln nht ca t m l 12 bit (4096=2
12
). Cu trc t in nh- sau:


n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 28

0 0
1 1


255 255
256 256 (Clear Code)
257 257 (End Of Information)
258 Chui
259 Chui


4095 Chui

256 t m u tin theo th t t 0 255 cha cc s nguyn t 0
255. y l m ca 256 k t c bn trong bng m ASCII.
T m th 256 cha mt m c bit l m xo (CC - Clear Code).
Khi s mu lp ln hn 4096 th ng- i ta s coi nh gm nhiu mnh nh v
t in s gm nhiu t in con. Khi ht mt mnh nh s gi 1 m xo (
CC ) bo hiu kt thc mnh nh c v bt u mnh nh mi ng thi
s khi to li t in.
T m th 257 cha m kt thc thng tin (EOI - End Of
Information). Thng th- ng mt file nh GIF c th cha nhiu mnh nh,
mi mnh nh ny s - c m ho ring. Ch- ng trnh gii m s lp i lp
li thao tc gii m tng nh cho n khi gp m kt thc thng tin th dng
li.
Cc t m cn li (t 258 n 4095) cha cc mu th- ng lp li
trong nh. 512 phn t u tin ca t in biu din bng 9 bit. Cc t m
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 29
t 512 n 1023 biu din bi 10 bit, t 1024 n 2047 biu din bi 11 bit,
v t 2048 n 4095 biu din bi 12 bit.

V d : Cho chui u vo HELLOHELLOHELL
T in ban u gm 256 k t c bn.
Kch th- c u vo : 14 x 8 = 112 bit

u vo u ra Thc hin
H(72) H c trong t in c tip
E(69) 72 Thm vo t in m 258 i din cho chui HE
L(76) 69 Thm vo t in m 259 i din cho chui EL
L 76 Thm vo t in m 260 i din cho chui LL
O(79) 76 Thm vo t in m 261 i din cho chui LO
H 79 Thm vo t in m 262 i din cho chui OH
E HE c trong t in c tip
L 258 Thm vo t in m 263 i din cho chui HEL
L LL c trong t in c tip
O 260 Thm vo t in m 264 i din cho chui LLO
H OH c trong t in c tip
E 262 Thm vo t in m 265 i din cho chui OHE
L EL c trong t in c tip
L 259 Thm vo t in m 266 i din cho chui ELL
76 Input = FALSE
EOI

Chui u ra l : 72 69 76 76 79 258 260 262 259 76
Kch th- c u ra : 6 x 8 + 4 x 9 = 84 bit
T s nn : 112 / 84 = 1.3

n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 30
Qu trnh gii nn thc hin nh- sau:

Code OutBuff()
AddToDictionary()
CodeWord String
72 H
69 E 258 HE
76 L 259 EL
76 L 260 LL
79 O 261 LO
258 HE 262 OHE
260 LL 263 HEL
262 OH 264 LLO
259 EL 265 OHE
76 L 266 ELL
EOI

Chui thu c sau gii nn :HELLOHELLOHELL








n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 31
II.4.2. Thut ton:
Thut ton nn bng LZW:
InitDictionary()
Output(Clear_Code)
OldStr = NULL
WHILE ( Input )
{
NewChar = GetNextChar()
NewStr = OldStr + NewChar
IF ( InDictionary(NewStr) ) OldsSr = NewStr
ELSE
{
Output(Code(OldStr))
AddToDictionary(NewStr)
OldStr = NewChar
}
}
Output(Code(OldStr))
Output(EOI)

Gi tr c Input = TRUE khi vn cn d liu u vo v ng- c
li.

Chc nng ca cc hm:
Hm InitDictionary() : Khi to t in. t gi tr cho 256 phn
t u tin. Gn m xo CC cho phn t th 256 v m kt thc thng
tin EOI cho phn t th 257. Xo gi tr tt c cc phn t cn li.
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 32
Hm InDictionary (NewStr) : Kim tra chui NewStr c trong
t in ch- a .
Hm OutPut() : Gi chui bit ra file.Tu thuc vo v tr ca t
m trong t in m chui bit c di l 9,10,11 hoc 12.
Hm GetNextChar() : Tr v mt k t t chui k t u vo.
Hm ny cp nht gi tr c Input xc nh xem cn d liu u vo
na hay khng.
Hm AddToDictionary() : Hm lm chc nng cp nht mu mi
vo phn t tip theo trong t in. Nu t in y n s gi ra m
xo CC v gi n hm InitDictionary() khi to li t in.
Hm Code() : Tr v t m ng vi 1 chui.

T- t- ng ca on m trn c th hiu nh- sau: Nu cn d liu u vo
th tip tc c. Mt chui mi s - c to ra t chui c (chui ny ban
u rng, chui ny phi l chui tn ti trong t in) v k t va c
vo. Sau kim tra xem chui mi c trong t in hay ch- a. Mc ch
ca cng vic ny l hi vng tm - c chui c s k t ln nht trong t
in. Nu tn ti th li tip tc c mt k t tip theo v lp li cng vic.
Nu ch- a c trong t in, th gi chui c ra ngoi v thm chui mi vo
t in.

Gii nn d liu bng LZW:

Gii thut gii nn gn nh- ng- c vi gii thut nn. Vi gii thut nn,
mt t m ng vi mt chui s - c ghi ra tp khi chui ghp bi chui
trn vi k t va c ch- a c mt trong t in. Ng- i ta cng cp nht
ngay vo t in t m ng vi chui to bi chui c vi k t va c. K
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 33
t ny ng thi l k t u tin trong chui ng vi t m s - c ghi ra
tip theo.
Thut ton - c m t nh- sau:

WHILE (GetNextCode() != EOI)
{
IF (First_Code) /*M u tin ca mi mnh nh*/
{
OutBuff(DeCode(code))
OldStr = DeCode(code)
}
ELSE
{
IF (code == CC) /*M xo*/
{
InitDictionary()
First_Code = TRUE
}
NewStr = DeCode(code)
OutBuff(NewStr)
OldStr = OldStr + FirstChar(NewStr)
AddToDictionary(OldStr)
OldStr = NewStr
}
}
Gi tr c First_Code = TRUE ch m va c l m u tin ca
mi mnh nh.

Chc nng ca cc hm:
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 34
M CC bo hiu ht mt mnh nh. M EOI bo hiu ht ton b
thng tin nh.
Hm GetNextCode() : Hm ny c thng tin u vo (d liu
nn) tr v m t- ng ng.
Hm OutBuff() : Hm ny gi chui gii m ra vng nh m.
Hm InitDictionary(): Khi to t in. t gi tr cho 256 phn t
u tin. Gn m xo CC cho phn t th 256 v m kt thc thng tin
EOI cho phn t th 257. Xo gi tr tt c cc phn t cn li.
Hm DeCode() : Hm ny tra cu t in v tr v chui k t t- ng
ng vi m.
Hm FirstChar() : Ly k t u tin ca mt chui. K t va
xc nh ni tip vo chui k t c ( gii m b- c tr- c) ta - c
chui k t c mt trong t in khi nn. Chui ny s - c thm vo
t in gii nn.
Hm AddToDictionary() : Hm lm chc nng cp nht mu mi
vo phn t tip theo trong t in. Nu t in y n s gi ra m
xo CC v gi n hm InitDictionary() khi to li t in..

II.4.3.Mt s th tc ch-ng trnh :
Ch-ng trnh nn ph-ng php LZW :
BOOL CLZW::Compress(CFile &source, CFile &destination)
{
long prefix = 0;
long result = 0;
BYTE readByte = 0;
unsigned long filetotal = 0;
CString logString;
DWORD resAdd = 256;
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 35
Init();
filetotal = source.GetLength();
if (m_tudien == NULL)
{
CreateDictionary();
}
source.Read(&prefix, 1);
while (source.GetPosition() < filetotal)
{
source.Read(&readByte, 1);
result = m_tudien->GetEntry(prefix, readByte);
if (result == -1)
{
resAdd = m_tudien->AddEntry(prefix, readByte);
CalculateBitSize(resAdd);
logString.Format("Adding combination of %d and %d to
dictionary to entry %d.",prefix, readByte, resAdd);
Log(logString);
CompressData(destination, prefix);
prefix = readByte;
result = -1;
}
else
{
prefix = result;
readByte = 0;
}
}
CompressData(destination, prefix);
CloseCompressedFile(destination);
ClearDictionary();
return TRUE;
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 36
}
Ch-ng trnh gii nn ph-ng php LZW :

BOOL CLZW::Decompress(CFile &source, CFile &destination)
{
DWORD prefix = 0, data = 0;
CString logString;
CByteArray decodeString;
BYTE writeData = 0, character = 0;
int counter = 0;
Init();

if (m_tudien == NULL)
{
CreateDictionary();
}
prefix = DecompressData(source);
character = (BYTE)prefix;
destination.Write(&prefix, 1);

while ((data = DecompressData(source)) != m_MaxCode[m_MaxBits])
{
if (!m_tudien->IsCodeExist(data))
{
decodeString.Add((BYTE)character);
m_tudien->GetBytesFromCode(&decodeString, prefix);
}

else
{
m_tudien->GetBytesFromCode(&decodeString, data);
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 37
character = decodeString.GetAt(decodeString.GetSize() - 1);
}
counter = decodeString.GetSize();


while (counter > 0)
{
writeData = (BYTE)decodeString.GetAt(--counter);
destination.Write(&writeData, 1);
logString.Format("Adding character code %d with know visualisation of:
%s", writeData, convertASCIIToText(writeData));
Log(logString);
}
decodeString.RemoveAll();

m_tudien->AddEntry(prefix, (BYTE)character);
CalculateBitSize(m_tudien->GetMaxCode()+1);
prefix = data;
}
return TRUE;
}










n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 38
P
h

n
k
h

i
nh
gc
8x8
8x8
8x8
DCT
L- ng t
ho
M
ho
nh
nn
Bng
l- ng
t
Bng
m
Khi
8x8
8 x 8

II.5.Ph-ng php m ho JPEG:

II.5.1.Nguyn tc:
JPEG l vit tt ca Joint Photographic Expert Group ( nhm cc
chuyn gia ho pht trin chun ny ). Chun ny - c pht trin t gia
thp nin 80 bi s hp tc ca t chc ISO v ITU v n 1992 - c cng
nhn l chun nh quc t ,phc v cc ng dng v nh cho cc lnh vc
nh- mng, y hc, khoa hc k thut, nh ngh thut
Chun JPEG - c s dng m ho nh a mc xm, nh mu. JPEG
khng cho kt qu n nh lm vi nh en trng, n cung cp c 2 ch
nn : nn mt mt thng tin v nn khng tn tht. Do phc tp v hiu
sut nn ca JPEG khng mt mt thng tin m n khng - c s dng ph
bin .D- i y ch trnh by chi tit v mt trong cc dng nn bin i chp
nhn mt mt thng tin dng bin i Cosin tun t ( Sequential DTC-
Based) ca chun JPEG.
II.5.2.Thut ton:
M ho JPEG bao gm nhiu cng on, s thut ton nn v gii
nn - c m t nh- d- i y








Hnh 2.5.1 : Qu trnh nn nh theo chun JPEG

n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 39
nh
nn
nh
gii
nn
T- ng
t ho
Gii
m
DCT
ng- c
Bng
l- ng
t
Bng
m
Qu trnh gii nn s - c thc hin ng- c li, ng- i ta da vo cc
thng tin lin quan ghi trong phn Header ca file nn gii m tng phn
nh nn ng vi ph- ng php nn. Kt qu thu - c l h s l- ng t.
Cn c vo bng l- ng t s khi phc li gi tr tr- c khi l- ng t ho ca
cc h s ny. Cui cng l bin i Cosin ng- c thu li - c nh nh-
ban u .







Hnh 2.5.2 : Qu trnh gii nn nh JPEG

A. Chia nh thnh khi

Chun nn JPEG phn nh ra cc khi 8x8. Bin i nhanh Cosin 2
chiu cho cc khi 8x8 s t hiu qu hn, vic bin i Cosin cho cc khi
c cng kch c c th gim - c mt phn cc tnh ton chung nh- vic
tnh h s C
i
j
. Khi n = 8 chng ta ch cn tnh h s C
i
j
cho 3 tng (8 = 2
3
),
s cc h s l: 4 + 2 + 1 = 7

Nu vi mt nh 512x512, php bin i nhanh Cosin mt chiu theo
hng ngang hoc hng dc ta phi cn qua 9 tng (512 = 2
9
). S cc h s C
i
j
l: 256 + 128 + 64 + 32 + 16 + 8 + 4 + 2 +1 =509. Nh- vy thi gian tnh
cc h s C
i
j
cho nh 512x512 ln gp 72 ln so vi thi gian tnh ton cc
h s ny cho tng khi 8x8 , nu kch th- c nh ln hn th chi ph thi
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 40
gian s cn tng gp nhiu ln ng thi cn gip vic tnh ton trong khi
bin i Cosin chnh xc hn.

nh s chia lm B khi vi:
Cc khi - c xc nh bi b s (m,n) vi m=[0..M
B
-1] v n=[0..N
B
-1].
Trong :
m : th t ca khi theo chiu rng.
n : th t ca khi theo chiu di.
Phn khi thc cht l xc nh t- ng quan gia to ring trong khi
vi to thc ca im nh trong nh ban u. Nu nh ban u k hiu
Image[i,j] th ma trn biu din khi (m,n) l x[u,v] - c tnh:
x[u,v] = Image[mk + u, nl + v]

B. Bin i
y l cng on quan trng ca cc ph- ng php nn s dng php bin
i, nhim v ca cng on ny l tp trung nng l- ng vo mt s t cc
h s bin i.
Cng thc bin i cho mi khi l:

Trong :
B B
N M
l
N
k
M
B =
|
.
|

\
|

|
.
|

\
|
=
' '
( )
16
) 1 2 (
16
) 1 2 (
) , (
4
,
2 2
7
0 1
7
0 2
1 1
2 1
2 1
2 1
H + H +
=

= =
k n
Cos
k n
Cos n n x k k X
n n
k k
c c
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 41

Thut ton bin i nhanh Cosin hai chiu cho mi khi s bao gm 16
php bin i nhanh Cosin mt chiu. Tin hnh bin i nhanh Cosin mt
chiu cho cc dy im nh trn 8 hng. Tip tin hnh bin i nhanh
Cosin mt chiu trn 8 ct ca ma trn va thu - c sau 8 php bin i
trn.Kt qu s l ma trn h s bin i ca khi t- ng ng.
Gii thut bin i nhanh - c m t nh- hnh sau:
Hnh 2.5.3: M t gii thut bin i nhanh


-1
-1
0

0
0
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
X(0)
X(1)
X(2)
X(3)
X(4)
X(5)
X(6)
X(7)
X(0)
X(1)
X(2)
X(3)
X(4)
X(5)
X(6)
X(7)
3
16
2C
2
16
2C
1
16
2C
0
16
2C
1
8
2C
0
8
2C
1
8
2C
0
8
2C
4
C
4
C
4
C
4
C

o

b

=
0
2
1
1 k
c
Khi k1 = 0
Khi (0<k1<8)

=
0
2
1
2 k
c
Khi k2 = 0
Khi (0<k2<8)
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 42

Trong s gii nn ta phi dng php bin i Cosin ng- c. Cng
thc bin i ng- c cho khi 8x8:
Trong :
Cng thc tnh x(k
1
,n
2
) l php bin i Cosin ng- c ri rc mt chiu
ca X(k
1
,k
2
). Nh- vy mun khi phc li nh ban u t h s bin i
chng ta s bin i nhanh Cosin ng- c ri rc mt chiu cc h s theo
hng, sau em bin i nhanh Cosin ri rc mt chiu theo ct cc kt
qu trung gian va tnh - c.
S bin i Cosin ng- c nhanh - c biu din nh- sau:

=
0
2
1
1 k
c
Khi k1 = 0
Khi (0<k1<8)

=
0
2
1
2 k
c
Khi k2 = 0
Khi (0<k2<8)
( )
16
) 1 2 (
16
) 1 2 (
) , (
4
,
2 2
7
0 1
7
0 2
1 1
2 1
2 1
2 1
H + H +
=

= =
k n
Cos
k n
Cos k k X n n x
k k
c c
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 43
Hnh 2.5.4 : S bin i Cosin ng- c
C. L-ng t ho

Khi l- ng t ho trong s nn ng vai tr quan trng v quyt nh
t l nn ca chun nn JPEG. u vo ca khi l- ng t ho l cc ma trn
h s bin i Cosin ca cc khi im nh. Nh- ta bit nh - c chia
lm nhiu khi, ng mi khi l cc h s xc nh. Tr- c khi thc hin
b- c l- ng t ho ta c th loi b vi h s. Ngoi h s (0,0) (- c coi l
thnh phn DC) biu din mc sng trung bnh ca mt khi ta c th loi
mt s h s khc trong khi mang thng tin chi tit v nh. Ta c th bt
u loi b t h s c lch chun thp nht. Vic nn gi li bao nhiu
h s cn tu thuc vo vic ta mun t l nn cao hay thp v nhng yu
cu v cht l- ng ca nh nn .

0.5
0.5
0.5
0.5
0.5
0.5
0.5
0.5
-1
-1
-1
-1
-1
-1
-1
-1
0.5
-1
-1
-1
-1
X(0)
X(1)
X(2)
X(3)
X(4)
X(5)
X(6)
X(7)
X(0)
X(1)
X(2)
X(3)
X(4)
X(5)
X(6)
X(7)

o

b

t
0.5
0.5
0.5
0
4
2
1
C
0
4
2
1
C
0
4
2
1
C
0
4
2
1
C
0
8
4
1
C
1
8
4
1
C
0
8
4
1
C
1
8
4
1
C
0
16
1
C
0
16
1
C
0
16
1
C
0
16
1
C
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 44
gim s b l- ng t,ng- i ta tm cch quy cc h s cc khi v
cng mt khong phn b. Chun nn JPEG ch s dng mt b l- ng t
ho. Gi s rng cc h s u c hm tnh xc sut xut hin nh- nhau. Ta
s cn chnh li h s y
j
bng php gn:
Vi:
j
l trung bnh cng ca h s th j
o
j
l lch c bn ca h s th j

Nh- vy chng ta s ng nht - c mc quyt nh v mc to li cho
tt c cc h s. Do , cc h s s - c biu din bng cng mt s l- ng
bit. C nhiu cch tip cn tnh - c cc mc quyt v mc to li. Lloyd
Max - a ra gii thut sau:

B- c 1: Chn gi tr khi to:
d
0
= y
L

d
n
= y
H

r
0
= d
0

N l s mc l- ng t
B- c 2: Cho i bin thin t 1 n N-1 thc hin cc cng vic sau:
Tnh r
i-1
theo cng thc:
Tnh r
i
theo cng thc:
r
i
= 2d
i
r
i-1

j
j j
j
y
y
o

=
}
}

i
i
i
i
d
d
d
d
i
dy y p
dy y p y
r
1
1
) (
) ( .
1
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 45
Tnh d
i
theo cng thc:
B- c 3: Tnh:
B- c 4: Nu r
N-1
= r iu chnh li r
0
v lp li t b- c 2 n b- c 4
Trong qu trnh ci t th tc to ra b l- ng t ho, Lloyd v Max
c nhiu ci tin tnh ton d dng hn.

Sau y l cc b- c m t ton b cng vic ca khi l- ng t ho tc
ng ln cc h s bin i Cosin:

B- c 1: Tnh trung bnh cng v lch c bn o cho tng h s mi v
tr trong khi:
Vi : y
j
l h s th j, n l s khi

B- c 2: La chn t l h s gi li trong mi khi.

B- c 3: Gi li cc h s c lch c bn ln hn.
2
1
+
=
i i
i
r r
d
}
}

= '
N
N
N
N
d
d
d
d
dy y p
dy y p y
r
1
1
) (
) ( .
n
y
j
j

=
) 1 (
) (
2 2

=

n n
y y n
j j
j
o
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 46

B- c 4: Lp ma trn khu vc T
T
n
= 1 nu h s (i,j) - c gi li
T
n
= 0 ng- c li
B- c 5: Cn chnh li gi tr ca cc h s xoay chiu - c gi li cc
khi:
B- c 6: Tnh phn b ca cc gi tr xoay chiu cn chnh.

B- c 7: Tnh lch c bn o
s
ca cc phn b va tnh.

B- c 8: L- ng t ho cc h s xoay chiu bng cch s dng b l- ng t
Lloyd Max sau khi iu chnh mc quyt nh v mc to li ca n
theo cch sau:
d
i
: d
i
x o
s

r
i
: ri x o
s

d
N
= - d
0

Thnh phn mt chiu s khng l- ng t ho (phn t (0,0) ca khi 8x8
- c coi l thnh phn mt chiu, 63 phn t cn li - c coi l cc thnh
phn xoay chiu). n y ta chuyn sang b- c nn.






ij
ij ij
ij
C
C
o

=
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 47
D. Nn

u vo ca khi nn gm hai thnh phn: thnh phn cc h s xoay
chiu v thnh phn cc h s mt chiu .
Thnh phn cc h s mt chiu C
i
(0,0) vi i = 0,1,, 63 cha phn
ln nng l- ng tn hiu hnh nh. Ng- i ta khng nn trc tip cc gi tr
C
i
(0,0) m xc nh lch ca C
i
(0,0):
d
i
= C
i+1
(0,0) - C
i
(0,0)
d
i
c gi tr nh hn hiu so vi C
i
nn trong biu din du phy ng c
nhu chui bt 0 cho hiu sut nn cao hn. Gi tr C
0
(0,0) v cc lch d
i

- c ghi ra mt tp tm thi. Tp ny - c nn bng ph- ng php nn
Huffman.
Thnh phn cc h s xoay chiu C
i
(m,n) vi 1=m<=7, 1<= n<=7 cha
cc thng tin chi tit ca nh. nng cao hiu qu nn cho mi b h s
trong mt khi ng- i ta xp chng li theo th t Zig-Zag. Vic sp xp ny
gip to ra nhiu lot h s ging nhau do nng l- ng ca khi h s gim
dn t gc trn bn tri xung gc d- i bn phi.D- i y l mt minh ho
v khi Zig-Zag :

0 2 3 9 10 20 21 35
1 4 8 11 19 22 34 36
5 7 12 18 23 33 37 48
6 13 17 24 32 38 47 49
14 16 25 31 39 46 50 57
15 26 30 40 45 51 56 58
27 29 41 44 52 55 59 62
28 42 43 53 54 60 61 63

Hnh 2.5.5 : Minh ho khi Zig-Zag
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 48
Mi khi Zig-Zag ny - c m ho theo ph- ng php RLE. Cui mi
khi u ra ca RLE, ta t du kt thc khi EOB (End Of Block). Sau
cc khi - c dn li v li m ho mt ln bng ph- ng php m Huffman.
Nh c du kt thc khi nn c th phn bit hai khi cnh nhau khi gii
m Huffman. Hai bng m Huffman cho hai thnh phn h s tt nhin s
khc nhau.

c th gii nn - c, chng ta phi ghi li cc thng tin nh- : kch
th- c nh, kch th- c khi, ma trn T, lch tiu chun, cc mc to li,
hai bng m Huffman, kch th- c khi nn mt chiu, kch th- c khi nn
xoay chiu v ghi ni tip vo hai tp nn ca hai thnh phn h s.

Ci t gii thut cho nn JPEG thc s phc tp. chng ta phi lm
- c cc kin thc v nn RLE, Huffman, bin i Cosin, xy dng b
l- ng t ho Lloyd Max Tuy nn v gii nn JPEG hi chm nh- ng b
li cho kch th- c tp nn nh.

II.5.3.Mt s th tc ch-ng trnh :
Ch-ng trnh nn ph-ng php JPEG:

bool CJPEG::write_JPEG_file(const char * filename, int quality)
// JPEG compression
{
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
FILE * outfile;
unsigned char ** buffer;
int row_stride;
outfile = fopen(filename, "wb");
if (outfile == NULL) return false;
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 49
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
jpeg_stdio_dest(&cinfo, outfile);
cinfo.image_width = m_width;
cinfo.image_height = m_height;
cinfo.input_components = m_mode;
switch (m_mode)
{
case MODE_RGB:
cinfo.in_color_space = JCS_RGB;
break;
case MODE_GRAYSCALE:
cinfo.in_color_space = JCS_GRAYSCALE;
break;
}
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, quality, TRUE);
jpeg_start_compress(&cinfo, TRUE);
row_stride = m_width * cinfo.input_components;

buffer = new unsigned char * [cinfo.image_height];
buffer[0] = new unsigned char[cinfo.image_height * row_stride];
for (int k = 0; k < (int)(cinfo.image_height); k++) {
buffer[k] = buffer[0] + row_stride*k;
}

int i, j;
switch (m_mode)
{
case MODE_RGB:
for (j = 0; j < m_height; j++)
{
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 50
for (i = 0; i < m_width*3; i += 3)
{
buffer[j][i] = image[j][i/3].red;
buffer[j][i+1] = image[j][i/3].green;
buffer[j][i+2] = image[j][i/3].blue;
}
}
break;
case MODE_GRAYSCALE:
for (j = 0; j < m_height; j++)
{
for (i = 0; i < m_width; i++)
{
buffer[j][i] = gImage[j][i];
}
}
break;
}

while (cinfo.next_scanline < cinfo.image_height)
{
(void) jpeg_write_scanlines(&cinfo, &buffer[cinfo.next_scanline], 1);
}

jpeg_finish_compress(&cinfo);
fclose(outfile);
delete [] buffer[0];
delete [] buffer;
jpeg_destroy_compress(&cinfo);
return true;
}
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 51
Ch-ng trnh gii nn ph-ng php JPEG:

int CJPEG::write_BMP_file(const char * outfile)
// Writes a bitmap (.bmp) file (JPEG decompression)
{
FILE * fp;
size_t result;
int rowEndBytes = (4-(m_width*3)%4)%4;
BITMAPFILEHEADER bmFileHdr;
bmFileHdr.bfType = 0x4d42;
bmFileHdr.bfSize = 58 + (m_width*3 + rowEndBytes) * m_height;
bmFileHdr.bfReserved1 = 0;
bmFileHdr.bfReserved2 = 0;
bmFileHdr.bfOffBits = 58;
BITMAPINFOHEADER bmInfo;
bmInfo.biSize = 40;
bmInfo.biWidth = m_width;
bmInfo.biHeight = m_height;
bmInfo.biPlanes = 1;
bmInfo.biBitCount = 24;
bmInfo.biCompression = BI_RGB;
bmInfo.biSizeImage = (m_width*3 + rowEndBytes) * m_height;
bmInfo.biXPelsPerMeter = 0;
bmInfo.biYPelsPerMeter = 0;
bmInfo.biClrUsed = 0;
bmInfo.biClrImportant = 0;
fp = fopen(outfile, "wb");
if (fp == NULL) return WRITE_BMP_FILENOTFOUND_ERR;
result = fwrite((char *)&bmFileHdr, sizeof(BITMAPFILEHEADER), 1, fp);
if (result == 0)
{
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 52
fclose(fp);
return WRITE_BMP_WRITE_ERR;
}
result = fwrite((char *)&bmInfo, sizeof(BITMAPINFO), 1, fp);
if (result == 0)
{
fclose(fp);
return WRITE_BMP_WRITE_ERR;
}


RGBpixel pixel;
BYTE pJunk = 0;
for (int j = m_height - 1; j >= 0; j--)
{
for (int i = 0; i < m_width; i++)
{
this->getPixel(j, i, pixel);
result = fwrite((BYTE *)&pixel.blue, sizeof(BYTE), 1, fp);
if (result == 0)
{
fclose(fp);
return WRITE_BMP_WRITE_ERR;
}
result = fwrite((BYTE *)&pixel.green, sizeof(BYTE), 1, fp);
if (result == 0)
{
fclose(fp);
return WRITE_BMP_WRITE_ERR;
}
result = fwrite((BYTE *)&pixel.red, sizeof(BYTE), 1, fp);
if (result == 0)
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 53
{
fclose(fp);
return WRITE_BMP_WRITE_ERR;
}
}
if (rowEndBytes != 0)
result = fwrite((BYTE *)&pJunk, sizeof(BYTE), rowEndBytes, fp);
if (result == 0)
{
fclose(fp);
return WRITE_BMP_WRITE_ERR;
}
}


fclose(fp);
return WRITE_BMP_SUCCESS;
}













n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 54
II.6.Ph-ng php m ho JPEG2000:

II.6.1. Lch s ra i v pht trin chun J PEG2000:

Nh chng ta bit, s ra i ca JPEG mang li nhiu li ch to ln
v nhiu mt. JPEG c th gim nh kch thc nh, gim thi gian truyn
v lm gim chi ph x l nh trong khi cht lng nh l kh tt. Tuy nhin
cho n nay ngi ta mi ch ng dng dng thc nn c tn tht thng tin
ca JPEG v m ho khng tn tht ca JPEG l kh phc tp. vic nn
nh c hiu qu hn, y ban JPEG a ra mt chun nn nh mi l
JPEG2000. JPEG2000 s dng bin i Wavelet v cc phng php m
ho c bit c c nh nn u vit hn hn JPEG. JPEG2000 hin vn
ang tip tc c pht trin, nhng phn I c t chc ISO chp nhn
l chun nn nh quc t p dng cho nh tnh. Chun nn nh JPEG2000
m xng sng l bin i Wavelet vi tnh nng vt tri so vi JPEG
chc chn s c s dng trong cc server ni dung chuyn i nh
dng nh trong mng di ng.

II.6.2.Cc tnh nng ca J PEG2000:

JPEG2000 c nhiu chc nng c bit hn mi chun nn nh tnh
khc nh JPEG hay GIF. Di y l cc chc nng u vit ca JPEG2000
so vi cc chun nn nh tnh khc
- Cho cht lng nh tt nht khi p dng nn nh tnh c tn tht.
- S dng c vi truyn dn v hin th lu tin v cht lng,
phn gii, cc thnh phn mu v c tnh nh v khng gian.
- S dng cng mt c ch nn nh cho c hai dng thc nn.
- Truy nhp v gii nn ti mi thi im trong khi nhn d liu.
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 55
- Gii nn tng vng trong nh m khng cn gii nn ton b nh.
- C kh nng m ho nh vi t l nn theo tng vng khc nhau.
- Nn mt ln nhng c th gii nn vi nhiu cp cht lng tu theo
yu cu ca ngi s dng.
Hin ti, ISO v u ban JPEG a ra khuyn ngh thay th JPEG
bng JPEG2000.

II.6.3.Cc bc thc hin nn nh theo chun JPEG2000:








Hnh 2.6.1:Cc b- c nn v gii nn JPEG2000
II.6.3.1.X l trc bin i:

Do s dng bin i Wavelet, JPEG2000 cn c d liu nh u vo
dng i xng qua 0. X l trc bin i chnh l giai on m bo d
liu a vo nn nh c dng trn. pha gii m, giai on x l sau bin
i s tr li gi tr gc ban u cho d liu nh.

II.6.3.2. Bin i lin thnh phn :

Giai on ny s loi b tnh tng quan gia cc thnh phn ca nh.
JPEG2000 s dng hai loi bin i lin thnh phn l bin i mu thun
nghch (Reversible Color Transform - RCT) v bin i mu khng thun
X l
tr- c
bin i
Bin i
thun lin
thnh phn
Bin i
thun ring
thnh phn


L- ng t ho

M ho

Gii m ho
Gii l- ng
t ho
Bin i
ng- c ring
thnh phn

Bin i
ng- c lin
thnh phn
X l sau
bin i
nh gc
nh
khi phc
nh sau
m ho
nh
m ho
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 56
nghch (Irreversible Color Transform - ICT) trong bin i thun nghch
lm vic vi cc gi tr nguyn, cn bin i khng thun nghch lm vic
vi cc gi tr thc. ICT v RCT chuyn d liu nh t khng gian mu RGB
sang YCrCb. RCT c p dng trong c hai dng thc nn c tn tht v
khng tn tht, cn ICT ch p dng cho nn c tn tht. Vic p dng cc
bin i ny trc khi nn nh khng nm ngoi mc ch lm tng hiu
qu nn. Cc thnh phn Cr, Cb c nh hng rt t ti s cm nhn hnh
nh ca mt trong khi thnh phn chi Y c nh hng rt ln ti nh.
Chng ta c th thy r hn iu ny trn hnh v sau:

Hnh 2.6.2

II.6.3.3. Bin i ring thnh phn (bin i Wavelet) :

Bin i ring thnh phn c p dng trong JPEG2000 chnh l
bin i Wavelet. m bo tnh ton vn thng tin cng phi p dng cc
php bin i thun nghch hoc khng thun nghch. Do php bin i
Wavelet khng phi l mt php bin i trc giao nh bin i DCT m l
mt php bin i bng con nn cc thnh phn s c phn chia thnh cc
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 57
bng tn s khc nhau v mi bng s c m ha ring r. JPEG2000 p
dng bin i Wavelet nguyn thun nghch 5/3 (IWT) v bin i thc
khng thun nghch Daubechies 9/7. Vic tnh ton bin i trong
JPEG2000 ny s c thc hin theo phng php Lifting. S ca
phng php Lifting 1D p dng trong JPEG2000 trn hnh 2.6.3.Vic tnh
ton bin i Wavelet 2D suy ra t bin i Wavelet 1D theo cc phng
php phn gii nh tu chn. Trong JPEG2000 c 3 phng php phn gii
nh nhng phng php c s dng nhiu nht chnh l phng php kim
t thp.

Hnh 2.6.3
Do bin i Wavelet 5/3 l bin i thun nghch nn c th p dng
cho nn nh theo c hai phng php, c tn tht v khng tn tht trong
khi bin i 9/7 ch p dng cho nn nh theo phng php c tn tht
thng tin.

II.6.3.4. Lng t ho - Gii lng t ho :

Cc h s ca php bin i s c tin hnh lng t ho. Qu
trnh lng t ho cho php t t l nn cao hn bng cch th hin cc gi
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 58
tr bin i vi chnh xc tng ng cn thit vi mc chi tit ca nh
cn nn. Cc h s bin i s c lng t ho theo php lng t ho v
hng. Cc hm lng t ho khc nhau s c p dng cho cc bng con
khc nhau v c thc theo biu thc:
V(x,y) = ) , ( sgn
| ) , ( |
y x U
y x U
(

A


vi l bc lng t, U(x,y) l gi tr bng con u vo; V(x,y) l gi tr
sau lng t ho. Trong dng bin i nguyn, t bc lng t bng
1.Vi dng bin i thc th bc lng t s c chn tng ng cho
tng bng con ring r. Bc lng t ca mi bng do phi c trong
dng bt truyn i pha thu c th gii lng t cho nh. Cng thc gii
lng t ho l:
U(x,y) = | |A + ) , ( sgn ) , ( y x V r y x V

r l mt tham s xc nh du v lm trn, cc gi tr ( U x,y);V(x,y) tng
ng l cc gi tr khi phc v gi tr lng t ho nhn c. JPEG2000
khng cho trc r tuy nhin thng chn r =1/ 2 .

II.6.3.5. M ho v kt hp dng d liu sau m ho:

JPEG2000 theo khuyn ngh ca u ban JPEG quc t c th s dng
nhiu phng php m ho khc nhau cng nh nhiu cch bin i
Wavelet khc nhau c th thu c cht lng nh tng ng vi ng
dng cn x l. iu ny gip cho JPEG2000 mm do hn nhiu so vi
JPEG. Vic p dng cc phng php m ho khc nhau cng c m
rng sang lnh vc nn nh ng bng bin i Wavelet. Trong thc t cc
phng php m ho nh c p dng khi nn nh bng bin i Wavelet
cng nh JPEG2000 th c hai phng php c coi l c s v c p
dng nhiu nht: phng php SPIHT v phng php EZW. Hin nay
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 59
JPEG2000 vn c p dng m ho bng hai phng php ny v mt
phng php pht trin t hai phng php ny l phng php m ho mt
phng bt. V th y chng ta s xem xt hai phng php ny. Vic kt
hp dng d liu sau m ho ca JPEG2000 thc cht l thc hin cc
tnh nng c bit ca JPEG2000 nh tnh nng ROI v.v...

II.6.3.6. Phng php m ho SPI HT:

C th thy rng d p dng bin i Wavelet no hay cng vi n l
mt php phn gii nh no th trong cc bng con c s th t thp cng l
nhng thnh phn tn s cao (mang thng tin chi tit ca nh) trong khi
nhng bng con c s th t cao hn th s cha nhng thnh phn tn s
thp (mang thng tin chnh v nh). iu ngha l cc h s chi tit s
gim dn t bng con mc thp (HH1 chng hn) (ng vi thnh phn tn
s cao) xung bng con mc cao (ng vi thnh phn tn s thp) v c tnh
tng t v khng gian gia cc bng con, v d nh mt ng bin ca
hnh v trong nh s tn ti cng mt v tr trn cc bng con (tng
ng vi mc phn gii ca bng con y). iu ny dn ti s ra i
ca phng php SPIHT (Set partitioning in hierarchical trees - phng
php m ho phn cp theo phn vng). Phng php SPIHT c thit k
ti u cho truyn dn lu tin. iu ny c ngha l ti mi thi im trong
qu trnh gii nn nh theo phng php m ho ny th cht lng nh hin
th ti thi im y l tt nht c th t c vi mt s lng bt a vo
gii m tnh cho ti thi im y. Ngoi ra, phng php ny s dng k
thut embedded coding; iu c ngha l mt nh sau nn vi kch c
(lu tr) ln (t l nn thp) s cha chnh d liu sau nn ca nh c kch
c (lu tr) nh (t l nn cao). B m ho ch cn nn mt ln nhng c
th gii nn ra nhiu mc cht lng khc nhau. Gi s gi cc pixel trong
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 60
mt nh p cn m ho l p
i
,
j
. p dng mt php bin i Wavelet T no
cho cc pixel trong nh to ra cc h s ca php bin i Wavelet l c
i
,
j
.
Cc h s ny to ra mt nh bin i l C. Php bin i ny c vit
di dng ton t nh sau: C=T(p). Trong phng php truyn dn lu tin
vi nh th b m ho s bt u qu trnh khi phc (gii nn) nh bng
cch t cc gi tr ca nh khi phc t cc h s bin i l c . S dng
cc gi tr gii m ca cc h s bin i to ra mt nh khi phc (vn
cha p dng bin i ngc Wavelet) l c v sau p dng bin i
ngc Wavelet to ra nh cui cng l
p . Chng ta c th vit di dng
ton t nh sau:
p =T
1
(c ). Nguyn tc quan trng ca phng php
truyn dn nh theo kiu lu tin chnh l phng php ny lun truyn i
cc gi tr mang thng tin quan trng hn ca nh i trc. S d lm nh
vy l do cc thng tin chnh l cc thng tin s lm gim thiu nhiu
nht mo dng ca nh (s sai khc gia nh gc v nh khi phc). y
chnh l l do ti sao phng php SPIHT lun truyn i cc h s ln trc
v cng l mt nguyn tc quan trng ca phng php ny. Mt nguyn tc
na l cc bt c trng s ln bao gi cng mang thng tin quan trng nht
trong d liu nh phn. Phng php SPIHT s dng c hai nguyn tc ny;
n sp xp cc h s bin i v truyn i cc bt c trng s ln nht. Qu
trnh gii m c th dng li bt k mt bc no ng vi gi tr nh cn
m ho yu cu. y chnh l cch m phng php m ho SPIHT lm tn
tht thng tin.

II.6.3.7. Phng php m ho EZW:

Phng php m ho EZW (Embedded Zerotree Wavelet Encoder)
cng da trn c s php m ho lu tin (progressive coding) ging nh
phng php m ho SPIHT. Phng php ny ch yu da trn khi nim
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 61
v cy zero (zerotree). V c bn, thut ton ny da trn hai nguyn tc
nh trnh by phn phng php m ho SPIHT. Sau y chng ta s
xem xt cc khi nim c bn ca thut ton: Cy t phn: Sau khi p dng
bin i Wavelet ng vi cc mc phn gii khc nhau chng ta c th biu
din cc h s bin i di dng mt cy. Ta thy rng vi cy biu din
ny c mi nt cha th c 4 nt con. S d c c iu ny l do qu trnh
bin i Wavelet cc t l khc nhau. Ta gi y l cc cy t phn
(quadtree).

Hnh 2.6.4: Cy t phn
Cy zero (zerotree): Cy zero l mt cy t phn, trong tt c cc nt
ca n u nh hn nt gc. Mt cy nh vy khi m ho s c m ho
bng mt i tng duy nht v khi gii m th chng ta cho tt c cc gi
tr bng khng. Ngoi ra c th m ho c cc h s Wavelet trong
trng hp ny, gi tr ca nt gc phi nh hn gi tr ngng ang c
xem xt ng vi h s Wavelet Sau khi c cc khi nim cn thit v
cy t phn v cy zero, chng ta c th trnh by nguyn l hot ng ca
thut ton. Thut ton s m ho cc h s theo th t gim dn. Chng ta
s dng mt gi tr gi l ngng (threshold) v s dng ngng ny tin
hnh m ho cc h s bin i. Cc h s c m ho theo th t t vng
tn s thp n vng tn s cao. V ch nhng h s c gi tr tuyt i ln
hn hoc bng ngng th mi c m ho. Tip theo gim ngng v tip
tc lm nh vy cho ti khi ngng t ti mt gi tr nh hn gi tr ca h
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 62
s nh nht. Cch gim gi tr ngng y thc hin tng i c bit,
gi tr ca ngng gim xung mt na so vi trc . B gii m phi
bit cc mc ngng ny th mi c th gii m nh thnh cng. Nhng khi
ta i t nt cha n nt con trong cy t phn th n vn c 3 nt con. Vy
ta phi i theo nhnh c nt con no trc. Hay ni mt cch y hn ta
di chuyn t h s ny n h s khc theo th t nh th no. C nhiu
cch di chuyn khc nhau, tuy nhin hai cch di chuyn trn hnh trn c
s dng nhiu nht.

Hnh 2.6.5
Vic sp xp ny cn phi c quy c thng nht gia qu trnh m
ho v qu trnh gii m vic gii m nh c thnh cng. Trn y ch
l nguyn l c bn ca phng php m ho EZW. Hin nay phng php
m ho ny c p dng ngy cng nhiu nn nh ng. Phng php ny
cho t l nn v tin cy gii m cao. Ngoi ra phng php EZW rt d
trin khai trn my tnh bi phng php ny khng yu cu vic lp trnh
qu phc tp.

II.6.4.So snh chun J PEG2000 vi J PEG v cc chun nn nh
tnh khc:

Mt tnh nng quan trng v l u im r nt nht ca JPEG2000so vi
JPEG cng nh cc chun nn nh khc nh MPEG 4 VTC hay JPEG - LS
v. v.... l JPEG2000 a ra c hai k thut nn c tn tht v khng tn tht
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 63
theo cng mt c ch m ho ngha l JPEG2000 thc hin tt c cc dng
thc ca JPEG ch bng mt c ch m ho duy nht. Nu xt v s tn ti
ca hai k thut ny th JPEG cng c kh nng nn nh c tn tht v
khng tn tht thng tin. Tuy nhin vi JPEG th c ch m ho vi hai
dng ny l khc nhau v rt kh s dng c hai dng ny cng lc cho
cng mt ng dng. Do , c th thy rng JPEG c tnh mm do hn bt
k chun nn nh tnh no trc y. Hn th, chng ta thy rng tt c
cc phng php thit k cho chun JPEG2000 u u vit v c nhiu tnh
nng hn so vi JPEG; ngoi ra nhng thng k v thc t cho thy vi
cng mt t l nn v mt loi nh th nh c nn bi JPEG2000 hu nh
lun c cht lng tt hn so vi JPEG. Chng ta xem xt hai nh trn hnh
2.6.6 thy r iu ny, nh bn tri c nn theo JPEG cn nh bn
phi c nn theo JPEG2000.


JPEG JPEG 2000

JPEG JPEG 2000
Hnh 2.6.6 (a ,b)
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 64
Tnh nng u vit th hai ca JPEG2000 so vi JPEG chnh l trong
dng thc nn c tn tht thng tin, JPEG2000 c th a ra t l nn cao
hn nhiu so vi JPEG. Cc phn mm nn nh JPEG hin ti (k c
Photoshop) cng ch thit k c th nn c ti t l 40:1nhng vi
JPEG2000 th t l nn c th ln ti 200:1. Theo cng thc tnh PSNR
trong n v dB, chng ta c: (b l s bt dng biu din mt pixel trn nh
gc)
PSNR(dB) =
|
|
.
|

\
|

1 2
10
log 20
b
RMSE

Trong MSE( Mean square error ) l sai s bnh phng trung
bnh , PSNR (peak to signal to noise ratio) l t s tn hiu trn nhiu
nh.MSE thng c gi l phng sai lng t -
2
q
o (quantization error
variance) .MSE gia nh gc v nh khi phc c tnh nh sau :
| | | | ( )

= =
k j
q
k i g k i f
N
MSE
,
2 2
, ,
1
o
Trong tng ly theo j, k tnh cho tng tt c cc im nh trong nh
v N l s im nh trong nh. RMSE l cn bc 2 ca MSE .
Vi hai nh hnh 2.6.6, s so snh v tham s PSNR cho trn bng
bn di. c th so snh d dng hn, ta xt nh c nn vi cc t l
khc nhau (o lng bi h s bt/pixel hay bpp). Tt c cc s liu trn
bng u cho thy JPEG2000 nn nh tt hn l JPEG; hn th h s PSNR
m chng ta xt trong bng c o trong h n v logarit.

Bit per pixel 0.125 0.50 2.00
nh 1 theo JPEG 24.42 31.17 35.15
nh 1 theo JPEG2000 28.12 32.95 37.35
nh 2 theo JPEG 22.6 28.92 35.99
nh 2 theo JPEG2000 24.85 31.13 38.80
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 65
Tnh nng u vit th 3 ca JPEG2000 so vi JPEG l chun nn nh
ny c th hin th c cc nh vi phn gii v kch thc khc nhau
t cng mt nh nn. Vi JPEG th iu ny l khng th thc hin c.
S d c iu ny l do JPEG2000 s dng k thut phn gii nh v m
ho nh km m chng ta ni ti phn m ho nh theo JPEG2000.
Tnh nng ny l mt li th c bit quan trng ca JPEG2000, trong
khi JPEG cng nh cc chun nn nh tnh trc y phi nn nhiu ln
thu c cht lng vi tng ln nn khc nhau th vi JPEG2000 ta ch
cn nn mt ln cn cht lng nh th s c quyt nh tu theo ngi s
dng trong qu trnh gii nn nh theo JPEG2000.Mt tnh nng u vit na
ca JPEG2000 l tnh nng m ho nh quan trng theo vng (ROI - Region
of Interest) m chng ta cp trong phn m ho nh theo JPEG2000.







Hnh 2.6.7 : Minh ho tnh nng ROI
JPEG2000 cn c mt kh nng c bit u vit hn so vi JPEG,
chnh l kh nng vt tri trong khi phc li. l khi mt nh c
truyn trn mng vin thng th thng tin c th b nhiu; vi cc chun nn
nh nh JPEG th nhiu ny s c thu vo v hin th, tuy nhin vi
JPEG2000, do c trng ca php m ho c th chng li, JPEG2000 c
th gim thiu cc li ny ti mc hu nh khng c.



n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 66
Ch-ng III:
Ci t ch-ng trnh v th nghim

- T nhng c s l thuyt trnh by trn em tin hnh ci t
ch- ng trnh cho mt s ph- ng php nn nh : RLE, HUFFMAN,
LZW, JPEG trn ngn ng lp trnh Visual C++ 6.0.
- Ch- ng trnh chy t- ng i n nh nh- ng ch h tr nh dng
nh Bitmap. Cc ph- ng php RLE, HUFFMAN, LZW ch- ng
trnh ch chy tt trn nh Bitmap 256 mu cn i vi JPEG th
hin thi mi h tr cho nh Bitmap 24 bit mu.
- Cc file nh - c nn theo cc ph- ng php khc nhau s - c l- u
vi cc nh dng ui khc nhau HUFFMAN (*.huff) , LZW
(*.lzw) , JPEG (*.jpg) ring RLE vn gi nguyn ui *.bmp.
- Ngoi cc th- vin v cc hm - c h tr sn trong Visual C++
6.0 ch- ng trnh cn s dng thm mt s th- vin ring.
- Cc thut ton u c - u nh- c im khc nhau v em li kt qu
ch- ng trnh khc nhau. Tc nn v hiu qu nn ca cc
ph- ng php rt khc nhau do phc tp gii thut v cht l- ng
nh kt qu yu cu l khc nhau. Ph- ng php RLE, HUFFMAN
cho kt qu nhanh chng v cht l- ng nh khng thay i nh- ng
hiu sut nn th- ng khng cao i vi nh Bitmap 256 mu cn
ph- ng php LZW do trong thut ton phi xy dng t in nn
tc nn t- ng i chm nh- ng kt qu nn rt cao.Cui cng
ph- ng php JPEG th cht l- ng nh nn v hiu qu nn t l
nghch vi nhau, cht l- ng nh nn tt th kch th- c file gim t
v ng- c li .

n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 67

Giao din chnh ch-ng trnh :












Ph- ng php
nn nh RLE
Ph- ng php
nn nh
HUFFMAN
Ph- ng php
nn nh LZW
Ph- ng php
nn nh JPEG
- ng dn
file ngun
- ng dn
file ch
Xem nh
Kch th- c
file ngun
Kch th- c
file ch

n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 68
Cc b-c thc hin ch-ng trnh :

1. Chn mc ch thc hin nn (Compression) hoc gii nn
(Decompression ) trong ph- ng php nn mun s dng.
2. Click vo nt Duongdan th nht m file mun thc thi ,nu
mun xem nh chn (nu l nh Bitmap) th click nt >> xem nh
cn kch th- c file hin th bn d- i.
3. Click vo nt Duongdan th hai ch - ng dn n file mun l- u
li kt qu.
4. Click nt Thuchien xong chng trnh s thng bo bng hp hi
thoi MessageBox cn ring vi JPEG ta cn phi chn cht l- ng
nh nn th mi thc hin nn. Cht l- ng cng cao th nh s t b
thay i nh- ng hiu sut nn thp v ng- c li.



5. Thng bo hon tt nn hoc gii nn



n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 69
Kt lun
Kt qu t -c v ng dng ca n
n trnh by cc khi nim quan trng cn thit ca k thut nn
nh ni chung v cc nguyn tc , c s l thuyt ,thut ton ca mt s
ph- ng php nn nh ph bin nh- : m lot di RLE, HUFFMAN, LZW,
JPEG, JPEG2000.Trong trnh by ph- ng php nn nh JPEG2000 s
dng bin i Wavelet nn nh, y l ph- ng php nn nh ang - c
quan tm pht trin v cc tnh nng ni bt so vi cc ph- ng php khc.
Ph- ng php ny khng ch cho hiu sut nn cao, cht l- ng nh bo m
so vi cc ph- ng php nn RLE, HUFFMAN, LZW, JPEG m cn cc tnh
nng ring bit do s- dng bin i Wavelet nn nh nh- : nn nh theo
vng (ROI) , trong mt nh cc vng hoc cc i t- ng c th c t l nn
khc nhau ,nn nh mt ln nh- ng c th gii nn nh vi cht l- ng nh v
kch th- c nh khc nhau tu theo yu cu ng- i s dng ....Cc ph- ng
php nn trnh by trong n l nhng ph- ng php ang - c s dng
kh rng ri trong nhiu lnh vc c bit trong truyn thng cho nh trn
mng m bo tc , thi gian v cht l- ng d liu truyn.
H-ng pht trin nghin cu
Mt s h- ng nghin cu trong t- ng lai :
- n mi cp n cc ph- ng php nn nh tnh m ch- a ng
dng chng cho m thanh, video, c bit l bin i Wavelet ca
chun JPEG2000 do vic i su nghin cu tm hiu cc h
ca Wavelet l rt cn thit.
- Nghin cu thm v cc gii thut SPIHT, EZW v cc ng dng
ca chng.
- Cc ph- ng php nn - c trnh by trong n ra i t
nhiu nm tr- c do cn nghin cu nhng ci tin i vi mi
phung php nng cao hiu qu nn.
n tt nghip Tm hiu mt s ph-ng php nn nh
Sinh vin thc hin : T Minh Thng CT 702 Trang : 70
Cc ti liu tham kho:

1. PGS.Nguyn Thanh Thu, Ths.L- ng Mnh B, Nhp mn x l
nh s
2. V c Khnh (2003),Gio trnh x l nh , Nxb Thng k, H Ni

3. D.Huffman , A method for the contruction of minimum
redundancy codes, Proc IRE 40, 1098-1101 (1952)

4. Nguyn Kim Sch (1997), X l nh v video s , Nxb Khoa hc v
K thut , H Ni .

5. Michael David Adams - Faouzi Kossentini - Touraji Ebrahimi -
JPEG2000 : The Next Generation Still Image Compression Standard
(2000)

6. Website: http://www.JPEG.org/JPEG2000.html

7. Website: http://www.codeproject.com