从matlab中的3个3D矩阵转换为rgb图像

时间:2013-05-07 12:54:52

标签: image matlab rgb

我将rgb图像转换为灰度图像,然后我在3个3D矩阵中分别检索了R,G,B通道..有没有办法将这3个3D矩阵(R,G,B)连接到在matlab中获取单个RGB图像?

修改

示例代码(从comment添加):

I成为rgb图片

gr=rgb2gray(I); 
blank = zeros(size(gr),'uint8'); 
r = cat(3,gr,blank,blank); 
g = cat(3,blank,gr,blank); 
b = cat(3,blank,blank,gr); 
imshow(r); 
figure(2),imshow(g); 
figure(3),imshow(b);

2 个答案:

答案 0 :(得分:0)

将RGB图像转换为灰色图像(gr = rgb2gray(I))您丢失了大量信息 没有(微不足道的)方式从gr图片返回原始RGB I
您创建的图片r gbgr外没有“额外”信息,以帮助您恢复原始I

你可以做的是

r = 0*I;
r(:,:,1) = I(:,:,1); % take only red channel to r
g = 0*I;
g(:,:,2) = I(:,:,2);
b = 0*I;
b(:,:,3) = I(:,:,3);
figure;
subplot(131);imshow(r);title('red channel');
subplot(132);imshow(g);title('green channel');
subplot(132);imshow(b);title('blue channel');

% recovering I
recover = cat(3, r(:,:,1), g(:,:,2), b(:,:,3) );
figure;
imshow( recover ); title('recovered RGB image');

答案 1 :(得分:0)

由于Matlab的rgb2gray使用rgb2ntsc来提取灰度信息(source),你可以使用rgb2ntsc而不是rgb2gray并将灰度图像分开,以某种方式存储其他两个通道以便以后用于重建(使用ntsc2rgb)

I2 = rgb2ntsc(I);  %I2 and I are both n x m x 3 images
gr = I2(:,:,1); % now gr is your n x m grayscale image
notgr = I2(:,:,2:3); % these are required to reconstruct image later