实现如下:
image='ste_cover.bmp';
ste_cover=imread(image);
[row,col]=size(ste_cover);
%计算穿过平面簇p0_num,p1_num总次数。
p0_num=0;
p1_num=0;
min_num=0;
max_num=0;
temp0=0;
temp1=0;
for i=1:row
for j=1:col
if((i<row)&&(j<col))
%计算行方向的值
min_num=min(ste_cover(i,j),ste_cover(i,j+1));
max_num=max(ste_cover(i,j),ste_cover(i,j+1));
%判断更小的值是奇数还是偶数
if(mod(min_num,2)==0)
%为奇数时
%计算p0_num
temp0=min_num+1.5;
while temp0<max_num
p0_num=p0_num+1;
temp0=temp0+2;
end
%计算p1_num
temp1=min_num+0.5;
while temp1<max_num
p1_num=p1_num+1;
temp1=temp1+2;
end
else
%为偶数时
temp0=min_num+0.5;
while temp0<max_num
p0_num=p0_num+1;
temp0=temp0+2;
end
%计算p1_num
temp1=min_num+1.5;
while temp1<max_num
p1_num=p1_num+1;
temp1=temp1+2;
end
end
%计算列方向的值
min_num=min(ste_cover(i,j),ste_cover(i+1,j));
max_num=max(ste_cover(i,j),ste_cover(i+1,j));
%判断更小的值是奇数还是偶数
if(mod(min_num,2)==0)
%为奇数时
%计算p0_num
temp0=min_num+1.5;
while temp0<max_num
p0_num=p0_num+1;
temp0=temp0+2;
end
%计算p1_num
temp1=min_num+0.5;
while temp1<max_num
p1_num=p1_num+1;
temp1=temp1+2;
end
else
%为偶数时
%计算p0_num
temp0=min_num+0.5;
while temp0<max_num
p0_num=p0_num+1;
temp0=temp0+2;
end
%计算p1_num
temp1=min_num+1.5;
while temp1<max_num
p1_num=p1_num+1;
temp1=temp1+2;
end
end
else
if((j==col)&&(i<row))
%计算列方向的值
min_num=min(ste_cover(i,j),ste_cover(i+1,j));
max_num=max(ste_cover(i,j),ste_cover(i+1,j));
%判断更小的值是奇数还是偶数
if(mod(min_num,2)==0)
%为奇数时
%计算p0_num
temp0=min_num+1.5;
while temp0<max_num
p0_num=p0_num+1;
temp0=temp0+2;
end
%计算p1_num
temp1=min_num+0.5;
while temp1<max_num
p1_num=p1_num+1;
temp1=temp1+2;
end
else
%偶数的时候干的活
%计算p0_num
temp0=min_num+0.5;
while temp0<max_num
p0_num=p0_num+1;
temp0=temp0+2;
end
%计算p1_num
temp1=min_num+1.5;
while temp1<max_num
p1_num=p1_num+1;
temp1=temp1+2;
end
end
end
if((i==row)&&(j<col))
%计算行方向的值
min_num=min(ste_cover(i,j),ste_cover(i,j+1));
max_num=max(ste_cover(i,j),ste_cover(i,j+1));
%判断更小的值是奇数还是偶数
if(mod(min_num,2)==0)
%为奇数时
%计算p0_num
temp0=min_num+1.5;
while temp0<max_num
p0_num=p0_num+1;
temp0=temp0+2;
end
%计算p1_num
temp1=min_num+0.5;
while temp1<max_num
p1_num=p1_num+1;
temp1=temp1+2;
end
else
%为偶数时
temp0=min_num+0.5;
while temp0<max_num
p0_num=p0_num+1;
temp0=temp0+2;
end
%计算p1_num
temp1=min_num+1.5;
while temp1<max_num
p1_num=p1_num+1;
temp1=temp1+2;
end
end
end
end
end
end
R=p1_num/p0_num;
fprintf('R=%f\n',R);
网友评论