一、 灰度级彩色变换的思路是将灰度图像分别进行RGB三个分量的变换,将输出的三个分量再合成为一幅彩色图像。示意图如下
本文采用的变换方式是
源代码如下:
其中L的值的变换可得到不同颜色的图像
P=rgb2gray(imread('tes6.jpg'));
[h,w]=size(P);
R=zeros(h,w);
G=zeros(h,w);
B=zeros(h,w);
L=80;
for i=1:h
for j=1:w
if(P(i,j)>=0&&P(i,j)<L/2)
R(i,j)=0;
G(i,j)=4*P(i,j);
B(i,j)=L;
else if(P(i,j)>=L/2&&P(i,j)<3*L/4)
R(i,j)=4*P(i,j)-2*L;
G(i,j)=L;
B(i,j)=2*L-4*P(i,j);
else if(P(i,j)>=3*L/4&&P(i,j)<L)
R(i,j)=255;
G(i,j)=4*L-4*P(i,j);
B(i,j)=0;
end
end
end
end
end
imshow(cat(3,R,G,B));
P=rgb2gray(imread('tes6.jpg'));
[h,w]=size(P);
R=zeros(h,w);
G=zeros(h,w);
B=zeros(h,w);
for i=1:h
for j=1:w
if(P(i,j)>=0&&P(i,j)<96)
R(i,j)=0;
else if(P(i,j)>=96&&P(i,j)<128)
R(i,j)=255*(P(i,j)-96)/32;
else if(P(i,j)>=128&&P(i,j)<256)
R(i,j)=255;
end
end
end
end
end
for i=1:h
for j=1:w
if(P(i,j)>=0&&P(i,j)<32)
G(i,j)=0;
else if(P(i,j)>=32&&P(i,j)<64)
G(i,j)=255*(P(i,j)-32)/32;
else if(P(i,j)>=64&&P(i,j)<128)
G(i,j)=255;
else if(P(i,j)>=128&&P(i,j)<192)
G(i,j)=255*(192-P(i,j))/64;
else if(P(i,j)>=192&&P(i,j)<256)
G(i,j)=255*(P(i,j)-192)/64;
end
end
end
end
end
end
end
for i=1:h
for j=1:w
if(P(i,j)>=0&&P(i,j)<32)
B(i,j)=255*P(i,j)*32;
else if(P(i,j)>=32&&P(i,j)<64)
B(i,j)=255;
else if(P(i,j)>=64&&P(i,j)<96)
B(i,j)=255*(96-P(i,j))/32;
else if(P(i,j)>=96&&P(i,j)<192)
B(i,j)=0;
else if(P(i,j)>=192&&P(i,j)<256)
B(i,j)=255*(P(i,j)-192)/64;
end
end
end
end
end
end
end
imshow(cat(3,R,G,B));
P=rgb2gray(imread('tes6.jpg'));
[h,w]=size(P);
R=zeros(h,w);
G=zeros(h,w);
B=zeros(h,w);
for i=1:h
for j=1:w
if(P(i,j)>=0&&P(i,j)<64)
R(i,j)=0;
else if(P(i,j)>=64&&P(i,j)<128)
R(i,j)=255*(P(i,j)-64)/64;
else if(P(i,j)>=128&&P(i,j)<256)
R(i,j)=255;
end
end
end
end
end
for i=1:h
for j=1:w
if(P(i,j)>=0&&P(i,j)<128)
G(i,j)=0;
else if(P(i,j)>=128&&P(i,j)<192)
G(i,j)=255*(P(i,j)-64)/64;
else if(P(i,j)>=192&&P(i,j)<256)
G(i,j)=255;
end
end
end
end
end
for i=1:h
for j=1:w
if(P(i,j)>=0&&P(i,j)<64)
B(i,j)=255*P(i,j)*64;
else if(P(i,j)>=64&&P(i,j)<96)
B(i,j)=255;
else if(P(i,j)>=96&&P(i,j)<128)
B(i,j)=255*(128-P(i,j))/32;
else if(P(i,j)>=128&&P(i,j)<192)
B(i,j)=0;
else if(P(i,j)>=192&&P(i,j)<256)
B(i,j)=255*(P(i,j)-192)/64;
end
end
end
end
end
end
end
imshow(cat(3,R,G,B));