使用灰度图像用matlab创建RGB图像(Using a grayscale image to create an RGB image with matlab)

系统教程 行业动态 更新时间:2024-06-14 16:58:30
使用灰度图像用matlab创建RGB图像(Using a grayscale image to create an RGB image with matlab)

我有一个灰度图像,我希望这个图像作为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 = 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?

最满意答案

问题原因:

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 );

更多推荐

本文发布于:2023-04-15 03:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/4d9ec001b70f244e88b6ac696ac33f4a.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   灰度   matlab   RGB   image

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!