Welcome to LEAD Support Forum Login | Register | Faq  

    LEAD Support Forum
  Resource to find answers and post technical questions about LEAD products.
Search    
   

Re: Example of grayscale to 24 bit RGB conversion
Started by programsunlimited at 11-18-2008 18:54. Topic has 4 replies.

Print Search « Previous Thread Next Thread »
  11-18-2008, 18:54
programsunlimited is not online. Last active: 12/2/2008 9:06:28 PM programsunlimited

Top 500 Posts
Joined on 11-01-2008
Posts 8
Example of grayscale to 24 bit RGB conversion
Reply Quote
I'm using the Leadtools Medical product and am trying to convert from an 8 bit grayscale image to a 24 bit RGB. I've found snippets that seem to point the way to doing this using the WindowsLevel command.

No matter what I seem to do, however, the .tif image is saved as greyscale, not RGB. Here's the code

                // Grab the 8 bit grayscale image stored in .cmp format
                RasterImage image =
                    prCodecs.Load(@"C:\Images\example.cmp");
 
               // Change the image to 16-bit grayscale 
               GrayscaleCommand grayscaleCmd = new GrayscaleCommand(16);
               grayscaleCmd.Run(image);

               // Get values needed for conversion (min and max bits etc.)
               MinMaxBitsCommand minMaxBitsCmd = new MinMaxBitsCommand();
               minMaxBitsCmd.Run(image);

               MinMaxValuesCommand minMaxValuesCmd = new MinMaxValuesCommand();
               minMaxValuesCmd.Run(image);

               int lowBit = minMaxBitsCmd.MinimumBit;
               int highBit = minMaxBitsCmd.MaximumBit;

               int size = (1 << (image.HighBit - image.LowBit + 1));

               // Create a new color palette since original image didn't have one
               RasterColor[] palette = new RasterColor[size];

               int minVal = minMaxValuesCmd.MinimumValue;
               int maxVal = minMaxValuesCmd.MaximumValue;

               // Set RGB colors to reflect same grayscale values 
               for (int x = 0; x < size; x++)
               {
                   palette[x].R =
                           Convert.ToByte(Math.Min(255, ((x - minVal) * 255 / (maxVal - minVal))));
                   palette[x].G = palette[x].R;
                   palette[x].B = palette[x].R;
                   palette[x].Reserved = 0;
               }
              
               // Convert image using windows leveling
               // THIS PART MAY BE THE PROBLEM? NOT SURE IF NEED TO
              // UNLOCK OR IF WINDOWLEVEL IS THE CORRECT WAY TO CONVERT
               image.WindowLevel(lowBit,
                                               highBit,
                                                palette,
                                                RasterWindowLevelMode.PaintAndProcessing);    

              // Not sure if this matters, but set anyway
               prCodecs.Options.Save.GrayOutput = false;

               // Save the file as tif
                prCodecs.Save
                    (image,
                    prTargetFilePath,
                    RasterImageFormat.Tif,
                    0); //use default bits per pixel
                       

                image.Dispose();

   Report 
  11-18-2008, 23:50
programsunlimited is not online. Last active: 12/2/2008 9:06:28 PM programsunlimited

