% write by 海轰
%该程序针对特定图片其作用
%为测试程序
t=im2bw(imread('homework1.png'));
%过滤边缘白点 这里仅针对这幅图像
t(:,1)=0;
t(140:145,:)=0;
imshow(t),title('原图');
[m,n]=size(t);
%行扫描 找出图像白色区域最左边的端点
for i=1:m
x=0;y=141;
tem=t(i,:);
h=sum(tem);
if h~=0
for j=1:n
if tem(j)==1
x=j;
break
end
end
%行扫描 反转 寻找白点区域最右边的端点
tem=flip(tem);
for j=1:n
if tem(j)==1
y=n-j+1;
break;
end
end
%对区域进去填充 便于找到边界
for j=x+1:y-1
t(i,j)=1;
end
end
end
figure,imshow(t),title('填充区域后');
t=bwperim(t,8);
figure,imshow(t),title('找到边界');
a=t;
rt=zeros(145,141);
%对图像进行重取样
%假设原图像为100*100 一点为(62,73)
%重取样模板为100*100 但是间隔为10 (分成10*10)
%先对(62,73)/10=(6.2,7.3)
%再取整 (6,7)
%再还原 10*(6,7)=(60,70)
%这样重取样后得到(60,70)
for i=1:m
for j=1:n
if t(i,j)==1
if round(j/15)==0
rt(15*round(i/15),15*(round(j/15)+1))=1;
else
rt(15*round(i/15),15*round(j/15))=1;
end
end
end
end
figure,imshow(rt),title('重取样后');
stack=[0 0];%保存起点
code=[];%保存链码
points=zeros(25,2);%保存端点
k=0;
t=rt;
%随便寻找起点
for i=1:m
for j=1:n
if t(i,j)==1
stack=[i j];
break;
end
end
end
s1=stack(1);
s2=stack(2)+1;
%while循环求链码
while (s1~=stack(1)||s2~=stack(2))&&k<500
k=k+1;
if k==1
s2=s2-1;
end
if s2+15<=141
if t(s1,s2+15)==1
code(k)=0;
points(k,1)=s1;
points(k,2)=s2;
t(s1,s2)=0;
s1=s1;s2=s2+15;
continue;
end
end
if s1-15>0&&s2+15<=141
if t(s1-15,s2+15)==1
code(k)=1;
points(k,1)=s1;
points(k,2)=s2;
t(s1,s2)=0;
s1=s1-15;
s2=s2+15;
continue;
end
end
if s1-15>0
if t(s1-15,s2)==1
code(k)=2;
points(k,1)=s1;
points(k,2)=s2;
t(s1,s2)=0;
s1=s1-15;s2=s2;
continue;
end
end
if s1-15>0&&s2-15>0
if t(s1-15,s2-15)==1
code(k)=3;
points(k,1)=s1;
points(k,2)=s2;
t(s1,s2)=0;
s1=s1-15;s2=s2-15;
continue;
end
end
if s2-15>0
if t(s1,s2-15)==1
code(k)=4;
points(k,1)=s1;
points(k,2)=s2;
t(s1,s2)=0;
s1=s1;s2=s2-15;
continue;
end
end
if s2-15>0
if t(s1+15,s2-15)==1
code(k)=5;
points(k,1)=s1;
points(k,2)=s2;
t(s1,s2)=0;
s1=s1+15;
s2=s2-15;
continue;
end
end
if t(s1+15,s2)==1
code(k)=6;
points(k,1)=s1;
points(k,2)=s2;
t(s1,s2)=0;
s1=s1+15;s2=s2;
continue;
end
if s2+15<=141
if t(s1+15,s2+15)==1
code(k)=7;
points(k,1)=s1;
points(k,2)=s2;
t(s1,s2)=0;
s1=s1+15;
s2=s2+15;
continue;
end
end
end
xt=rt-t;
figure,imshow(xt),title('相似多边形'),hold on;
for i=1:25
if i==25
plot([points(25,2);points(1,2)],[points(25,1);points(1,1)]);
else
plot(points(i:i+1,2),points(i:i+1,1));
end
end