clear
close all;
%------读入图片flower.jpg，存在A------%
A=imread('E:\work\flower.jpg');
subplot(2,2,1)
imshow(A);
title('原始图片')
[r c d]=size(A);  %计算图像大小,r位行,c是列
%------计算红色分量并显示分解图------%
red(:,:,1)=A(:,:,1);
red(:,:,2)=zeros(r,c); %蓝色跟绿色用零矩阵填充
red(:,:,3)=zeros(r,c);
red=uint8(red);
subplot(2,2,2)
imshow(red)
title('红色分量');
%-------计算绿色分量并显示分解图-------%
green(:,:,2)=A(:,:,2);
green(:,:,1)=zeros(r,c);
green(:,:,3)=zeros(r,c);
green=uint8(green);
subplot(2,2,3)
imshow(green)
title('绿色分量');
%--------计算蓝色分量并显示分解图-------%
blue(:,:,3)=A(:,:,3);
blue(:,:,1)=zeros(r,c);
blue(:,:,2)=zeros(r,c);
blue=uint8(blue);
subplot(2,2,4)
imshow(blue)
title('蓝色分量'); 
%------------合成彩色图像-----------%
ci=cat(3,red(:,:,1),green(:,:,2),blue(:,:,3));
figure;
subplot(1,2,1)
imshow(A);
title('原始图片')
subplot(1,2,2)
imshow(ci);
title('合成彩色图像');