三种图形如下
MATLAB代码如下
clc
clear
Image=imread('红绿圆.png');
R=Image(:,:,1); G=Image(:,:,2); B=Image(:,:,3);
figure(1);
subplot(2,2,1),imshow(Image),title('image')
subplot(2,2,2),imshow(R),title('r')%有红则显示白,没有则黑
subplot(2,2,3),imshow(G),title('g')%有绿则白,无则黑
subplot(2,2,4),imshow(B),title('b')%有蓝则白,无则黑
XYR=((R-G)>80&(R-B)>80);
XYG=((G-R)>80&(G-B)>80);
XYB=((B-R)>80&(B-G)>80);
XYY=((R-B)>200&(G-B)>200&(R-B)&(R-G)<30);
sumG=sum(sum(XYG));
sumR=sum(sum(XYR));
sumB=sum(sum(XYB));
sumY=sum(sum(XYY));
if(sumR>sumG&sumR>sumB)
disp('It is red!');
MY_Bit_img=XYR;
end
if(sumG>sumR&sumG>sumB)
disp('It is green!');
MY_Bit_img=XYG;
end
if(sumB>sumG&sumB>sumR)
disp('It is blue!');
MY_Bit_img=XYB;
end
if(sumY>sumR&sumY>sumB&sumY>sumG)
disp('It is yellow!');
MY_Bit_img=XYY;
end
sum_l=sum(MY_Bit_img);
sum_r=sum(MY_Bit_img,2);
len_l=max(sum_l);
len_r=max(sum_r);
len=(len_r+len_l)/2;
area=sum(sum_l);
circle=(3.14*len^2)/4;
square=len_l*len_r;
triangle=1.732*len_l*len_r/4;
a=[circle/area,square/area,triangle/area];
b=[1,1,1];
[value,pos]=min(abs(a-b));
if(pos==1)
disp('It is circle!');
elseif(pos==2)
disp('It is square!');
else
disp('It is triangle!');
end
效果图
红圆
蓝三角
绿方