Top 500 Posts
Joined on 11-01-2008
Posts 8
Re: Example of grayscale to 24 bit RGB conversion
Reply Quote
Following another thread I found that I probably have to use the WindowLevelCommand to be able to actually save an image in the RGB 24 bit format. I modified the code as highlighted in yellow below but still have no luck in saving an RGB 24 bit image.

            Leadtools.Codecs.RasterCodecs prCodecs;

            prCodecs.ThrowExceptionsOnInvalidImages = false;

            // Make sure that we can convert this type of file
            CodecsImageInfo iiCodecsImageInfo =
                prCodecs.GetInformation(prSourceFilePath, false);

            // Unable to convert this format, pop up message
            if (iiCodecsImageInfo.Format == RasterImageFormat.Unknown)
            {
                MessageBox.Show("The file, " + prSourceFilePath +
                    " cannot be converted to an RGB image", "Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);

                boInputValid = false;
            }

            // If can convert, create directory and perform conversion
            if (iiCodecsImageInfo.Format != RasterImageFormat.Unknown)
            {
                // Grab the image
                RasterImage image =
                    prCodecs.Load(prSourceFilePath);

                // Change the image to 16-bit grayscale  
                GrayscaleCommand grayscaleCmd = new GrayscaleCommand(16);
                grayscaleCmd.Run(image);

                // WindowLevelCommand is used to convert from greyscale
                WindowLevelCommand command = new WindowLevelCommand();

                MinMaxBitsCommand minMaxBitsCmd = new MinMaxBitsCommand();
                minMaxBitsCmd.Run(image);

                MinMaxValuesCommand minMaxValuesCmd = new MinMaxValuesCommand();
                minMaxValuesCmd.Run(image);

                int lowBit = minMaxBitsCmd.MinimumBit;
                int highBit = minMaxBitsCmd.MaximumBit;

                int size = (1 << (image.HighBit - image.LowBit + 1));
                RasterColor[] palette = new RasterColor[size];

                int minVal = minMaxValuesCmd.MinimumValue;
                int maxVal = minMaxValuesCmd.MaximumValue;

               // Fill the rest with gray values  
               for (int x = 0; x < size; x++)
               {
                   palette[x].R = Convert.ToByte(Math.Min(255, ((x - minVal) * 255 / (maxVal - minVal))));
                   palette[x].G = palette[x].R;
                   palette[x].B = palette[x].R;
                   palette[x].Reserved = 0;
               }
               
               image.WindowLevel(lowBit, highBit, palette, RasterWindowLevelMode.PaintAndProcessing);

               command.HighBit = minMaxBitsCmd.MaximumBit;
               command.LowBit = minMaxBitsCmd.MinimumBit;
               command.LookupTable = palette;
               command.Order = Leadtools.RasterByteOrder.Rgb;
               command.Run(image);

                  // Save the image in the file path passed to the newly
                // established raw image file name in TIFF format
               prCodecs.Options.Save.GrayOutput = false;
 
                prCodecs.Save
                    (image,
                    prTargetFilePath,
                    RasterImageFormat.Tif,
                    0); //use default bits per pixel
                        //prBitsPerPixel);

                image.Dispose();


   Report 
  11-19-2008, 8:06
Maen Hasan is not online. Last active: 12/30/2008 4:32:47 PM Maen Hasan



Top 10 Posts
Joined on 08-05-2004
Posts 1,876
Re: Example of grayscale to 24 bit RGB conversion
Reply Quote
Which LEADTOOLS version (v14, v14.5, v15, etc.) are you using?

If you try the same issue by using the following code, do you face the same problem?
+---------------+
RasterCodecs.Startup();
RasterCodecs prCodecs = new RasterCodecs();
string prSourceFilePath = @"f:\TempFiles\Winter.jpg";
string prTargetFilePath = @"c:\test.tif";
prCodecs.ThrowExceptionsOnInvalidImages = false;

// Make sure that we can convert this type of file
CodecsImageInfo iiCodecsImageInfo =
prCodecs.GetInformation(prSourceFilePath, false);

// Unable to convert this format, pop up message
if (iiCodecsImageInfo.Format == RasterImageFormat.Unknown)
{
MessageBox.Show("The file, " + prSourceFilePath +
" cannot be converted to an RGB image", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);

//boInputValid = false;
}

// If can convert, create directory and perform conversion
if (iiCodecsImageInfo.Format != RasterImageFormat.Unknown)
{
// Grab the image
RasterImage image =
prCodecs.Load(prSourceFilePath);

// Change the image to 16-bit grayscale  
Leadtools.ImageProcessing.GrayscaleCommand grayscaleCmd = new Leadtools.ImageProcessing.GrayscaleCommand(16);
grayscaleCmd.Run(image);

// WindowLevelCommand is used to convert from greyscale
WindowLevelCommand command = new WindowLevelCommand();

MinMaxBitsCommand minMaxBitsCmd = new MinMaxBitsCommand();
minMaxBitsCmd.Run(image);

MinMaxValuesCommand minMaxValuesCmd = new MinMaxValuesCommand();
minMaxValuesCmd.Run(image);

int lowBit = minMaxBitsCmd.MinimumBit;
int highBit = minMaxBitsCmd.MaximumBit;

int size = (1 << (image.HighBit - image.LowBit + 1));
RasterColor[] palette = new RasterColor[size];

int minVal = minMaxValuesCmd.MinimumValue;
int maxVal = minMaxValuesCmd.MaximumValue;

for (int x = 0; x < size / 2; x++)
palette[x] = new Leadtools.RasterColor(255, 0, 0);

// fill the rest with gray values.
for (int x = size / 2; x < size; x++)
{
int y = (byte)((x - minVal) * 255 / (maxVal - minVal));
palette[x] = new Leadtools.RasterColor(y, y, y);
}


command.HighBit = minMaxBitsCmd.MaximumBit;
command.LowBit = minMaxBitsCmd.MinimumBit;
command.LookupTable = palette;
command.Order = Leadtools.RasterByteOrder.Rgb;
command.Run(image);

// Save the image in the file path passed to the newly
// established raw image file name in TIFF format
prCodecs.Options.Save.GrayOutput = false;
prCodecs.Save
 (image,
prTargetFilePath,
RasterImageFormat.Tif,
0); //use default bits per pixel
//prBitsPerPixel);

image.Dispose();
+---------------+

Thanks,
Maen Badwan
LEADTOOLS Technical Support

   Report 
  11-19-2008, 17:18
programsunlimited is not online. Last active: 12/2/2008 9:06:28 PM programsunlimited

Top 500 Posts
Joined on 11-01-2008
Posts 8
Re: Example of grayscale to 24 bit RGB conversion
Reply Quote
We're using V16 (Medical Tools). The code you supplied does work insofar as the image is saved in the correct format. But it colors the first part of the image in red. I changed the code slightly so that only one pixel was colored and it worked as well.

Does this mean that the only way to save as grayscale is to add a color? Can I add a transparent layer and add a color there?

Thanks for your help.



   Report 
  11-20-2008, 5:29
Maen Hasan is not online. Last active: 12/30/2008 4:32:47 PM Maen Hasan



Top 10 Posts
Joined on 08-05-2004
Posts 1,876
Re: Example of grayscale to 24 bit RGB conversion
Reply Quote
I went back and read your original question.
If all you want is to take an 8-bit image and save it as 24-bit image WITHOUT coloring it, you actually don't need any of the image processing functions. You can simply use RasterCodecs.Load() then call RasterCodecs.Save and pass a value of 24 in the bits per pixel parameter.

If you want to do the same thing in memory (without re-saving the image), you can use the ColorResolutionCommand Class.

Also, you can pass 24 in the Bits Per Pixel parameter of the RasterCodecs.Load to load the image as 24 BPP in the first place.

If none of these options is what you need, please explain why.

Thanks,
Maen Badwan
LEADTOOLS Technical Support

   Report 
Post
LEAD Support Fo... » General » Example Request... » Re: Example of grayscale to 24 bit RGB conversion

Powered by Community Server, by Telligent Systems