简体   繁体   English

如何在MATLAB中将灰度矩阵转换为RGB矩阵?

[英]How to convert a grayscale matrix to an RGB matrix in MATLAB?

rgbImage = grayImage / max(max(grayImage));

or 要么

rgbImage = grayImage / 255;

Which of the above is right,and reason? 以上哪项是正确的,还有原因?

To convert a grayscale image to an RGB image , there are two issues you have to address: 要将灰度图像转换为RGB图像 ,您必须解决两个问题:

  • Grayscale images are 2-D, while RGB images are 3-D, so you have to replicate the grayscale image data three times and concatenate the three copies along a third dimension. 灰度图像是2- d,而RGB图像是3-d,所以你必须复制灰度图像数据三次并连接所有的三个拷贝沿第三维度。
  • Image data can be stored in many different data types , so you have to convert them accordingly. 图像数据可以存储在许多不同的数据类型中 ,因此您必须相应地转换它们。 When stored as a double data type, the image pixel values should be floating point numbers in the range of 0 to 1. When stored as a uint8 data type, the image pixel values should be integers in the range of 0 to 255. You can check the data type of an image matrix using the function class . 当存储为double数据类型时,图像像素值应为0到1范围内的浮点数。当存储为uint8数据类型时,图像像素值应为0到255范围内的整数。您可以使用函数class检查图像矩阵的数据类型。

Here are 3 typical conditions you might encounter: 以下是您可能遇到的3种典型情况:

  • To convert a uint8 or double grayscale image to an RGB image of the same data type , you can use the functions repmat or cat : 要将uint8double灰度图像转换为相同数据类型的RGB图像,可以使用repmatcat函数:

     rgbImage = repmat(grayImage,[1 1 3]); rgbImage = cat(3,grayImage,grayImage,grayImage); 
  • To convert a uint8 grayscale image to a double RGB image, you should convert to double first, then scale by 255: 要将uint8灰度图像转换为double RGB图像,您应首先转换为double ,然后再转换为255:

     rgbImage = repmat(double(grayImage)./255,[1 1 3]); 
  • To convert a double grayscale image to a uint8 RGB image, you should scale by 255 first, then convert to uint8 : 要将double灰度图像转换为uint8 RGB图像,首先应缩放255,然后转换为uint8

     rgbImage = repmat(uint8(255.*grayImage),[1 1 3]); 

By definition, an RGB image has 3 channels, which implies you need a three-dimensional matrix to represent the image. 根据定义,RGB图像有3个通道,这意味着您需要一个三维矩阵来表示图像。 So, the right answer is: 所以,正确的答案是:

rgbImage = repmat(255*grayImage/max(grayImage(:)),[1 1 3]);

Be careful when normalizing grayImage . 规范化grayImage时要小心。 If grayImage is uint8 then you will lose some precision in the 255*grayImage/max(grayImage(:)) operation. 如果grayImageuint8那么你将在255*grayImage/max(grayImage(:))操作中失去一些精度。

Also, normalizing grayImage depends on the data. 此外,规范化grayImage取决于数据。 In your question, you used two methods: 在您的问题中,您使用了两种方法:

rgbImage = grayImage / max(max(grayImage));

which normalizes the grayscale image such that the maximum value in the image is 1 and 它使灰度图像标准化,使得图像中的最大值为1

rgbImage = grayImage / 255;

which only makes sense if the values in grayImage lie in the 0-255 range. 只有在grayImage中的值位于0-255范围内时才有意义。

So it really depends on what you want to do. 所以这真的取决于你想做什么。 But, if you want an RGB image you need to convert your single-channel matrix to a 3-channel matrix. 但是,如果您想要RGB图像,则需要将单通道矩阵转换为3通道矩阵。

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

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