24 bit RGB to 16 bit gray conversion?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

24 bit RGB to 16 bit gray conversion?

ij-Newsgroup
Does anyone know how to convert a 24 bit RGB image (16.7 million
colours) into a 16 bit grayscale image?

Thanks
Art
--
============================================
==    CSO & System Administrators         ==
==    ALS, Sydney, Australia              ==
==    [hidden email]  ==
============================================

========================================================================
The information contained in this e-mail message, including any
attachments, is confidential, subject to copyright, and maybe the
subject of legal and other privilege. Any dissemination, distribution,
or copying of this message, in its entirety or part by any entity apart
from the intended recipient is strictly prohibited. Please accept our
apologies if you are not the intended recipient.  If you think that you
have received this e-mail message in error, please e-mail the sender and
destroy the original message without making a copy. Unfortunately, we
cannot warrant that the email has not been altered or corrupted during
transmission. Thank you.
========================================================================
Reply | Threaded
Open this post in threaded view
|

Re: 24 bit RGB to 16 bit gray conversion

Robert Rohland
Art -

Not too difficult. Gray scale is just the average of the red, green, and
blue channels. If you have 16 bits (65,536 levels) to work with, you can
simply add the red, green, and blue channels and scale them up however you
like.

Here's how to do one manually:

I just opened the baboon.jpg, a 24-bit RGB. Select the baboon window. Then
select the menu command Image / Color / RGB Split. The colorful baboon will
go away and you will see three new windows entitled baboon.jpg (red),
baboon.jpg (green), and baboon.jpg (blue). Each is 8-bit gray scale.

First, you have to convert each of these 8-bit (256 levels) images to
16-bits (65536 levels). Select "baboon.jpg (red)." Select menu command Image
/ Type / 16-bit. Do the same with the green and blue 8-bit channels.
Converting to 16-bits before doing math will prevent overflow.

Now select menu command Process / Image Calculator. In the dialog box,
select "baboon.jpg (red)" for Image1, "Add" for Operation, and "baboon.jpg
(green)" for Image2. Check the "Create New Window" checkbox and hit OK.
You will get a new window entitled "Result of baboon.jpg," which will be a
16-bit result.

Now run the Image calculator again to add in the "baboon.jpg (blue)" layer.
You will a new image called "Result of Result." This is your 16-bit
grayscale image. Since you just added three 256-level images together, the
maximum will be 765. You can scale this up by selecting menu command Process
/ Math / Multiply. A dialog box will ask you for the multiplier. The biggest
multiplier you can use is 85 before you overflow the 16-bit maximum of
65535.

If you need to adjust the brightness and contrast, use Image / Adjust /
Brightness/Contrast.

If you have a whole bunch of pictures to do, make a macro using this
sequence of commands.

- Robert M. Rohland
  [hidden email]
  [hidden email]
  146 Garner Circle
  Montrose MN 55363
  763-675-8905
  612-226-9735 (cell)

----- Original Message -----
From: "ij-Newsgroup" <[hidden email]>
To: <[hidden email]>
Sent: Sunday, November 13, 2005 3:54 PM
Subject: 24 bit RGB to 16 bit gray conversion?


> Does anyone know how to convert a 24 bit RGB image (16.7 million
> colours) into a 16 bit grayscale image?
>
> Thanks
> Art
Reply | Threaded
Open this post in threaded view
|

Re: 24 bit RGB to 16 bit gray conversion

Jeremy Winston-2
Robert Rohland wrote:
> Art -
>
> Not too difficult. Gray scale is just the average of the red, green, and
> blue channels.

Robert,
    That's not quite right.  Gray scale can be computed from
RGB as the *weighted* average of the RG&B, taking into account
the sensitivity of the eye's rods and cones to those three
colors.  Cf.
http://documents.wolfram.com/applications/digitalimage/FunctionIndex/ToGrayLevel.html

HTH,
-Jeremy
Reply | Threaded
Open this post in threaded view
|

Re: 24 bit RGB to 16 bit gray conversion

Joel Sheffield
Indeed, this is an option for ImageJ.  Check Edit>Options>Conversions
on the ImageJ menu, and search in the list for more discussion of
this topic.  It is also discussed in the Help>Online Docs section on
the Edit menu.  Here are the comments:

Check Scale When Converting to have ImageJ scale from min-max to 0-
255 when converting from 16-bits or 32-bits to 8-bits or to scale
from min-max to 0-65535 when converting from 32-bits to 16-bits.

If Unweighted RGB to Grayscale Conversion is checked, the formula
gray=(red+green+blue)/3 is used to convert RGB images to grayscale.
If this option is not checked, the formula
gray=0.299*red+0.587*green+0.114*blue is used.



> Robert Rohland wrote:
> > Art -
> >
> > Not too difficult. Gray scale is just the average of the red, green,
> > and blue channels.
>
> Robert,
>     That's not quite right.  Gray scale can be computed from
> RGB as the *weighted* average of the RG&B, taking into account
> the sensitivity of the eye's rods and cones to those three
> colors.  Cf.
> http://documents.wolfram.com/applications/digitalimage/FunctionIndex/T
> oGrayLevel.html
>
> HTH,
> -Jeremy


Joel B. Sheffield, Ph.D.
Biology Department, Temple University
1900 North 12th Street
Philadelphia, PA 19122
[hidden email]  
(215) 204 8839, fax (215) 204 0486
http://astro.temple.edu/~jbs
Reply | Threaded
Open this post in threaded view
|

not quite yet... Re: 24 bit RGB to 16 bit gray conversion

ij-Newsgroup
In reply to this post by Robert Rohland
Thanks for your suggestions!

However, I still feel like having lost more information than I wanted to.

If I look at the histogram of either red, blue or green channel, then it
looks like a comb when converting from 8bit to 16bit (obviously) and
after adding the 3 channels, I again end up with a comb-like histogram
that uses only 256 (or less) of the 65535 possible colours. So in short,
I have gone from 16 million colours to 256 shades of gray and not to
65535 as intended.

What I want to do is to convert the 16 million colours into 65535 levels
of gray WITHOUT  chopping it down to 256 colours in any of the
intermediate steps.

This is for  a densitometry application for which the response of the
eye is not relevant, but it is important to destinguish more than 256
different gray values to quantify the staining accurately.

I understand that 24bit means 3 times 8 bit, hence 8 bit per colour
(red, blue, green) and that 8 bit allow 256 histogram channels and hence
256*256*256=16,777,216 result in 16.7 million colours. But that still
somehow does not fully convince me that I can only obtain 256 different
gray values. If I use different weights for each colour channel (as
suggested by one of you) then the resulting sum of the 3 split images
would not give me the honeycomb structure in the histogram, but
weighting colours by the wavelength-dependent sensitivity of the human
eye may not be adequate for my application.

Any more comments?

Thanks,
Art



Robert Rohland wrote:

> Art -
>
> Not too difficult. Gray scale is just the average of the red, green, and
> blue channels. If you have 16 bits (65,536 levels) to work with, you can
> simply add the red, green, and blue channels and scale them up however
> you like.
>
> Here's how to do one manually:
>
> I just opened the baboon.jpg, a 24-bit RGB. Select the baboon window.
> Then select the menu command Image / Color / RGB Split. The colorful
> baboon will go away and you will see three new windows entitled
> baboon.jpg (red), baboon.jpg (green), and baboon.jpg (blue). Each is
> 8-bit gray scale.
>
> First, you have to convert each of these 8-bit (256 levels) images to
> 16-bits (65536 levels). Select "baboon.jpg (red)." Select menu command
> Image / Type / 16-bit. Do the same with the green and blue 8-bit
> channels. Converting to 16-bits before doing math will prevent overflow.
>
> Now select menu command Process / Image Calculator. In the dialog box,
> select "baboon.jpg (red)" for Image1, "Add" for Operation, and
> "baboon.jpg (green)" for Image2. Check the "Create New Window" checkbox
> and hit OK.
> You will get a new window entitled "Result of baboon.jpg," which will be
> a 16-bit result.
>
> Now run the Image calculator again to add in the "baboon.jpg (blue)"
> layer. You will a new image called "Result of Result." This is your
> 16-bit grayscale image. Since you just added three 256-level images
> together, the maximum will be 765. You can scale this up by selecting
> menu command Process / Math / Multiply. A dialog box will ask you for
> the multiplier. The biggest multiplier you can use is 85 before you
> overflow the 16-bit maximum of 65535.
>
> If you need to adjust the brightness and contrast, use Image / Adjust /
> Brightness/Contrast.
>
> If you have a whole bunch of pictures to do, make a macro using this
> sequence of commands.
>
> - Robert M. Rohland
>  [hidden email]
>  [hidden email]
>  146 Garner Circle
>  Montrose MN 55363
>  763-675-8905
>  612-226-9735 (cell)
>
> ----- Original Message ----- From: "ij-Newsgroup"
> <[hidden email]>
> To: <[hidden email]>
> Sent: Sunday, November 13, 2005 3:54 PM
> Subject: 24 bit RGB to 16 bit gray conversion?
>
>
>> Does anyone know how to convert a 24 bit RGB image (16.7 million
>> colours) into a 16 bit grayscale image?
>>
>> Thanks
>> Art

--
============================================
==    CSO & System Administrators         ==
==    ALS, Sydney, Australia              ==
==    [hidden email]  ==
============================================

========================================================================
The information contained in this e-mail message, including any
attachments, is confidential, subject to copyright, and maybe the
subject of legal and other privilege. Any dissemination, distribution,
or copying of this message, in its entirety or part by any entity apart
from the intended recipient is strictly prohibited. Please accept our
apologies if you are not the intended recipient.  If you think that you
have received this e-mail message in error, please e-mail the sender and
destroy the original message without making a copy. Unfortunately, we
cannot warrant that the email has not been altered or corrupted during
transmission. Thank you.
========================================================================
Reply | Threaded
Open this post in threaded view
|

Re: 24 bit RGB to 16 bit gray conversion

Wayne Rasband
The following macro converts an RGB image with 16.7 million possible  
colors into a 32-bit image with 16.7 million possible gray values.  
Convert the 32-bit image to 16-bits and you get an image with 65536  
possible gray values.

   if (bitDepth!=24)
       exit("RGB image required");
   setBatchMode(true);
   w = getWidth;
   h = getHeight;
   rgb = getImageID;
   newImage("32-bit", "32-bit", w, h, 1);
   float = getImageID;
   for (y=0; y<h; y++) {
       showProgress(y, h);
       for (x=0; x<w; x++) {
           selectImage(rgb);
           v = getPixel(x,y)&0xffffff;
           selectImage(float);
           setPixel(x, y, v);
       }
   }
   resetMinAndMax;
   setBatchMode(false);
   //run("16-bit");

-wayne

On Nov 14, 2005, at 5:10 PM, ij-Newsgroup wrote:

> Thanks for your suggestions!
>
> However, I still feel like having lost more information than I  
> wanted to.
>
> If I look at the histogram of either red, blue or green channel,  
> then it
> looks like a comb when converting from 8bit to 16bit (obviously) and
> after adding the 3 channels, I again end up with a comb-like histogram
> that uses only 256 (or less) of the 65535 possible colours. So in  
> short,
> I have gone from 16 million colours to 256 shades of gray and not to
> 65535 as intended.
>
> What I want to do is to convert the 16 million colours into 65535  
> levels
> of gray WITHOUT  chopping it down to 256 colours in any of the
> intermediate steps.
>
> This is for  a densitometry application for which the response of the
> eye is not relevant, but it is important to destinguish more than 256
> different gray values to quantify the staining accurately.
>
> I understand that 24bit means 3 times 8 bit, hence 8 bit per colour
> (red, blue, green) and that 8 bit allow 256 histogram channels and  
> hence
> 256*256*256=16,777,216 result in 16.7 million colours. But that still
> somehow does not fully convince me that I can only obtain 256  
> different
> gray values. If I use different weights for each colour channel (as
> suggested by one of you) then the resulting sum of the 3 split images
> would not give me the honeycomb structure in the histogram, but
> weighting colours by the wavelength-dependent sensitivity of the human
> eye may not be adequate for my application.
>
> Any more comments?
>
> Thanks,
> Art
>
>
>
> Robert Rohland wrote:
> > Art -
> >
> > Not too difficult. Gray scale is just the average of the red,  
> green, and
> > blue channels. If you have 16 bits (65,536 levels) to work with,  
> you can
> > simply add the red, green, and blue channels and scale them up  
> however
> > you like.
> >
> > Here's how to do one manually:
> >
> > I just opened the baboon.jpg, a 24-bit RGB. Select the baboon  
> window.
> > Then select the menu command Image / Color / RGB Split. The colorful
> > baboon will go away and you will see three new windows entitled
> > baboon.jpg (red), baboon.jpg (green), and baboon.jpg (blue). Each is
> > 8-bit gray scale.
> >
> > First, you have to convert each of these 8-bit (256 levels)  
> images to
> > 16-bits (65536 levels). Select "baboon.jpg (red)." Select menu  
> command
> > Image / Type / 16-bit. Do the same with the green and blue 8-bit
> > channels. Converting to 16-bits before doing math will prevent  
> overflow.
> >
> > Now select menu command Process / Image Calculator. In the dialog  
> box,
> > select "baboon.jpg (red)" for Image1, "Add" for Operation, and
> > "baboon.jpg (green)" for Image2. Check the "Create New Window"  
> checkbox
> > and hit OK.
> > You will get a new window entitled "Result of baboon.jpg," which  
> will be
> > a 16-bit result.
> >
> > Now run the Image calculator again to add in the "baboon.jpg (blue)"
> > layer. You will a new image called "Result of Result." This is your
> > 16-bit grayscale image. Since you just added three 256-level images
> > together, the maximum will be 765. You can scale this up by  
> selecting
> > menu command Process / Math / Multiply. A dialog box will ask you  
> for
> > the multiplier. The biggest multiplier you can use is 85 before you
> > overflow the 16-bit maximum of 65535.
> >
> > If you need to adjust the brightness and contrast, use Image /  
> Adjust /
> > Brightness/Contrast.
> >
> > If you have a whole bunch of pictures to do, make a macro using this
> > sequence of commands.
> >
> > - Robert M. Rohland
> >  [hidden email]
> >  [hidden email]
> >  146 Garner Circle
> >  Montrose MN 55363
> >  763-675-8905
> >  612-226-9735 (cell)
> >
> > ----- Original Message ----- From: "ij-Newsgroup"
> > <[hidden email]>
> > To: <[hidden email]>
> > Sent: Sunday, November 13, 2005 3:54 PM
> > Subject: 24 bit RGB to 16 bit gray conversion?
> >
> >
> >> Does anyone know how to convert a 24 bit RGB image (16.7 million
> >> colours) into a 16 bit grayscale image?
> >>
> >> Thanks
> >> Art
>
> --
> ============================================
> ==    CSO & System Administrators         ==
> ==    ALS, Sydney, Australia              ==
> ==    [hidden email]  ==
> ============================================
>
> ======================================================================
> ==
> The information contained in this e-mail message, including any
> attachments, is confidential, subject to copyright, and maybe the
> subject of legal and other privilege. Any dissemination, distribution,
> or copying of this message, in its entirety or part by any entity  
> apart
> from the intended recipient is strictly prohibited. Please accept our
> apologies if you are not the intended recipient.  If you think that  
> you
> have received this e-mail message in error, please e-mail the  
> sender and
> destroy the original message without making a copy. Unfortunately, we
> cannot warrant that the email has not been altered or corrupted during
> transmission. Thank you.
> ======================================================================
> ==
>
Reply | Threaded
Open this post in threaded view
|

