简体   繁体   English

使用灰度图像用matlab创建RGB图像

[英]Using a grayscale image to create an RGB image with matlab

I have a grayscale image and I want this image as the three bands of an RGB image. 我有一个灰度图像,我希望这个图像作为RGB图像的三个波段。 In other words, each band of the new RGB image will be this grayscale image 换句话说,新RGB图像的每个带将是该灰度图像

That's what i tried so far: 那是我到目前为止所尝试的:

gray_image=imread('image.pgm');
rgb_image=zeros(size(gray_image,1),size(gray_image,2),3);
rgb_image(:,:,1)=gray_image;
rgb_image(:,:,2)=gray_image;
rgb_image(:,:,3)=gray_image;


>> rgb_image(1,2,1)

 ans = 44

>> rgb_image(1,2,2)

 ans = 44

>> rgb_image(1,2,3)

ans = 44

You can see that the above code works, but when I do the following command: 您可以看到上面的代码有效,但是当我执行以下命令时:

imwrite(rgb_image,'rgb_image.ppm');

the image is all white. 图像全是白色的。 How to save the image with the right format? 如何使用正确的格式保存图像?

Cause of problem: 问题原因:

gray_image is of type uint8 while rgb_image is of type double , but both has values in the range [0..255] . gray_image的类型为uint8rgb_image的类型为double ,但两者的值都在[0..255]范围内。 When matlab saves a double precision image it expects its values to be in range [0..1] all values greater than 1 are truncated to 1 - the brightest possible value. 当matlab保存double精度图像时,它希望其值在范围[0..1]所有大于1值都被截断为1 - 最亮的值。 This is why saveing a double precision rgb_image results with a completely white image. 这就是为什么用完全白色的图像rgb_image双精度rgb_image结果的原因。

Correction: 更正:

Use uint8 type for rgb_imge as well: 对于rgb_imge也使用uint8类型:

rgb_image=zeros(size(gray_image,1),size(gray_image,2),3,'uint8');

Alternatively, you can convert gray_image to double 或者,您可以将gray_image转换为double

gray_image = im2double( gray_image );

I managed to create a RGB image in Matlab 2016a where the original image has mostly gray colors 我设法在Matlab 2016a中创建了一个RGB图像,其中原始图像主要是灰色

imgGray = imread('Ceres-Bright-Spots-4_500-miles.jpg');  

[r g b]=size(imgGray);
rgb=zeros(r,g,3); 
rgb(:,:,1)=imgGray;
rgb(:,:,2)=rgb(:,:,1);
rgb(:,:,3)=rgb(:,:,1);
imgRGB=rgb/255; 
figure,imshow(imgRGB);

Doing Shai's commands were not sufficient. 做Shai的命令是不够的。 I prepared the data to complete all steps in the guide here about Image Processing Made Easy . 我准备的数据完成了指南中的所有步骤, 在这里关于图像处理变得容易

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM