Support Center

Determining if a 24/32 bit Image has color or is greyscale or Black & White with the Document Edition

Last Updated: May 09, 2013 09:45AM EDT
You can use this class along with the Document Edition of ImageMan.Net to determine if an image is Color, Greyscale or Black & White.

Here's some sample code that shows using it:

// Requires a reference to the DTI.ImageMan.TessOcr assembly
using DTI.ImageMan;

ColorAnalyzer a = new ColorAnalyzer(viewer1.Images.CurrentImage);

Console.WriteLine("Color = {0}, Greyscale = {1}, Black&WHite = {2}", a.IsColor, a.IsGreyscale, a.IsBlackAndWhite);
if (a.IsGreyscale)
Console.WriteLine("{0} Grey Entries", a.GreyScaleEntries);

A couple of notes:

1. This will only work on 4,8,24 and 32 bit images. 16 Bit images are not currently supported but will be in a near future build.

2. Some scanned images might basically be black and white images but have some greyscale pixels, in this case it would return IsGreyscale == true but you can check the GreyScaleEntries property to see how many greyscale entries there are, if only a small number then perhaps it would be worth reducing to a 1 bit image. You could also modify this code to confirm that the greyscale entries are clustered around the black & white entries.

3. This requires the DTI.ImageMan.TessOcr assembly to be referenced in your solution.

The ColorAnalyzerClass:

    public class ColorAnalyzer
    {
        int[,] histogram;
        public ColorAnalyzer(ImImage img)
        {
            histogram = Analyze.GetColorHistogram(img);
            GreyScaleEntries = 0;

            // Check for Color and Greyscale
            for( int i = 0; i < 256; i++  ) {
                if (histogram[0, i] == histogram[1, i] && histogram[1, i] == histogram[2, i])
                {
                    if (histogram[0, i] > 0)
                    {
                        GreyScaleEntries++;
                        IsGreyscale = true;
                    }
                } 
                else
                {
                    IsGreyscale = false;
                    IsColor = true;
                }
            }

            // This could be a black & White image so lets check
            if (IsGreyscale)
            {
                if (GreyScaleEntries == 2 && histogram[0, 0] > 0 && histogram[0, 255] > 0)
                {
                    IsBlackAndWhite = true;
                    IsGreyscale = false;
                }
            }
        }

        public bool IsColor;
        public bool IsGreyscale;
        public bool IsBlackAndWhite;
        public int GreyScaleEntries;
    }

Contact Us