A. Mã nguồn đầy đủ của chương trình
A.3. Mã nguồn các module
1) Main:
y=imread(filename);
anhgoc=y;
%%%%%%%%%%%%% Cac bien phu trong chuong trinh
% bien de tranh pha tich trung lap global letraicu;
global lephaicu;
global startcu;
global limitcu;
%%%%%%%%%%%%%%%%%%%
% loc nhieu anh goc
y=double(y)/255; % chuyen sang kieu double
%%%%%%% mat na loc k=[0 1 0;1 2 1;0 1 0];
k=1/8*k;
%%%%%%% dung ham loc y=conv2(y,k);
y=uint8(round(y*255));
%%%%%%%%%%%%%%%%
kich_thuoc_anh=size(y);
chieucao=kich_thuoc_anh(1);
chieurong=kich_thuoc_anh(2);
%%%%%%%%%Phan nguong va tim hinh chieu doc gray_min=min(min(y));
gray_max=max(max(y));
nguongtach=round(((gray_min+gray_max)/2));
Main
(chương trình chính)
Topdown
FSign
FSignKV
DemoFS
Phantich
Phantichtrong
for i=1:chieucao for j=1:chieurong
if (y(i,j)>nguongtach) % vung nen y(i,j)=255; % xoa trang vung nen else
z(i)=z(i)+1; % tinh cho bieu do xam end
end end
%%%%%%%%%%%%%%%
chieu_cao_hang=timhang(z);
%%%%%%%%%%%%%%%%% tach khoi bang cach vao tung khoi van ban ngang tim hinh
%%%%%%%%%%%%%%%%% chieu ngang i=1;
start=i;
limit=i;
luocdo=zeros(1,chieurong);
while (i<=chieucao)
while (i<=chieucao)&(z(i)>(2*min(z))) %di het vung van ban de tinh chieu cao i=i+1;
end
limit=i-1; %limit-start la chieu cao vung van ban ngang luocdo=zeros(1,chieurong);
% tinh luoc do xam cho moi vung ngang if (start<chieucao)&(limit>start)
if limit>chieucao
limit=chieucao; % tranh vuot qua vung bien anh end
if (limit-start)>=(0.5*chieu_cao_hang)
y=phantich(start,limit,y,nguongtach,chieu_cao_hang,1,size(y,2));
end end
while (i<=chieucao)&(z(i)<=(2*min(z))) i=i+1;
end start=i;
end
imshow(y);
2) Topdown:
y=imread(filename);
anhgoc=y;
%%%%%%%%%%%%% Cac bien phu trong chuong trinh
% bien de tranh pha tich trung lap global letraicu;
global lephaicu;
global startcu;
global limitcu;
%%%%%%%%%%%%%%%%%%%
% loc nhieu anh goc
y=double(y)/255; % chuyen sang kieu double
%%%%%%% mat na loc k=[0 1 0;1 2 1;0 1 0];
k=1/8*k;
%%%%%%% dung ham loc y=conv2(y,k);
y=uint8(round(y*255));
%%%%%%%%%%%%%%%%
kich_thuoc_anh=size(y);
chieucao=kich_thuoc_anh(1);
chieurong=kich_thuoc_anh(2);
%%%%%%%%%Phan nguong va tim hinh chieu doc gray_min=min(min(y));
gray_max=max(max(y));
nguongtach=round(((gray_min+gray_max)/2));
z=zeros(1,chieucao);% z la hinh chieu doc for i=1:chieucao
for j=1:chieurong
if (y(i,j)>nguongtach) % vung nen y(i,j)=255; % xoa trang vung nen else
z(i)=z(i)+1; % tinh cho bieu do xam end
end end
%%%%%%%%%%%%%%%
chieu_cao_hang=timhang(z);
%%%%%%%%%%%%%%%%% tach khoi bang cach vao tung khoi van ban ngang tim hinh
%%%%%%%%%%%%%%%%% chieu ngang i=1;
start=i;
limit=i;
luocdo=zeros(1,chieurong);
while (i<=chieucao)
while (i<=chieucao)&(z(i)>(2*min(z))) %di het vung van ban de tinh chieu cao i=i+1;
end
limit=i-1; %limit-start la chieu cao vung van ban ngang luocdo=zeros(1,chieurong);
% tinh luoc do xam cho moi vung ngang if (start<chieucao)&(limit>start)
if limit>chieucao
limit=chieucao; % tranh vuot qua vung bien anh end
if (limit-start)>=(0.5*chieu_cao_hang)
y=phantich(start,limit,y,nguongtach,chieu_cao_hang,1,size(y,2));
end end
while (i<=chieucao)&(z(i)<=(2*min(z))) i=i+1;
end start=i;
end
% Lam noi vung van ban
%%%%%%%%%%%%%%%%%%%
%subplot(1,2,1);
%title('ANH GOC');
%imshow(anhgoc,'InitialMagnification',100);
%subplot(1,2,2);
%title('KET QUA');
imshow(y);
3) Phantich:
function ketqua=phantich(start,limit,anh,nguongtach,chieu_cao_hang,bientrai,bienphai)
%%%%%%%%%%%%%%%
global letraicu;
global lephaicu;
global startcu;
%%%%%%%%%%%%%%%%
CDmang=bienphai-bientrai+1;
luocdo=zeros(1,CDmang);
for k=start:limit % tim hinh chieu ngang for q=bientrai:bienphai
if (anh(k,q)<=nguongtach)
luocdo(q-bientrai+1)=luocdo(q-bientrai+1)+1;
end end end
%%%%%%%%%%%%% tim cuc tieu trong luoc do xam nhung bo qua suon dau va cuoi dau=1;
cuoi=CDmang;
while (luocdo(dau+1)>=luocdo(dau))&(dau<CDmang) dau=dau+1;
end
while (luocdo(cuoi-1)>=luocdo(cuoi))&(cuoi>2) cuoi=cuoi-1;
end
%%%%%%%%%%%%
if cuoi>dau
cuctieu=round(min(luocdo(dau:cuoi))*1.2);
else
cuctieu=bienphai-bientrai;
end
%%%%%%%%%%%%
if (cuctieu>0)&((limit-start)>= 2*chieu_cao_hang)% vung anh for hang_anh=start:limit
for cot_anh=bientrai:bienphai
anh(hang_anh,cot_anh)=255-anh(hang_anh,cot_anh);
end end else
dorong=0;
truoc=luocdo(1);
letrai=bientrai;
lephai=bientrai;
i=bientrai;
dautien=0;
while(i<=bienphai)
while (i<=bienphai)&(luocdo(i-bientrai+1)<=cuctieu) % tim diem bat dau vung van ban
i=i+1;
dorong=dorong+1;
end
% if (letrai==bientrai) % doan trang dau qua ngan````````````````````
% letrai=i;dautien=1;
% end
if (dorong>=(1.2*chieu_cao_hang))|((lephai>letrai)&(i>=bienphai)) % khoang canh cho phep
if (lephai>letrai) % tien hanh ve vung van ban % dautien=0;
if ((limit-start)< 2*chieu_cao_hang) % khung vung van ban
if lephai>bienphai lephai=bienphai;
end
for ve_bien_ngang=letrai:lephai anh(start,ve_bien_ngang)=0;
anh(limit,ve_bien_ngang)=0;
end
for ve_bien_doc=start:limit anh(ve_bien_doc,letrai)=0;
anh(ve_bien_doc,lephai)=0;
end else
if (letraicu==letrai) & (lephaicu==lephai)&(startcu==start)&(limitcu==limit) % tranh bi treo
% trung khoi phan tich letraicu
lephaicu startcu limitcu else
letraicu=letrai;
lephaicu=lephai;
startcu=start;
limitcu=limit;
anh=phantichtrong(start,limit,letrai,lephai,anh,nguongtach,chieu_cao_hang);
end end end
letrai=i;% bat dau mot vung van ban dorong=0;
else
dorong=0;
end
while (i<=bienphai)&(luocdo(i-bientrai+1)>cuctieu) i=i+1; % di qua vung van ban
lephai=i;
end end end
ketqua=anh;
4) Phantichtrong:
function ketqua=phantichtrong(start,limit,letrai,lephai,anh,nguongtach,chieu_cao_hang) y=anh;
chieucao=limit;
chieurong=lephai;
%%%%%%%%%Phan nguong va tim hinh chieu doc
%z=zeros(1,size(y,1));% z la hinh chieu doc z=zeros(1,limit-start+1);% z la hinh chieu doc for i=start:chieucao
for j=letrai:chieurong
if (y(i,j)>nguongtach) % vung nen else
z(i-start+1)=z(i-start+1)+1; % tinh cho bieu do xam end
end end
%%%%%%%%%%%%%%%%% tach khoi bang cach vao tung khoi van ban ngang tim hinh
%%%%%%%%%%%%%%%%% chieu ngang i=start;
start_t=i;
while (i<=chieucao)
while (i<=chieucao)&(z(i-start+1)>(2*min(z))) %di het vung van ban de tinh chieu cao i=i+1;
end
limit_t=i-1; %limit-start la chieu cao vung van ban ngang % tinh luoc do xam cho moi vung ngang
if (start_t<chieucao)&(limit_t>start_t) if limit_t>chieucao
limit_t=chieucao; % tranh vuot qua vung bien anh end
y=phantich(start_t,limit_t,y,nguongtach,chieu_cao_hang,letrai,lephai);
end
while (i<=chieucao)&(z(i-start+1)<=(2*min(z))) i=i+1;
end start_t=i;
end
%%%%%%%%%%%%%%%%%%%
ketqua=y;
5) Fsign
%anh=imread('C:\chuongtrinh\anhmau\textc3.jpg'); % doc anh tu mot file JPG anh=imread(filename); % doc anh tu mot file JPG
N=15; % kich thuoc moi block
k=1; % so block tren anh co kich thuoc NxN chieucao=size(anh,1); % chieu cao cuar anh vao chieucao_goc=chieucao;
chieurong=size(anh,2); % chieu rong cua anh vao chieurong_goc=chieurong;
%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%
%%%%%%%%%Phan nguong va tim hinh chieu doc gray_min=min(min(anh));
gray_max=max(max(anh));
nguongtach=round(((gray_min+gray_max)/2));
% chuan hoa kich thuoc anh the nguyen lan cua NxN
%chuan chieu cao anh
if (chieucao/N)>round(chieucao/N) chieucao=round(chieucao/N)*N+N;
else
chieucao=round(chieucao/N)*N;
end
%chuan chieu rong anh
if (chieurong/N)>round(chieurong/N) chieurong=round(chieurong/N)*N+N;
else
chieurong=round(chieurong/N)*N;
end
%anh=zeros(1:chieucao,1:chieurong);
%%%%%%%%%%%%%%%%%%%%
%loc nhieu anh goc
%anh = filter2(fspecial('average',3),anh);
anh=double(anh)/255; % chuyen sang kieu double
%%%%%%% mat na loc loc=[0 1 0;1 2 1;0 1 0];
loc=1/8*loc;
%%%%%%% dung ham loc anh=conv2(anh,loc);
anh=uint8(round(anh*255));
%%%%%%%%%%%%%%
for i=1:chieucao for j=1:chieurong
if (i>chieucao_goc)|(j>chieurong_goc) % ngoai vung bien anh goc anh(i,j)=0; % dat la vung nen anh
else % trong vung bien anh goc %anh(i,j)=255-anh(i,j);
if (anh(i,j)>nguongtach) % vung nen %anh(i,j)=255; % xoa trang vung nen anh(i,j)=0;
else
%z(i)=z(i)+1; % tinh cho bieu do xam anh(i,j)=255-anh(i,j);
end end
end end
%%%%%%%%%%%%%%% tinh FS cho moi block FS=[];
k=0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
u_layer1=zeros(N+2);b_layer1=zeros(N+2);u_layer2=zeros(N+2);b_layer2=zeros(N+2);bloc k_k=zeros(N+2);
%%%%%%%%%%%%%%%%%%%%%%
for i=2:round(chieucao/N) for(j=2:round(chieurong/N))
%%%%%%%%%% thao tac voi block thu K
u_layer1(:,:)=0; b_layer1(:,:)=0; u_layer2(:,:)=0; b_layer2(:,:)=0;
vol1=0; vol2=0;
block_k(2:N+1,2:N+1)=anh((i-1)*N+1:i*N,(j-1)*N+1:j*N);
% tinh gia tri cho lop tren va duoi voi delta1 cho moi vung thu k for ki=2:N+1
for kj=2:N+1
g1_u=block_k(ki,kj)+1;
g1_b=block_k(ki,kj)-1;
g2=block_k(ki,kj+1);
g3=block_k(ki,kj-1);
g4=block_k(ki+1,kj);
g5=block_k(ki-1,kj);
temp=[g1_u g2 g3 g4 g5 g1_b];
u_layer1(ki,kj)=max(temp(1:5));
b_layer1(ki,kj)=min(temp(2:6));
vol1=vol1 + (u_layer1(ki,kj)-b_layer1(ki,kj));
% tinh xong lop tren va duoi voi delta1 % tinh gia tri cho lop tren va duoi voi delta2 g1_u=u_layer1(ki,kj)+1;
g1_b=b_layer1(ki,kj)-1;
g2=u_layer1(ki,kj+1);
g3=u_layer1(ki,kj-1);
g4=u_layer1(ki+1,kj);
g5=u_layer1(ki-1,kj);
temp=[g1_u g2 g3 g4 g5 g1_b];
u_layer2(ki,kj)=max(temp(1:5));
b_layer2(ki,kj)=min(temp(2:6));
vol2=vol2+(u_layer2(ki,kj)-b_layer2(ki,kj));
end end % tinh FS k=k+1;
FS(k)=(vol2-vol1)/2;
end end
%%%%%%%%%%%%%%%% Tach anh va text k=0;
%nguong=((min(FS)+max(FS))/2);
%nguong=3000;
anh=255-anh;
anh=cat(3,anh,anh,anh);
nguong=min(FS)+50;
for i=2:round(chieucao/N) for(j=2:round(chieurong/N)) k=k+1;
if (FS(k)>4000) % anh
anh((i-1)*N+1:i*N,(j-1)*N+1:j*N,2:3)=20;
elseif (FS(k)>nguong) % text
anh((i-1)*N+1:i*N,(j-1)*N+1:j*N,1)=150;
anh((i-1)*N+1:i*N,(j-1)*N+1:j*N,3)=150;
end end end
imshow(anh)
6) FSignKV
global anh
anh=imread(filename); % doc anh tu mot file JPG N=15; % kich thuoc moi block
k=1; % so block tren anh co kich thuoc NxN chieucao=size(anh,1); % chieu cao cuar anh vao chieucao_goc=chieucao;
chieurong=size(anh,2); % chieu rong cua anh vao chieurong_goc=chieurong;
%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%
%%%%%%%%%Phan nguong va tim hinh chieu doc gray_min=min(min(anh));
gray_max=max(max(anh));
nguongtach=round(((gray_min+gray_max)/2));
% chuan hoa kich thuoc anh the nguyen lan cua NxN
%chuan chieu cao anh
if (chieucao/N)>round(chieucao/N) chieucao=round(chieucao/N)*N+N;
else
chieucao=round(chieucao/N)*N;
end
%chuan chieu rong anh
if (chieurong/N)>round(chieurong/N) chieurong=round(chieurong/N)*N+N;
else
chieurong=round(chieurong/N)*N;
end
%%%%%%%%%%%%%%%%%%%%
%loc nhieu anh goc
%anh = filter2(fspecial('average',3),anh);
anh=double(anh)/255; % chuyen sang kieu double
%%%%%%% mat na loc loc=[0 1 0;1 2 1;0 1 0];
loc=1/8*loc;
%%%%%%% dung ham loc anh=conv2(anh,loc);
anh=uint8(round(anh*255));
%%%%%%%%%%%%%%
for i=1:chieucao for j=1:chieurong
if (i>chieucao_goc)|(j>chieurong_goc) % ngoai vung bien anh goc anh(i,j)=0; % dat la vung nen anh
else % trong vung bien anh goc %anh(i,j)=255-anh(i,j);
if (anh(i,j)>nguongtach) % vung nen %anh(i,j)=255; % xoa trang vung nen anh(i,j)=0;
else
%z(i)=z(i)+1; % tinh cho bieu do xam anh(i,j)=255-anh(i,j);
end end
end end
%%%%%%%%%%%%%%% tinh FS cho moi block FS=[];
k=0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
u_layer1=zeros(N+2);b_layer1=zeros(N+2);u_layer2=zeros(N+2);b_layer2=zeros(N+2);bloc k_k=zeros(N+2);
%%%%%%%%%%%%%%%%%%%%%%
% mang chua FS global AFS
AFS=zeros(round(chieucao/N)-1,round(chieurong/N)-1);
%%%%%%%%%%%%%%%%%%%%%%
for i=2:round(chieucao/N) for(j=2:round(chieurong/N))
%%%%%%%%%% thao tac voi block thu K
u_layer1(:,:)=0; b_layer1(:,:)=0; u_layer2(:,:)=0; b_layer2(:,:)=0;
vol1=0; vol2=0;
block_k(2:N+1,2:N+1)=anh((i-1)*N+1:i*N,(j-1)*N+1:j*N);
% tinh gia tri cho lop tren va duoi voi delta1 cho moi vung thu k for ki=2:N+1
for kj=2:N+1
g1_u=block_k(ki,kj)+1;
g1_b=block_k(ki,kj)-1;
g2=block_k(ki,kj+1);
g3=block_k(ki,kj-1);
g4=block_k(ki+1,kj);
g5=block_k(ki-1,kj);
temp=[g1_u g2 g3 g4 g5 g1_b];
u_layer1(ki,kj)=max(temp(1:5));
b_layer1(ki,kj)=min(temp(2:6));
% tinh xong lop tren va duoi voi delta1 % tinh gia tri cho lop tren va duoi voi delta2 g1_u=u_layer1(ki,kj)+1;
g1_b=b_layer1(ki,kj)-1;
g2=u_layer1(ki,kj+1);
g3=u_layer1(ki,kj-1);
g4=u_layer1(ki+1,kj);
g5=u_layer1(ki-1,kj);
temp=[g1_u g2 g3 g4 g5 g1_b];
u_layer2(ki,kj)=max(temp(1:5));
b_layer2(ki,kj)=min(temp(2:6));
vol2=vol2+(u_layer2(ki,kj)-b_layer2(ki,kj));
%%%%%%%%%%%%%%%% tinh xong gia tri cua lop tren duoi voi delta2 end
end % tinh FS k=k+1;
FS(k)=(vol2-vol1)/2;
% AFS
%AFS(i,j)=(vol2-vol1)/2;
end end
%%%%%%%%%%%%%%%% Tach anh va text k=0;
anh=cat(3,anh,anh,anh);
nguong=min(FS)+50;
for i=1:round(chieucao/N-1) for(j=1:round(chieurong/N)-1) k=k+1;
if ((FS(k)>nguong)&(FS(k)<4000)) % text AFS(i,j)=1; % danh dau vung text elseif (FS(k)>=4000) % vung anh AFS(i,j)=2; % danh dau vung anh end
end end
%%%%%%%%% khoanh vung global MLT
MLT=AFS;
MLT=0; % chua 1 khoi cac phan tu lien thong
%%%%%%%%%%%
set(0,'RecursionLimit',1000)
%%%%%%%%%%%%
anh=255-anh;
for i=1:round(chieucao/N-1) for(j=1:round(chieurong/N)-1)
if ((AFS(i,j)~=3)&(AFS(i,j)==1)); % phan tu la text chua xet trong anh MLT=0;
lienthong(i,j,1);
if(sum(sum(MLT))>20 ) % bo qua vung lien thong qua nho vebien(MLT,N,1);
end
elseif ((AFS(i,j)~=3)&(AFS(i,j)==2)); % phan tu la text chua xet trong anh MLT=0;
lienthong(i,j,2);
if(sum(sum(MLT))>20 ) % bo qua vung lien thong qua nho vebien(MLT,N,2);
end
end end
end
imshow(anh)
7) DemFS:
anh=imread(filename); % doc anh tu mot file JPG goc=anh;
N=15; % kich thuoc moi block
k=1; % so block tren anh co kich thuoc NxN chieucao=size(anh,1); % chieu cao cuar anh vao chieucao_goc=chieucao;
chieurong=size(anh,2); % chieu rong cua anh vao chieurong_goc=chieurong;
%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%
%%%%%%%%%Phan nguong va tim hinh chieu doc gray_min=min(min(anh));
gray_max=max(max(anh));
nguongtach=round(((gray_min+gray_max)/2));
% chuan hoa kich thuoc anh the nguyen lan cua NxN
%chuan chieu cao anh
if (chieucao/N)>round(chieucao/N) chieucao=round(chieucao/N)*N+N;
else
chieucao=round(chieucao/N)*N;
end
%chuan chieu rong anh
if (chieurong/N)>round(chieurong/N) chieurong=round(chieurong/N)*N+N;
else
chieurong=round(chieurong/N)*N;
end
%%%%%%%%%%%%%%%%%%%%
%loc nhieu anh goc
anh=double(anh)/255; % chuyen sang kieu double
%%%%%%% mat na loc loc=[0 1 0;1 2 1;0 1 0];
loc=1/8*loc;
%%%%%%% dung ham loc anh=conv2(anh,loc);
anh=uint8(round(anh*255));
%%%%%%%%%%%%%%
for i=1:chieucao for j=1:chieurong
if (i>chieucao_goc)|(j>chieurong_goc) % ngoai vung bien anh goc anh(i,j)=0; % dat la vung nen anh
else % trong vung bien anh goc %anh(i,j)=255-anh(i,j);
if (anh(i,j)>nguongtach) % vung nen %anh(i,j)=255; % xoa trang vung nen anh(i,j)=0;
else
%z(i)=z(i)+1; % tinh cho bieu do xam anh(i,j)=255-anh(i,j);
end end
end end
FS=[];
k=0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
u_layer1=zeros(N+2);b_layer1=zeros(N+2);u_layer2=zeros(N+2);b_layer2=zeros(N+2);bloc k_k=zeros(N+2);
%%%%%%%%%%%%%%%%%%%%%%
for i=1:round(chieucao/N) for(j=1:round(chieurong/N))
%%%%%%%%%% thao tac voi block thu K
u_layer1(:,:)=0; b_layer1(:,:)=0; u_layer2(:,:)=0; b_layer2(:,:)=0;
vol1=0; vol2=0;
block_k(2:N+1,2:N+1)=anh((i-1)*N+1:i*N,(j-1)*N+1:j*N);
% tinh gia tri cho lop tren va duoi voi delta1 cho moi vung thu k for ki=2:N+1
for kj=2:N+1
g1_u=block_k(ki,kj)+1;
g1_b=block_k(ki,kj)-1;
g2=block_k(ki,kj+1);
g3=block_k(ki,kj-1);
g4=block_k(ki+1,kj);
g5=block_k(ki-1,kj);
temp=[g1_u g2 g3 g4 g5 g1_b];
u_layer1(ki,kj)=max(temp(1:5));
b_layer1(ki,kj)=min(temp(2:6));
vol1=vol1 + (u_layer1(ki,kj)-b_layer1(ki,kj));
% tinh xong lop tren va duoi voi delta1 % tinh gia tri cho lop tren va duoi voi delta2 g1_u=u_layer1(ki,kj)+1;
g1_b=b_layer1(ki,kj)-1;
g2=u_layer1(ki,kj+1);
g3=u_layer1(ki,kj-1);
g4=u_layer1(ki+1,kj);
g5=u_layer1(ki-1,kj);
temp=[g1_u g2 g3 g4 g5 g1_b];
u_layer2(ki,kj)=max(temp(1:5));
b_layer2(ki,kj)=min(temp(2:6));
vol2=vol2+(u_layer2(ki,kj)-b_layer2(ki,kj));
%%%%%%%%%%%%%%%% tinh xong gia tri cua lop tren duoi voi delta2 end
end
% ve he truc toa do trucx=[1:N+2];
trucy=[1:N+2];
meshgrid(trucx,trucy);
% axes(handles.anhgoc);
subplot(2,2,1);
anhtem=goc;
anhtem((i-1)*N+1:(i-1)*N+N,(j-1)*N+1:(j-1)*N+N)=0;
imshow(anhtem);
subplot(2,2,2);
%axes(handles.uper);
% surf(trucx,trucy,b_layer2) surf(trucx,trucy,u_layer2) subplot(2,2,3);
%axes(handles.block);
imshow(255-anh((i-1)*N+1:(i-1)*N+N,(j-1)*N+1:(j-1)*N+N));
subplot(2,2,4);
%axes(handles.surf);
trucx=[1:N];
trucy=[1:N];
meshgrid(trucx,trucy);
surf(trucx,trucy,double(anh((i-1)*N+1:(i-1)*N+N,(j-1)*N+1:(j-1)*N+N)));
pause(0.3) % tinh FS k=k+1;
FS(k)=(vol2-vol1)/2;
end end
8) Lienthong
function lienthong(i,j,loai) % loai=1 la text, =2 la anh global AFS;
global MLT;
AFS(i,j)=3; % danh dau la da xet
MLT(i,j)=1; % danh dau diem thuoc vung lien thong
if (((i-1)>0)&((j-1)>0)& (AFS(i-1,j-1)~=3)&(AFS(i-1,j-1)==loai)) lienthong(i-1,j-1,loai);end if (((j-1)>0)& (AFS(i,j-1)~=3)&(AFS(i,j-1)==loai)) lienthong(i,j-1,loai);end
if (((i+1)<size(AFS,1))&((j-1)>0)&(AFS(i+1,j-1)~=3)&(AFS(i+1,j-1)==loai))lienthong(i+1,j- 1,loai);end
if (((i-1)>0)& (AFS(i-1,j)~=3)&(AFS(i-1,j)==loai)) lienthong(i-1,j,loai);end
if (((i+1)<size(AFS,1))& (AFS(i+1,j)~=3)&(AFS(i+1,j)==loai)) lienthong(i+1,j,loai);end if (((i-1)>0)&((j+1)<size(AFS,2))& (AFS(i-1,j+1)~=3)&(AFS(i-1,j+1)==loai)) lienthong(i- 1,j+1,loai);end
if (((j+1)<size(AFS,2))& (AFS(i,j+1)~=3)&(AFS(i,j+1)==loai)) lienthong(i,j+1,loai);end if (((i+1)<size(AFS,1))&((j+1)<size(AFS,2))& (AFS(i+1,j+1)~=3)&(AFS(i+1,j+1)==loai)) lienthong(i+1,j+1,loai);end
9) Vebien
function vebien(MLT,N,loai) global anh;
m=size(MLT,1); % chieu cao n=size(MLT,2); % chieu rong MLTF=zeros(m+4,n+4);
MLTF(3:m+2,3:n+2)=MLT; % de da bao kong bi vuot bien
%MLTF
for i=3:m+1 % tim diem den dau tien for j=3:n+1
if (MLTF(i,j)==1)break end
end
if (MLTF(i,j)==1)break end
end
%%%%%%%%%%%%%%%
% quy dinh huong
% 1: len
% 2: phai
% 3: xuong
% 4: trai
%%%%%%%%%%%%%%% thuat toan do bien huong=2;
gocx=i;
gocy=j;
hanhcu=i;