Login  Register

Re: 24 bit RGB to 16 bit gray conversion

Posted by ij-Newsgroup on Nov 15, 2005; 3:23am
URL: http://imagej.273.s1.nabble.com/24-bit-RGB-to-16-bit-gray-conversion-tp3704473p3704477.html

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