====================================================================

One.m

该程序实现了细化,应用的是matlab子带的函数,注意“a=~a”,非常关键,我调试了一个下午发现,如果不是白字黑底,则情况很糟糕。

程序中,b=bwmorph(a,'thin',Inf);换成“b=bwmorph(a,'skel',Inf);”则可以得到同样结果。

clc;

clear;

a=imread('w2.bmp');

a=~a;

% subplot(1,2,1),imshow(a);

% b=bwmorph(a,'thin',Inf);

% subplot(1,2,2),imshow(b);

% figure,imshow(b);

%%%%%%%%%%%%%%%%%%%%

figure,imshow(a);

b=bwmorph(a,'thin',Inf);

figure,imshow(b);

图像细化matlab代码实现_image

图1

图像细化matlab代码实现_xp_02

图2

Two.m

效果不是太好,可以看图3和图4.

% clear;
clc;
% A=imread(‘fingerprint.jpg’);
A=imread(‘w.bmp’);
I=double(imresize(A,[512 512]));
J=double(size(512,512));
sum=0;
for i=1:512
for j=1:512
sum=sum+I(I,j);
end
end
T=sum/(512*512);
for i=1:512
for j=1:512
if I(I,j)>T
J(I,j)=1;
else
J(I,j)=0;
end
end
end
% imshow(I,[])
% title(‘原图’)
figure,imshow(J,[])
title(‘二值化后的图’)%首先要对指纹图象进行二值化
G=double(size(515,515));
for i=2:513
for j=2:513
G(I,j)=J(i-1,j-1);
end
end
for i=2:513
G(I,1)=J(i-1,1);
G(I,514)=J(i-1,512);
G(I,515)=J(i-1,512);
G(1,i)=J(1,i-1);
G(514,i)=J(512,i-1);
G(515,i)=J(512,i-1);
end
G(1,1)=J(1,1);
G(1,514)=J(1,512);
G(1,515)=J(1,512);
G(514,1)=J(512,1);
G(515,1)=J(512,1);
G(514,514)=J(512,512);
G(515,515)=J(512,512);
t=0;
H=zeros(512,512);
% W=zeros(512;G);
while (t<=10)%选10为界仅是根据这副图所得的测试最小值
for i=2:513
for j=2:513
% W(t;I,j)-W(t-1;I,j)==0)
% p1=G(i-1,j-1);
% p2=G(i-1,j);
% p3=G(i-1,j+1);
% p4=G(I,j-1);
% p5=G(I,j);
% p6=G(I,j+1);
% p7=G(i+1,j-1);
% p8=G(i+1,j);
% p9=G(i+1,j+1);
% p10=G(I,j+2);
% p11=G(i+2,j);
if (G(I,j)==0)
if (G(i-1,j-1)==1&&G(i-1,j)==1&&G(i-1,j+1)==1&&G(I,j-1)==0&&G(I,j)==0&&G(I,j+1)==0&&G(i+1,j)==0)
if ((G(i-1,j)==1&&G(I,j)==0&&G(i+1,j)==0&&G(i+2,j)==1)||(G(I,j-1)==1&&G(I,j)==0&&G(I,j+1)==0&&G(I,j+2)==1))
G(I,j)=0;
else
G(I,j)=1;
end
elseif (G(i-1,j-1)==1&&G(i-1,j)==0&&G(I,j-1)==1&&G(I,j)==0&&G(I,j+1)==0&&G(i+1,j-1)==1&&G(i+1,j)==0)
if ((G(i-1,j)==1&&G(I,j)==0&&G(i+1,j)==0&&G(i+2,j)==1)||(G(I,j-1)==1&&G(I,j)==0&&G(I,j+1)==0&&G(I,j+2)==1))
G(I,j)=0;
else
G(I,j)=1;
end
elseif (G(i-1,j)==0&&G(i-1,j+1)==1&&G(I,j-1)==0&&G(I,j)==0&&G(I,j+1)==1&&G(i+1,j)==0&&G(i+1,j+1)==1)
if ((G(i-1,j)==1&&G(I,j)==0&&G(i+1,j)==0&&G(i+2,j)==1)||(G(I,j-1)==1&&G(I,j)==0&&G(I,j+1)==0&&G(I,j+2)==1))
G(I,j)=0;
else
G(I,j)=1;
end
elseif (G(i-1,j)==0&&G(I,j-1)==0&&G(I,j)==0&&G(I,j+1)==0&&G(i+1,j-1)==1&&G(i+1,j)==1&&G(i+1,j+1)==1)
if ((G(i-1,j)==1&&G(I,j)==0&&G(i+1,j)==0&&G(i+2,j)==1)||(G(I,j-1)==1&&G(I,j)==0&&G(I,j+1)==0&&G(I,j+2)==1))
G(I,j)=0;
else
G(I,j)=1;
end
elseif (G(i-1,j)==1&&G(i-1,j+1)==1&&G(I,j-1)==0&&G(I,j)==0&&G(I,j+1)==1&&G(i+1,j)==0)
if ((G(i-1,j)==1&&G(I,j)==0&&G(i+1,j)==0&&G(i+2,j)==1)||(G(I,j-1)==1&&G(I,j)==0&&G(I,j+1)==0&&G(I,j+2)==1))
G(I,j)=0;
else
G(I,j)=1;
end
elseif (G(i-1,j-1)==1&&G(i-1,j)==1&&G(I,j-1)==1&&G(I,j)==0&&G(I,j+1)==0&&G(i+1,j)==0)
if ((G(i-1,j)==1&&G(I,j)==0&&G(i+1,j)==0&&G(i+2,j)==1)||(G(I,j-1)==1&&G(I,j)==0&&G(I,j+1)==0&&G(I,j+2)==1))
G(I,j)=0;
else
G(I,j)=1;
end
elseif (G(i-1,j)==0&&G(I,j-1)==1&&G(I,j)==0&&G(I,j+1)==0&&G(i+1,j-1)==1&&G(i+1,j)==1)
if ((G(i-1,j)==1&&G(I,j)==0&&G(i+1,j)==0&&G(i+2,j)==1)||(G(I,j-1)==1&&G(I,j)==0&&G(I,j+1)==0&&G(I,j+2)==1))
G(I,j)=0;
else
G(I,j)=1;
end
elseif (G(i-1,j)==0&&G(I,j-1)==0&&G(I,j)==0&&G(I,j+1)==1&&G(i+1,j)==1&&G(i+1,j+1)==1)
if ((G(i-1,j)==1&&G(I,j)==0&&G(i+1,j)==0&&G(i+2,j)==1)||(G(I,j-1)==1&&G(I,j)==0&&G(I,j+1)==0&&G(I,j+2)==1))
G(I,j)=0;
else
G(I,j)=1;
end
else
G(I,j)=0;
end
% W(t;I,j)=G(I,j);
end
end
% G1(i-1,j-1)=G(I,j);
end
t=t+1;
end
H=G(2:513,2:513);
figure,imshow(H)
title(‘细化后的图象’)

