converting gray to rgb - problem

3 views (last 30 days)
Ahmad
Ahmad on 10 Apr 2011
Commented: Image Analyst on 11 Apr 2015
I have a picture in Gray scale ones and zeros
and for some multiplications characteristics to apply functions of convolution i will need to convert it to rgb with a matrix that consist of 3
how can i do gray2rgb? I've tried some defining new function for this that i took from the shared files in this website but didn't work.
mentioning that my version is (MATLAB 7.6.0 r2008a)
I get this message: Function definitions are not permitted at the prompt or in scripts.
Whenever I define this function:
MATLAB code
Image=imread('input.bmp')
function [Image]=gray2rgb(Image)
%Gives a grayscale image an extra dimension
%in order to use color within it
[m n]=size(Image);
rgb=zeros(m,n,3);
rgb(:,:,1)=Image;
rgb(:,:,2)=rgb(:,:,1);
rgb(:,:,3)=rgb(:,:,1);
Image=rgb/255;
end
  4 Comments
Durgesh Naik
Durgesh Naik on 11 Apr 2015
use tool in matlab gray to RGB
Image Analyst
Image Analyst on 11 Apr 2015
There is a function ind2rgb() but that requires a colormap, which he does not have, so that function won't work. Other than that, the function to use is cat() like Walter and I already recommended below.

Sign in to comment.

Answers (3)

Walter Roberson
Walter Roberson on 10 Apr 2011
gray2rgb = @(Image) double(cat(3,Image,Image,Image))./255;

Image Analyst
Image Analyst on 3 Nov 2012
For a floating point RGB image in the range 0.0 - 1.0, which will let it be seen with imshow(), you can do what Walter suggests. You can't view an RGB image as floating point unless it's in the range 0-1. You can convert it to uint8 but you lose precision of up to 1/2 a gray level. If you want a uint8 image that has the same range of values as your convolved/manipulated image has then just concatenate and convert to uint8, which will round:
rgbUint8 = uint8(cat(3, singleImage, singleImage, singleImage));
or it could be a double image instead of a single image, just use whatever you have. This will allow you to view it with imshow() and also allow you to save it with imwrite() since it's now uint8. So the drawbacks are that you lose precision of up to 1/2 a gray level, but has the advantages that the intensity range is the same and you can now display and save it with ease. It's your call which way to do it

hayet hadfi
hayet hadfi on 11 Apr 2015
how to Create model hardware Conversion Blockset RGB to Gray using simulink
  1 Comment
Image Analyst
Image Analyst on 11 Apr 2015
I suggest you start your own question with a subject line "Conversion Blockset RGB to Gray using Simulink"

Sign in to comment.

Categories

Find more on Modify Image Colors in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!