
numVal_1 = sum(sum(bw))
连用两次sum是将图像中数值的行和列分别加在一起,就可求出像素为1的总数。
要求数值为“0”的像素数(设变量“numVal_0”)可以这样:
numVal_0 = length(find(bw==0))
其中“find(bw==0)”输出所有bw为0的像素序号,“length”函数求序号的长度,也就是所求的数值为“0”的像素个数;
比例:
numVal_1/(numVal_1+numVal_0)
或者:
numVal_1/(size(bw,1)*size(bw,2))
其中“size(bw,1)”求bw中的行数,“size(bw,2)”求bw中的列数。
把下面的程序保存为plotkoch.m,然后在command windows中输入plotkoch(8)即可,其中8是迭代次数,你可以换成别的整数。function plotkoch(n)
[x,y]=koch(n)
fill(x,y,'w')
text(0,0,{'\centerline{The area surrounded by Koch Snow Curve is $\displaystyle\frac{2\sqrt {3}{n}^{2}}{5}$.}',...
'\centerline{($n$ is the side length of the original triangle.)}',...
'\centerline{The dimension of the curve is $\log{_34}$.}'},...
'interpreter','latex','horizontalalignment','center')
axis equal off
function [x,y]=koch(n)
if n==1
t=linspace(0,2*pi,4)
x=cos(t)
y=sin(t)
else
p=1/3h=p*sqrt(3)/2
[x,y]=koch(n-1)
xx=x(1)yy=y(1)
dx=diff(x)dy=diff(y)
ax=(1-p)*x(1:end-1)+p*x(2:end)
ay=(1-p)*y(1:end-1)+p*y(2:end)
bx=p*x(1:end-1)+(1-p)*x(2:end)
by=p*y(1:end-1)+(1-p)*y(2:end)
ox=(x(1:end-1)+x(2:end))/2+h*dy
oy=(y(1:end-1)+y(2:end))/2-h*dx
x=[reshape([x(1:end-1)axoxbx],1,[]),x(end)]
y=[reshape([y(1:end-1)ayoyby],1,[]),y(end)]
end
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)