matlab计算图像中不同颜色所占面积原理很简单,就是数像素点的个数,我举个例子,现在将我之前画青天白日旗的程序修改一下:
x0=[3.6,3.6,-3.6,-3.6];
y0=[2.1,-2.1,-2.1,2.1];
fill(x0,y0,'k')
hold on
theta=[pi/2:pi/12:2.5*pi];
le=1.8;
r=[le,1,le,1,le,1,le,1,le,1,le,1,le,1,le,1,le,1,le,1,le,1,le,1,le];
[x,y]=pol2cart(theta,r);
%画12道叉光
fill(x,y,'r')
plot(x,y,'r')
hold on
theta1=[pi/2:pi/48:2.5*pi];
x1=cos(theta1);y1=sin(theta1);
fill(x1,y1,'b')
plot(x1,y1,'b')
axis equal
axis off
print('-dpng','-r500',1) %保存图像
注意图像保存后是有白色的空余,但没关系

统计像素点程序如下:
X=imread('1.png');
[a,b,c]=size(X);
red=0;
blue=0;
black=0;
white=0;
for i=1:a
for
j=1:b
if sum(X(i,j,:))==0
black=black+1;
elseif sum(X(i,j,:))==255&X(i,j,1)==255
red=red+1;
elseif sum(X(i,j,:))==255&X(i,j,3)==255
blue=blue+1;
else
white=white+1;
end
end
end
total=red+white+blue+black
s1=blue/(total-white)
s2=pi/(4.2*7.2)
算出来s1=0.1042,而理论值s2=0.1039,非常接近,误差是可以接受的,毕竟画的圆并不是真正意义上的圆,而是接近圆形的多边形。
ps.程序参考了http://www.yelinsky.com/blog/archives/301.html
加载中,请稍候......