%分离RGB图层
P=imread('D:\monarch.bmp'); %载入图片,需要处理的图片位置
figure;%figure函数用于设定图像显示窗口
%提取图像三通道信息
R1=P;G1=P;B1=P;
%R为1通道,G为2通道,B为3通道
R1(:,:,2)=0;R1(:,:,3)=0; %R1(:,:,2)=0是将第二个通道置零,这一行是将2,3通道置零
G1(:,:,1)=0;G1(:,:,3)=0;
B1(:,:,1)=0;B1(:,:,2)=0;
%保存图像,将分离后的图层分别保存在桌面
imwrite(R1,'C:\Users\Desktop\R1.png','png');
imwrite(G1,'C:\Users\Desktop\G1.png','png');
imwrite(B1,'C:\Users\Desktop\B1.png','png');
% 显示图像 Subplot(m,n,p)分成m*n个小窗口,在第p个窗口中创建坐标轴为当前坐标轴,用于显示图形。
threshold=0.2
subplot(1,3,1);imshow(R1,[]);title('R通道');
subplot(1,3,2);imshow(G1,[]);title('G通道');
subplot(1,3,3);imshow(B1,[]);title('B通道');
将分离后的RGB图层结合
%将RGB图层结合
R=imread('C:\Users\Desktop\R1.png'); %读取R图层
G=imread('C:\Users\Desktop\G1.png');
B=imread('C:\Users\Desktop\B1.png');
RGB1(:,:,1)=R(:,:,1); %1通道
RGB1(:,:,2)=G(:,:,2);
RGB1(:,:,3)=B(:,:,3);
imwrite(RGB1,'D:\RGB1.png');
imshow(RGB1)