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

or

rgbImage = grayImage / 255;

Which of the above is right,and reason?

4

2 回答 2

23

要将灰度图像转换为RGB 图像,您必须解决两个问题:

  • 灰度图像是2-D,而RGB 图像是 3-D,因此您必须将灰度图像数据复制三次,然后将三个副本沿三维连接。
  • 图像数据可以存储在许多不同的数据类型中,因此您必须对它们进行相应的转换。作为double数据类型存储时,图像像素值应为 0 到 1 范围内的浮点数。作为uint8数据类型存储时,图像像素值应为 0 到 255 范围内的整数。您可以查看使用函数的图像矩阵的数据类型class

以下是您可能会遇到的 3 种典型情况:

  • 要将灰度图像uint8double灰度图像转换为相同数据类型的 RGB 图像,可以使用函数repmatcat

    rgbImage = repmat(grayImage,[1 1 3]);
    rgbImage = cat(3,grayImage,grayImage,grayImage);
    
  • 要将uint8灰度图像转换为doubleRGB 图像,应double先转换为,然后按 255 缩放:

    rgbImage = repmat(double(grayImage)./255,[1 1 3]);
    
  • 要将double灰度图像转换为uint8RGB 图像,应先缩放 255,然后转换为uint8

    rgbImage = repmat(uint8(255.*grayImage),[1 1 3]);
    
于 2010-04-12T12:41:35.857 回答
2

By definition, an RGB image has 3 channels, which implies you need a three-dimensional matrix to represent the image. So, the right answer is:

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

Be careful when normalizing grayImage. If grayImage is uint8 then you will lose some precision in the 255*grayImage/max(grayImage(:)) operation.

Also, normalizing grayImage depends on the data. 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

rgbImage = grayImage / 255;

which only makes sense if the values in grayImage lie in the 0-255 range.

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.

于 2010-04-12T03:21:59.423 回答