Matlab HSV域区域色彩判定(可用于红绿灯检测)
(2011-05-10 20:33:47)
标签:
颜色识别颜色分类区域色彩判定matlabhsv |
%红绿等检测程序,基于HVS空间
%作者:ltx1215
%日期:2011-04-12
%主要功能描述:实现对红绿灯检测功能,将图像从RGB转换到HVS空间,
%避免了普通RGB空间易于受到亮度影响判定结果的弊端。
clear;
close all;
Image_f=imread('pic.jpg');
figure(1),imshow(Image_f);title('检测图像');
%RGB TO HSV
hsv_f = rgb2hsv(Image_f);
H = hsv_f(:,:,1)*255;
S = hsv_f(:,:,2)*255;
V = hsv_f(:,:,3)*255;
figure,imhist(uint8(H));
[y,x,z]=size(Image_f);
Red_y=zeros(y,1);
Green_y=zeros(y,1);
Yellow_y=zeros(y,1);
for i=1:y
for
j=1:x
if(((H(i,j)>=0)&&(H(i,j)<15))||((H(i,j)
>= 245) && (H(i,j)
<=255))
&&(V(i,j)>50)&&(S(i,j)>30))
%(V(i,j)<255)
&&(V(i,j)>50)&&(S(i,j)>30)
Red_y(i,1)= Red_y(i,1)+1;%红像素点统计
elseif(((H(i,j)>=66)&&(H(i,j)<130))
&&(V(i,j)>50)&&(S(i,j)>30))
%(V(i,j)<255)
&&(V(i,j)>50)&&(S(i,j)>30)
Green_y(i,1)= Green_y(i,1)+1;%绿像素点统计
elseif(((H(i,j)>=20)&&(H(i,j,1)<65))
&&(V(i,j)>50)&&(S(i,j)>30))
%(V(i,j)<255)
&&(V(i,j)>50)&&(S(i,j)>30)
Yellow_y(i,1)= Yellow_y(i,1)+1;%黄像素点统计
end
end
end
Max_Red_y=max(Red_y)
Max_Green_y=max(Green_y)
Max_Yellow_y=max(Yellow_y)
if((Max_Red_y> Max_Green_y)&&(Max_Red_y> Max_Yellow_y))
Result
=1;
elseif((Max_Green_y> Max_Red_y)&&(Max_Green_y> Max_Yellow_y))
Result
=2;
elseif((Max_Yellow_y> Max_Green_y)&&(Max_Yellow_y> Max_Red_y))
Result
=3;
else
Result
=4;
end
if(Result==1)
disp('检测结果为红灯');
elseif(Result==2);
disp('检测结果为绿灯');
elseif(Result==3)
disp('检测结果为黄灯');
else
disp('检测失败');
end
%作者:ltx1215
%日期:2011-04-12
%主要功能描述:实现对红绿灯检测功能,将图像从RGB转换到HVS空间,
%避免了普通RGB空间易于受到亮度影响判定结果的弊端。
clear;
close all;
Image_f=imread('pic.jpg');
figure(1),imshow(Image_f);title('检测图像');
%RGB TO HSV
hsv_f = rgb2hsv(Image_f);
H = hsv_f(:,:,1)*255;
S = hsv_f(:,:,2)*255;
V = hsv_f(:,:,3)*255;
figure,imhist(uint8(H));
[y,x,z]=size(Image_f);
Red_y=zeros(y,1);
Green_y=zeros(y,1);
Yellow_y=zeros(y,1);
Max_Red_y=max(Red_y)
Max_Green_y=max(Green_y)
Max_Yellow_y=max(Yellow_y)
if((Max_Red_y> Max_Green_y)&&(Max_Red_y> Max_Yellow_y))
elseif((Max_Green_y> Max_Red_y)&&(Max_Green_y> Max_Yellow_y))
elseif((Max_Yellow_y> Max_Green_y)&&(Max_Yellow_y> Max_Red_y))
else
end
if(Result==1)
elseif(Result==2);
elseif(Result==3)
else
end