Using a grayscale image to create an RGB image with matlab

image, image-processing, matlab

Solution

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]`. 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. This is why saveing a double precision `rgb_image` results with a completely white image.

Correction:

Use `uint8` type for `rgb_imge` as well:

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

Alternatively, you can convert `gray_image` to double

gray_image = im2double( gray_image );

Problem

I have a grayscale image and I want this image as the three bands of an RGB image. In other words, each band of the new RGB image will be this grayscale image 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?

Original source