matlab 图像细化代码,图像细化matlab代码实现

本文档展示了两个MATLAB程序,用于图像细化处理。One.m使用bwmorph函数进行细化操作,而Two.m则通过自定义算法实现细化,并进行二值化处理。程序中强调了'~a'操作的重要性,它将图像从黑色背景转换为白色背景,以获得预期结果。同时,Two.m中的细化算法基于Zhang-Suen骨架算法,通过迭代和条件判断实现细节优化。最后,程序显示细化前后的图像对比。
摘要由CSDN通过智能技术生成

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

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);

1ae755cd3119198872adeb19daecce03.png

图1

图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(‘细化后的图象’)

图3

66d251e32b58f326ed15e2f4fdef1c58.png

图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);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值