求出叶子边界周长 求出8方向链码 计算周长CI
求出叶子边界周长 求出8方向链码 计算周长CIR
clc;
clear all;
close all;
I=imread('D:\我的文档\MATLAB\yezi.jpg'); I=im2bw(I);
[m,n]=size(I); %计算图像的大小
figure,imshow(I);
R=1; %二值图像的背景像素
I0=zeros(m+2,n+2)+R; %原图像+边框,避免边界查找出错 I0(2:(m+1),2:(n+1))=I;
x0=0;
y0=0;
fsp=0; %是否找到起始点
%%%%%%%%%%%%寻找目标区域的一个边界点%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:m
for j=1:n
if (I0(i,j)~=R)
x0=i; %记录起始点的纵坐标i
y0=j; %记录起始点的横坐标j
fsp=1;
break;
end
end
if fsp==1
break;
end
end
row=x0;
col=y0;
%%%%%%%%%%%%%%%%%%%%按某一方向查找边界坐标并且记录%%%%%%%%%%%%%%%%%%%%%%
A=[]; %存储边界点坐标的矩阵
B=zeros(m,n); %边界跟踪的图像矩阵
ended=0;
direction=4; %由起始点的左边开始查找 while(ended==0)
found_next=0;
b=[row-1,col-1]; %消除边框,与原图保持一致
A=[A;b];
B(row-1,col-1)=255;
while(found_next==0)
switch mod(direction,8)
case 0
if (I0(row, col+1)~=R)
row=row; %记录当前坐标
col=col+1;
direction=5; %下一点由方向5开始查找
found_next=1; %找到边界点,准备跳出循环
end;
case 1
if (I0(row-1, col+1)~=R)
row=row-1; %记录当前坐标
col=col+1;
direction=6; %下一点由方向6开始查找
found_next=1; %找到边界点
end;
case 2
if (I0(row-1, col)~=R)
row=row-1; %记录当前坐标
col=col;
direction=7; %下一点由方向7开始查找
found_next=1; %找到边界点
end;
case 3
if (I0(row-1, col-1)~=R)
row=row-1; %记录当前坐标
col=col-1;
direction=0; %下一点由方向0开始查找
found_next=1; %找到边界点
end;
case 4
if (I0(row, col-1)~=R)
row=row; %记录当前坐标
col=col-1;
direction=1; %下一点由方向1开始查找
found_next=1; %找到边界点
end;
case 5
if (I0(row+1, col-1)~=R)
row=row+1; %记录当前坐标
col=col-1;
direction=2; %下一点由方向2开始查找
found_next=1; %找到边界点
end;
case 6
if (I0(row+1, col)~=R)
row=row+1; %记录当前坐标
col=col;
direction=3; %下一点由方向3开始查找
found_next=1; %找到边界点
end;
case 7
if (I0(row+1, col+1)~=R)
row=row+1; %记录当前坐标
col=col+1;
direction=4; %下一点由方向4开始查找
found_next=1; %找到边界点
end
end
if (found_next==0)
direction=direction+1; %未找到边界点,继续在下一方向查找
end
end
if(and((x0==row),(y0==col)))
ended=1; %是否与起始点相同
end
end
A1=[A;[x0-1,y0-1]];
figure,imshow(B); %输出跟踪结果 hold on
%%%%%%%%%%%%%%%%%%%%求出8方向链码%%%%%%%%%%%%%%%%%%%%%%%%%% C=[];
for i=1:length(A1)-1
if(A1(i+1,1)-A1(i,1)==0)&&(A1(i+1,2)-A1(i,2)==1)
code=0;
C=[C,code];
end
if(A1(i+1,1)-A1(i,1)==-1)&&(A1(i+1,2)-A1(i,2)==1)
code=1;
C=[C,code];
end
if(A1(i+1,1)-A1(i,1)==-1)&&(A1(i+1,2)-A1(i,2)==0)
code=2;
C=[C,code];
end
if(A1(i+1,1)-A1(i,1)==-1)&&(A1(i+1,2)-A1(i,2)==-1)
code=3;
C=[C,code];
end
if(A1(i+1,1)-A1(i,1)==0)&&(A1(i+1,2)-A1(i,2)==-1)
code=4;
C=[C,code];
end
if(A1(i+1,1)-A1(i,1)==1)&&(A1(i+1,2)-A1(i,2)==-1)
code=5;
C=[C,code];
end
if(A1(i+1,1)-A1(i,1)==1)&&(A1(i+1,2)-A1(i,2)==0)
code=6;
C=[C,code];
end
if(A1(i+1,1)-A1(i,1)==1)&&(A1(i+1,2)-A1(i,2)==1)
code=7;
C=[C,code];
end
end
%%%%%%%%%%%%%%%%%%%计算周长CIR%%%%%%%%%%%%%%%%%%%%%%
sum1=0;
sum2=0;
for i=1:length(C)
if mod(C(1,i),2)==0
sum1=sum1+1;
end
if mod(C(1,i),2)==1
sum2=sum2+1;
end
end
CIR=sum1+sum2*sqrt(2);%求出边界周长
%%%%%%%%%%%%%%%%%%%%%%%%求外观比R%%%%%%%%%%%%%%%%%%%%%%%%%%%%
L=max(A(:,1))-min(A(:,1));
W=max(A(:,2))-min(A(:,2));
R=L/W;
%%%%%%%%%%%%%%%%%%%%%%%形状因子F%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
AREA=0;
x_point=0;
y_point=0;
for i=1:m
for j=1:n
if(I(i,j)==0)
AREA=AREA+1;
x_point=x_point+i;
y_point=y_point+j;
end
end
end
F=CIR^2/(4*pi*AREA);
x_average=x_point/AREA;
y_average=y_point/AREA;
%%%%%%%%%%%%%%%%%%%%%%%偏心率E%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
STATS=regionprops(I,'Eccentricity'); d1=regionprops(I,'MajorAxisLength'); d2=regionprops(I,'MinorAxisLength'); a=d1.MajorAxisLength;
b=d2.MinorAxisLength;
E=a/b;
%%%%%%%%%%%%%%%%%%%%球状性S%%%%%%%%%%%%%%%%%%%%%%%%%%%%% dirmax=max(sqrt((A(:,1)-x_average).^2+(A(:,2)-y_average).^2)); dirmin=min(sqrt((A(:,1)-x_average).^2+(A(:,2)-y_average).^2)); S=dirmin/dirmax;
%%%%%%%%%%%%%%%%%%%%%%%圆形性Cxy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% [k,l]=size(A);
sum_i=0; %记录边界点纵坐标相加的和
sum_j=0; %记录边界点横坐标相加的和
for i=1:k
sum_i=sum_i+sqrt((A(i,1)-x_average)^2+(A(i,2)-y_average)^2); end
UR=sum_i/k; %图像中心的纵坐标
for i=1:k
sum_j=sum_j+(sqrt((A(i,1)-x_average)^2+(A(i,2)-y_average)^2)-UR)^2; end
TR=sqrt(sum_j/k); %图像中心的横坐标
Cxy=UR/TR;
display(Cxy);
display(S);
display(E);
display(F);
display(R);
display(CIR);
display(C);
输出结果:
Cxy= E= R=
5.3186 2.0232 1.7931
S= F= CIR=
0.5428 1.5297 402.0315
8方向链码
C=
Columns 1 through 14 21212222122122
67667760767707
55456556565565 Columns 239 through 252
Columns 127 through 140
Columns 15 through 28 22222221231222
67770777070077
65565665655656 Columns 253 through 266
Columns 141 through 154
Columns 29 through 42 22222222231232
07000700007777
55655655656565 Columns 267 through 280
Columns 155 through 168
Columns 43 through 56 23223232232332
67767766766766
55656565566655 Columns 281 through 294
Columns 169 through 182
Columns 57 through 70 22333223332332
66702122221222
66566655666566 Columns 295 through 308
Columns 183 through 196
Columns 71 through 84 33343323333334
22122122122100
66666576665667 Columns 309 through 322
Columns 197 through 210
Columns 85 through 98 33333233232333
10111011110112
56676666766667 Columns 323 through 336
Columns 211 through 224
Columns 99 through 112 23323332332322
10112121122121
66767666766677 Column 337
Columns 225 through 238
Columns 113 through 126 4