欢迎您的到来!
今天学习了利用matlab对一张图片的颜色主体进行判断并输出颜色
这里就不具体介绍了,若想了解可以翻看我前面的博文
点击查看前文
i=size(picture_2,1); %i为横轴
j=size(picture_2,2) %j为纵轴
(1)想要判断图片的颜色首先就要了解rgb的组成点击查看RGB颜色对照表
(2)表示出图片(24位位图)的RGB
picture_2(i,j,1); %表示坐标系中(i,j)点的R值
picture_2(i,j,2) ;%表示坐标系中(i,j)点的G值
picture_2(i,j,3) ;%表示坐标系中(i,j)点的B值
picture = 'D:\picture3\2.jpeg';
picture=imread(picture);
picture_1=picture(:,:,1);
picture_2=picture(:,:,2);
picture_3=picture(:,:,3);
subplot(2,2,1);
imshow(picture);
title('RGB');
subplot(2,2,2);
imshow(picture_1);
title('R');
subplot(2,2,3);
imshow(picture_2);
title('G');
subplot(2,2,4);
imshow(picture_3);
title('B');
for i = 1:size(picture_2,1)
for j = 1:size(picture_2,2)
..... %处理程序
end
end
if picture_2(i,j,1)>=200&&picture_2(i,j,2)<=50&&picture_2(i,j,3)<=50 %这里的200 和50可以根据具体情况进行设置
red=red+1;
elseif picture_2(i,j,1)>=200&&picture_2(i,j,2)>=200&&picture_2(i,j,3)<=50
yellow=yellow+1;
elseif picture_2(i,j,2)>=200&&picture_2(i,j,1)<=100&&picture_2(i,j,3)<=100
green=green+1;
end
if max(max(red,yellow),green)==red
color = 'red'
elseif max(max(red,yellow),green)==yellow
color='yellow'
elseif max(max(red,yellow),green)==green
color='green'
end
save_path='D:\picture2\'; %获取图片所在位置
img_path_list = dir(strcat(save_path,'*.jpg')); %依次仅读取文件夹中的.jpg格式图片
img_num=length(img_path_list); %判断图片个数
for i=1:img_num
picture_name = img_path_list(i).name; %获取图片的名称
picture_1 = imread(strcat(save_path,picture_name)); %建立循环函数依次读取原始图像
imshow(picture_1);
[x,y]=ginput(2);
picture_2 = imcrop(picture_1,[x(1),y(1),abs(x(1)-x(2)),abs(y(1)-y(2))]);
imwrite(picture_2,[num2str(i),'.jpg']);
detection_color(picture_2);
end
function jianche = detection_color(picture_2)
red=0;
yellow=0;
green=0;
for i = 1:size(picture_2,1)
for j = 1:size(picture_2,2)
if picture_2(i,j,1)>=200&&picture_2(i,j,2)<=50&&picture_2(i,j,3)<=50
red=red+1;
elseif picture_2(i,j,1)>=200&&picture_2(i,j,2)>=200&&picture_2(i,j,3)<=50
yellow=yellow+1;
elseif picture_2(i,j,2)>=200&&picture_2(i,j,1)<=100&&picture_2(i,j,3)<=100
green=green+1;
end
end
end
if max(max(red,yellow),green)==red
color = 'red'
elseif max(max(red,yellow),green)==yellow
color='yellow'
elseif max(max(red,yellow),green)==green
color='green'
end