%生成 Red
for j=1:n
if w(i,j)<64
r(i,j)=255;
elseif w(i,j)>127
r(i,j)=0;
else
end
end
%生成 Green
for j=1:n
r(i,j)=(127-w(i,j))*4;
if w(i,j)<128
g(i,j)=0;
elseif w(i,j)>191
g(i,j)=255;
else
g(i,j)=(w(i,j)-127)*4;
end
end
%生成 Blue
for j=1:n
假彩色图像变换 MatLab 程序
clear;
w=imread('lung.jpg'); % 如果图像是一幅真彩色 RGB 图像
[m n]=size(w);
for i=1:m
if w(i,j)<64
b(i,j)=w(i,j)*4;
elseif w(i,j)>191
b(i,j)=255-(w(i,j)-191)*4;
b(i,j)=255;
else
end
end
end
subplot(2,3,1),imshow(w);
title('原始图像');
subplot(2,3,4),imshow(r);
title('红色通道');
subplot(2,3,5),imshow(g);
title('绿色通道');
subplot(2,3,6),imshow(b);
title('蓝色通道');
%合成为假彩色
p=uint8(zeros(m,n,3));
p(:,:,1)=uint8(r);
p(:,:,2)=uint8(g);
p(:,:,3)=uint8(b);
subplot(2,3,3),imshow(p);
title('假彩色图象');