图像细化matlab代码实现

网友投稿 721 2022-12-01

图像细化matlab代码实现

图像细化matlab代码实现

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

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

图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:512for j=1:512sum=sum+I(I,j);endendT=sum/(512*512);for i=1:512for j=1:512if I(I,j)>TJ(I,j)=1;elseJ(I,j)=0;endendend% imshow(I,[])% title(‘原图’)figure,imshow(J,[])title(‘二值化后的图’)%首先要对指纹图象进行二值化G=double(size(515,515));for i=2:513for j=2:513G(I,j)=J(i-1,j-1);endendfor i=2:513G(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);endG(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:513for 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;elseG(I,j)=1;endelseif (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;elseG(I,j)=1;endelseif (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;elseG(I,j)=1;endelseif (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;elseG(I,j)=1;endelseif (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;elseG(I,j)=1;endelseif (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;elseG(I,j)=1;endelseif (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;elseG(I,j)=1;endelseif (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;elseG(I,j)=1;endelseG(I,j)=0;end% W(t;I,j)=G(I,j);endend% G1(i-1,j-1)=G(I,j);endt=t+1;endH=G(2:513,2:513); figure,imshow(H)title(‘细化后的图象’)

图3

图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)==1out=0;elseout=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)==1out=0;elseout=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;endN=N+1;last=previous;previous=current;if mod(N,2)==0,current=applylut(current,luteven);elsecurrent=applylut(current,lutodd);end;end;out=current;clc;clear;a=imread('w2.bmp');a=~a;b=zs(a);figure,imshow(b);

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:数字水印学习系统之三 DWT操作
下一篇:OpenCV学习笔记-阈值化
相关文章

 发表评论

暂时没有评论,来抢沙发吧~