In the Image Processing Toolbox, MATLAB provides

rgb2gray

to convert a 3 channel color image into a 1 channel intensity (grayscale image). But not the other way around. Sometimes it is useful to have an intensity image represented in the 3 channel colour format.

Write a function to convert an input grayscale image into an equivalent RGB image, ensuring that the datatype of the input image matrix is preserved.

Example:

grayImg = [1 2 3; 4 5 6; 7 8 9];
rgbImg = gray2rgb(grayImg);
rgbImg(:,:,1) = [1 2 3; 4 5 6; 7 8 9];
rgbImg(:,:,2) = [1 2 3; 4 5 6; 7 8 9];
rgbImg(:,:,3) = [1 2 3; 4 5 6; 7 8 9];

Solution Stats

58 Solutions

35 Solvers

Last Solution submitted on Jun 24, 2025

Last 200 Solutions

Problem Comments

Solution Comments

Show comments
Loading...

Problem Recent Solvers35

Suggested Problems

Problem Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!