Welcome Guest! To enable all features, please Login or Register.

Notification

Icon
Error

Options
View
Last Go to last post Unread Go to first unread post
#1 Posted : Friday, June 2, 2017 4:13:35 PM(UTC)

Nick  
Nick

Groups: Registered, Tech Support, Administrators
Posts: 163

Was thanked: 9 time(s) in 9 post(s)

Typically, the RasterByteOrder of an image can be checked to determine the color range in an image. However, sometimes a color image may only contain greyscale information: that is, the intensity value for each pixel has the same values for Red, Green, and Blue.

The quickest way to determine this is to examine the histogram of the image. If any of the intensity values differ, the image is not greyscale.
https://www.leadtools.com/help/leadtools/v19m/dh/pc/histogramcommand.html

Code:

      public static bool IsFastGray(string input)
      {
         using (RasterCodecs codecs = new RasterCodecs())
         {
            RasterImage image = codecs.Load(input);

            HistogramCommand hc = new HistogramCommand();
            hc.Channel = HistogramCommandFlags.Red;
            hc.Run(image);
            long[] h_red = hc.Histogram;

            hc.Channel = HistogramCommandFlags.Green;
            hc.Run(image);
            long[] h_green = hc.Histogram;

            hc.Channel = HistogramCommandFlags.Blue;
            hc.Run(image);
            long[] h_blue = hc.Histogram;

            for (int i = 0; i < h_red.Length; i++)
            {
               long red = h_red[i];
               long green = h_green[i];
               long blue = h_blue[i];

               if (red != green || red != blue || green != blue)
               {
                  return false;
               }
            }
         }

         return true;
      }


Note this method, while quick, is not necessarily foolproof. There is an edge case that can occur such that if a color image has an equal distribution of all red, yellow, and blue pixels. While such images typically only exist in contrived situations, these can still be accommodated by examining the individual pixels and comparing their color values. In this case, a single pixel having a color intensity value in one channel differing from the others is sufficient to determine the image is actually "color".

Code:

 input)
      {
         using (RasterCodecs codecs = new RasterCodecs())
         {
            RasterImage image = codecs.Load(input);

            for (int i = 0; i < image.Width; i++)
            {
               for (int j = 0; j < image.Height; j++)
               {
                  RasterColor cl = image.GetPixelColor(i, j);

                  if (cl.R != cl.G || cl.R != cl.B || cl.G != cl.B)
                  {
                     return false;
                  }
               }
            }
         }

         return true;
      }


Note examining each pixel in the image will be considerably slower than the histogram, but will produce a more accurate response to accommodate edge cases.

Edited by moderator Monday, June 5, 2017 8:51:03 AM(UTC)  | Reason: Not specified

Nick Crook
Developer Support Engineer
LEAD Technologies, Inc.
LEAD Logo
 

Try the latest version of LEADTOOLS for free for 60 days by downloading the evaluation: https://www.leadtools.com/downloads

Wanna join the discussion? Login to your LEADTOOLS Support accountor Register a new forum account.

You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Powered by YAF.NET | YAF.NET © 2003-2024, Yet Another Forum.NET
This page was generated in 0.040 seconds.