Re: 24 bit RGB to 16 bit gray conversion

ij-Newsgroup
Thanks Wayne,

that is looking good. However, I am unable to run your macro.  I get the
following error:

Undefined identifier in line 7
   <newImage>("32-bit", "32-bit", w, h, 1);

I used
Plugins -> Macro -> Record
and pasted your code in and then tried to run it - without succes. I did
have one 24bit RGB image open (a copy of the one I wanted to convert).

Any suggestions? I am afraid I am not very versatile in debugging the
example code.

Thanks for all your help!


Rasband Wayne wrote:

> The following macro converts an RGB image with 16.7 million possible
> colors into a 32-bit image with 16.7 million possible gray values.
> Convert the 32-bit image to 16-bits and you get an image with 65536
> possible gray values.
>
>   if (bitDepth!=24)
>       exit("RGB image required");
>   setBatchMode(true);
>   w = getWidth;
>   h = getHeight;
>   rgb = getImageID;
>   newImage("32-bit", "32-bit", w, h, 1);
>   float = getImageID;
>   for (y=0; y<h; y++) {
>       showProgress(y, h);
>       for (x=0; x<w; x++) {
>           selectImage(rgb);
>           v = getPixel(x,y)&0xffffff;
>           selectImage(float);
>           setPixel(x, y, v);
>       }
>   }
>   resetMinAndMax;
>   setBatchMode(false);
>   //run("16-bit");
>
> -wayne
>
> On Nov 14, 2005, at 5:10 PM, ij-Newsgroup wrote:
>
>> Thanks for your suggestions!
>>
>> However, I still feel like having lost more information than I  wanted
>> to.
>>
>> If I look at the histogram of either red, blue or green channel,  then it
>> looks like a comb when converting from 8bit to 16bit (obviously) and
>> after adding the 3 channels, I again end up with a comb-like histogram
>> that uses only 256 (or less) of the 65535 possible colours. So in  short,
>> I have gone from 16 million colours to 256 shades of gray and not to
>> 65535 as intended.
>>
>> What I want to do is to convert the 16 million colours into 65535  levels
>> of gray WITHOUT  chopping it down to 256 colours in any of the
>> intermediate steps.
>>
>> This is for  a densitometry application for which the response of the
>> eye is not relevant, but it is important to destinguish more than 256
>> different gray values to quantify the staining accurately.
>>
>> I understand that 24bit means 3 times 8 bit, hence 8 bit per colour
>> (red, blue, green) and that 8 bit allow 256 histogram channels and  hence
>> 256*256*256=16,777,216 result in 16.7 million colours. But that still
>> somehow does not fully convince me that I can only obtain 256  different
>> gray values. If I use different weights for each colour channel (as
>> suggested by one of you) then the resulting sum of the 3 split images
>> would not give me the honeycomb structure in the histogram, but
>> weighting colours by the wavelength-dependent sensitivity of the human
>> eye may not be adequate for my application.
>>
>> Any more comments?
>>
>> Thanks,
>> Art
>>
>>
>>
>> Robert Rohland wrote:
>> > Art -
>> >
>> > Not too difficult. Gray scale is just the average of the red,
>> green, and
>> > blue channels. If you have 16 bits (65,536 levels) to work with,
>> you can
>> > simply add the red, green, and blue channels and scale them up  however
>> > you like.
>> >
>> > Here's how to do one manually:
>> >
>> > I just opened the baboon.jpg, a 24-bit RGB. Select the baboon  window.
>> > Then select the menu command Image / Color / RGB Split. The colorful
>> > baboon will go away and you will see three new windows entitled
>> > baboon.jpg (red), baboon.jpg (green), and baboon.jpg (blue). Each is
>> > 8-bit gray scale.
>> >
>> > First, you have to convert each of these 8-bit (256 levels)  images to
>> > 16-bits (65536 levels). Select "baboon.jpg (red)." Select menu  command
>> > Image / Type / 16-bit. Do the same with the green and blue 8-bit
>> > channels. Converting to 16-bits before doing math will prevent
>> overflow.
>> >
>> > Now select menu command Process / Image Calculator. In the dialog  box,
>> > select "baboon.jpg (red)" for Image1, "Add" for Operation, and
>> > "baboon.jpg (green)" for Image2. Check the "Create New Window"
>> checkbox
>> > and hit OK.
>> > You will get a new window entitled "Result of baboon.jpg," which
>> will be
>> > a 16-bit result.
>> >
>> > Now run the Image calculator again to add in the "baboon.jpg (blue)"
>> > layer. You will a new image called "Result of Result." This is your
>> > 16-bit grayscale image. Since you just added three 256-level images
>> > together, the maximum will be 765. You can scale this up by  selecting
>> > menu command Process / Math / Multiply. A dialog box will ask you  for
>> > the multiplier. The biggest multiplier you can use is 85 before you
>> > overflow the 16-bit maximum of 65535.
>> >
>> > If you need to adjust the brightness and contrast, use Image /
>> Adjust /
>> > Brightness/Contrast.
>> >
>> > If you have a whole bunch of pictures to do, make a macro using this
>> > sequence of commands.
>> >
>> > - Robert M. Rohland
>> >  [hidden email]
>> >  [hidden email]
>> >  146 Garner Circle
>> >  Montrose MN 55363
>> >  763-675-8905
>> >  612-226-9735 (cell)
>> >
>> > ----- Original Message ----- From: "ij-Newsgroup"
>> > <[hidden email]>
>> > To: <[hidden email]>
>> > Sent: Sunday, November 13, 2005 3:54 PM
>> > Subject: 24 bit RGB to 16 bit gray conversion?
>> >
>> >
>> >> Does anyone know how to convert a 24 bit RGB image (16.7 million
>> >> colours) into a 16 bit grayscale image?
>> >>
>> >> Thanks
>> >> Art
>>
>> --
>> ============================================
>> ==    CSO & System Administrators         ==
>> ==    ALS, Sydney, Australia              ==
>> ==    [hidden email]  ==
>> ============================================
>>
>> ====================================================================== ==
>> The information contained in this e-mail message, including any
>> attachments, is confidential, subject to copyright, and maybe the
>> subject of legal and other privilege. Any dissemination, distribution,
>> or copying of this message, in its entirety or part by any entity  apart
>> from the intended recipient is strictly prohibited. Please accept our
>> apologies if you are not the intended recipient.  If you think that  you
>> have received this e-mail message in error, please e-mail the  sender and
>> destroy the original message without making a copy. Unfortunately, we
>> cannot warrant that the email has not been altered or corrupted during
>> transmission. Thank you.
>> ====================================================================== ==
>>

--
============================================
==    CSO & System Administrators         ==
==    ALS, Sydney, Australia              ==
==    [hidden email]  ==
============================================

========================================================================
The information contained in this e-mail message, including any
attachments, is confidential, subject to copyright, and maybe the
subject of legal and other privilege. Any dissemination, distribution,
or copying of this message, in its entirety or part by any entity apart
from the intended recipient is strictly prohibited. Please accept our
apologies if you are not the intended recipient.  If you think that you
have received this e-mail message in error, please e-mail the sender and
destroy the original message without making a copy. Unfortunately, we
cannot warrant that the email has not been altered or corrupted during
transmission. Thank you.
========================================================================