图像细化matlab代码实现_xp_03

图3

图像细化matlab代码实现_matlab_04

图4

如下程序,同样道理,要有“a=~a;”,否则得到结果很糟糕!

function out=zsodd(nbhd);
s=sum(nbhd(:))-nbhd(5);
temp1=(2<=s)&(s<=6);
p=[nbhd(1) nbhd(4) nbhd(7) nbhd(8) nbhd(9) nbhd(6) nbhd(3) nbhd(2)];
pp=[p(2:8) p(1)];
xp=sum((1-p).*pp);
temp2=(xp==1);
prod1=nbhd(4)*nbhd(8)*nbhd(6);
prod2=nbhd(8)*nbhd(6)*nbhd(2);
temp3=(prod1==0)&(prod2==0);
if temp1&temp2&temp3&nbhd(5)==1
out=0;
else
out=nbhd(5);
end;
function out=zseven(nbhd);
s=sum(nbhd(:))-nbhd(5);
temp1=(2<=s)&(s<=6);
p=[nbhd(1) nbhd(4) nbhd(7) nbhd(8) nbhd(9) nbhd(6) nbhd(3) nbhd(2)];
pp=[p(2:8) p(1)];
xp=sum((1-p).*pp);
temp2=(xp==1);
prod1=nbhd(4)*nbhd(8)*nbhd(2);
prod2=nbhd(4)*nbhd(6)*nbhd(2);
temp3=(prod1==0)&(prod2==0);
if temp1&temp2&temp3&nbhd(5)==1
out=0;
else
out=nbhd(5);
end;
function out=zs(im)
%
%zs appises the Zhang-Suen skeletonization algorithm to image IM. IM must
%be binary.
%
luteven=makelut('zseven',3);
lutodd=makelut('zsodd',3);
done=0;
N=2;
last=im;
previous=applylut(last ,lutodd);
current=applylut(previous,luteven);
while done==0,
if all(current(:)==last(:)),
done=1;
end
N=N+1;
last=previous;
previous=current;
if mod(N,2)==0,
current=applylut(current,luteven);
else
current=applylut(current,lutodd);
end;
end;
out=current;
clc;
clear;
a=imread('w2.bmp');
a=~a;
b=zs(a);
figure,imshow(b);