问题1.背景知识:
Matlab读取的RGB模型为图像中每一个像素的RGB分量分配一个0~255范围内的强度值。例如:纯红色表示为(255,0,0),即R值为255,G值为0,B值为0;白色表示为(255,255,255);黑色表示为(0,0,0)。
代码整体功能:
程序结构:主要是循环结构+分支结构为主,当然还有顺序结构赋值;
代码:
clc,clear,close all;
a = imread('a.png');
subplot(2,3,1)
imshow(a)
title('原图')
a = double(a);
s = size(a);
b = a;
for i = 1:s(1)
for j = 1:s(2)
if a(i,j,1)<100 && a(i,j,3)>150
b(i,j,1)=255;
b(i,j,2)=0;
b(i,j,3)=0;
end
end
end
subplot(2,3,2)
imshow(uint8(b))
title('效果图1')
% a = imread('a.png');
% a = double(a);
% s = size(a);
% b = a;
for i = 1:s(1)
for j = 1:s(2)
if a(i,j,1)>100 && a(i,j,3)<100
b(i,j,1)=0;
b(i,j,2)=0;
b(i,j,3)=255;
end
end
end
subplot(2,3,3)
imshow(uint8(b))
title('效果图2')
% a = imread('a.png');
% a = double(a);
% s = size(a);
% b = a;
for i = 1:s(1)
for j = 1:s(2)
if a(i,j,1)<100 && a(i,j,2)>100 || a(i,j,1)>150 ...
&& a(i,j,2)<100 && a(i,j,3)>200
b(i,j,1)=255;
b(i,j,2)=150;
b(i,j,3)=0;
end
end
end
subplot(2,3,4)
imshow(uint8(b))
title('效果图3')
结果:【由于是截的题主的图,所以颜色分量有损,导致RGB颜色筛选和题目中的结果有差异】
问题2.
代码整体功能:同样是根据if语句的条件,将RGB分量进行分区域重新赋值,最后在坐标区内画了个三色圆环的效果(具体过程就不多说了,参考问题1自行理解完全可以读懂)
程序结构:依旧是循环结构+分支结构为主,当然还有顺序结构赋值;
代码:
clc,clear,close all;
a = imread('a.png');
subplot(2,3,1)
imshow(a)
title('原图')
a = double(a);
s = size(a);
b = a;
for i = 1:s(1)
for j = 1:s(2)
if a(i,j,1)<100 && a(i,j,3)>150
b(i,j,1)=255;
b(i,j,2)=0;
b(i,j,3)=0;
end
end
end
subplot(2,3,2)
imshow(uint8(b))
title('效果图1')
for i = 1:s(1)
for j = 1:s(2)
if a(i,j,1)>100 && a(i,j,3)<100
b(i,j,1)=0;
b(i,j,2)=0;
b(i,j,3)=255;
end
end
end
subplot(2,3,3)
imshow(uint8(b))
title('效果图2')
for i = 1:s(1)
for j = 1:s(2)
if a(i,j,1)<100 && a(i,j,2)>100 || a(i,j,1)>150 ...
&& a(i,j,2)<100 && a(i,j,3)>200
b(i,j,1)=255;
b(i,j,2)=150;
b(i,j,3)=0;
end
end
end
subplot(2,3,4)
imshow(uint8(b))
title('效果图3')
b=b/2;
x0=(s(1)+1)/2;
y0=(s(2)+1)/2;
l4=200;
l3=170;
l2=140;
l1=110;
for i = 1:s(1)
for j = 1:s(2)
l=((i-x0)^2+(j-y0)^2)^0.5;
if l<=14 && l>13
b(i,j,1)=(b(i,j,1)+50)*2;
elseif l<=13 && l>12
b(i,j,2)=(b(i,j,2)+50)*2;
elseif l<=l2 && l>l1
b(i,j,3)=(b(i,j,3)+50)*2;
else
b(i,j,:) = b(i,j,:)*2;
end
end
end
subplot(2,3,5)
imshow(uint8(b))
结果: