Forums

Converting image from grayscale to RGB

Started by sajjad_strong November 9, 2011
Hi

I am trying to write a Matlab program and having hard time figuring out how to convert back an image which have been converted to gray scale to RGB for instance :

IRG = imread('someimage.bmp');
X = rgb2gray(IRG);
after doing my operations on X how can I convert it back to RGB

I'll really appreciate it if anyone can help me
I never tried such conversion but here is something it might work:
imagine a color palette in gray. It goes from 0 (black) to 255 (white).
If you were to use a triplet (a la RGB) you would have something like
[0 0 0
1 1 1
.....
255 255 255]
Now, the palette in RGB as defined in Matlab should be something like
[255 0 0
.....
0 0 255]
Because the two palettes are in correspondence you can go pixel by pixel
in your gray image, set the correspondence and create three image
matrices, one for each color of the RGB.

As a example of the opposite conversion using such correspondence you
can look for

convertrgb2gray.m

in the File Exchange.

Best regards

Atte.

Tordar

On Sat, 2011-10-22 at 06:22 +0000, sajjad_strong wrote:
>
> Hi
>
> I am trying to write a Matlab program and having hard time figuring
> out how to convert back an image which have been converted to gray
> scale to RGB for instance :
>
> IRG = imread('someimage.bmp');
> X = rgb2gray(IRG);
> after doing my operations on X how can I convert it back to RGB
>
> I'll really appreciate it if anyone can help me
>
The short answer is, you can not. Because conversion to grayscale results
in the loss of information which can not be recovered.

If you have an algorithm that works only on grayscale images, you can work
on each image plane (R,G,B) separately which you can get as IRG( : ,: ,1)
for the Red plane.

Hope it helps,
On 22 October 2011 08:22, sajjad_strong wrote:

> **
> Hi
>
> I am trying to write a Matlab program and having hard time figuring out
> how to convert back an image which have been converted to gray scale to RGB
> for instance :
>
> IRG = imread('someimage.bmp');
> X = rgb2gray(IRG);
> after doing my operations on X how can I convert it back to RGB
>
> I'll really appreciate it if anyone can help me
>
>
>

--
Yasir Latif
u can try with following function

x is your rgb2gray(IRG);

imRGB = grs2rgb(x,cmap);

with Best Regards
MM Gehlot
the answer is :

IRG = imread('someimage.bmp');

[X  colorMap]= rgb2gray(IRG);
imRGB=gray2egb(x,colorMAP)

All the best

Eng. Abdulaziz  Ghableb

--- On Thu, 11/10/11, Yasir Latif wrote:

From: Yasir Latif
Subject: Re: [matlab] Converting image from grayscale to RGB
To: "sajjad_strong"
Cc: m...
Date: Thursday, November 10, 2011, 2:10 AM

 

The short answer is, you can not. Because conversion to grayscale
results in the loss of information which can not be recovered.

If you have an algorithm that works only on grayscale images, you can
work on each image plane (R,G,B) separately which you can get as IRG( :
,: ,1) for the Red plane.

Hope it helps,
On 22 October 2011 08:22, sajjad_strong wrote:

 

Hi

I am trying to write a Matlab program and having hard time figuring out how to convert back an image which have been converted to gray scale to RGB for instance :

IRG = imread('someimage.bmp');

X = rgb2gray(IRG);

after doing my operations on X how can I convert it back to RGB

I'll really appreciate it if anyone can help me

--
Yasir Latif