根据HSV查找颜色分量

%因为要查找红橙蓝三种颜色的物体,所以编写了一下程序,以HSV颜色空间定位

%查找红橙黄分量

%all add
close all
clear
img=imread('DSC2.jpg');
image=imresize(img,0.5);
figure,imshow(image);title('test1');
hsv=rgb2hsv(image);
h=hsv(:,:,1);
s=hsv(:,:,2);
v=hsv(:,:,3);
[x,y]=size(h);
BW_R(x,y)=0;
BW_Y(x,y)=0;
BW_B(x,y)=0;
%  红R0,(330,22)-(0.9167,0.0611)
%  橙O1,[22,45]-[0.0611,0.1250]
%  黄Y2,(45,70)-(0.1250,0.1944)
%  绿G3,[70,155]-[0.1944,0.4306]
%  青I4,(155,180)-(0.4306,0.5)
%  蓝B5,[180,272]-[0.5,0.7556]
%  紫P6,(272,330]-[0.7556.0.9167]
for i=1:x
    for j=1:y
        if((h(i,j)>0.9167)|(h(i,j)<0.0611)&(s(i,j)>=0.6))
          BW_R(i,j)=1;
        elseif((h(i,j)>0.5)&(h(i,j)<0.7556)&(s(i,j)>=0.6))
             BW_B(i,j)=1;
        elseif((h(i,j)>0.1250)&(h(i,j)<0.1944)&(s(i,j)>=0.6))
             BW_Y(i,j)=1;
        end
    end
end
figure,imshow(BW_R);
 figure,imshow(BW_Y);
 figure,imshow(BW_B);

你可能感兴趣的:(根据HSV查找颜色分量)