Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
756 views
in Technique[技术] by (71.8m points)

image - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To convert a grayscale image to an RGB image, there are two issues you have to address:

  • 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.
  • 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.

Here are 3 typical conditions you might encounter:

  • 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:

    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:

    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:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...