我有一个灰度图像,我希望这个图像作为RGB图像的三个波段。 换句话说,新RGB图像的每个带将是该灰度图像
那是我到目前为止所尝试的:
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您可以看到上面的代码有效,但是当我执行以下命令时:
imwrite(rgb_image,'rgb_image.ppm');图像全是白色的。 如何使用正确的格式保存图像?
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 = 44You 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?
最满意答案
问题原因:
gray_image的类型为uint8而rgb_image的类型为double ,但两者的值都在[0..255]范围内。 当matlab保存double精度图像时,它期望其值在[0..1]范围内,所有大于1值都被截断为1 - 最亮的值。 这就是为什么用完全白色的图像rgb_image双精度rgb_image结果的原因。
更正:
对于rgb_imge也使用uint8类型:
rgb_image=zeros(size(gray_image,1),size(gray_image,2),3,'uint8');或者,您可以将gray_image转换为double
gray_image = im2double( gray_image );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 );更多推荐
发布评论