rgbImage = grayImage / max(max(grayImage));
or
rgbImage = grayImage / 255;
Which of the above is right,and reason?
double
数据类型存储时,图像像素值应为 0 到 1 范围内的浮点数。作为uint8
数据类型存储时,图像像素值应为 0 到 255 范围内的整数。您可以查看使用函数的图像矩阵的数据类型class
。以下是您可能会遇到的 3 种典型情况:
要将灰度图像uint8
或double
灰度图像转换为相同数据类型的 RGB 图像,可以使用函数repmat
或cat
:
rgbImage = repmat(grayImage,[1 1 3]);
rgbImage = cat(3,grayImage,grayImage,grayImage);
要将uint8
灰度图像转换为double
RGB 图像,应double
先转换为,然后按 255 缩放:
rgbImage = repmat(double(grayImage)./255,[1 1 3]);
要将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. 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.