Strabo.Core.ImageProcessing.ColorHistogram.GetColorHashSet C# (CSharp) Method

GetColorHashSet() public method

public GetColorHashSet ( Bitmap srcimg ) : HashSet
srcimg System.Drawing.Bitmap
return HashSet
        public HashSet<int> GetColorHashSet(Bitmap srcimg)
        {
            srcimg=ImageUtils.AnyToFormat24bppRgb(srcimg);
            HashSet<int> color24bppRgb = new HashSet<int>();
            // get source image size
            int width = srcimg.Width;
            int height = srcimg.Height;

            PixelFormat srcFmt = (srcimg.PixelFormat == PixelFormat.Format8bppIndexed) ?
                PixelFormat.Format8bppIndexed : PixelFormat.Format24bppRgb;

            // lock source bitmap data
            BitmapData srcData = srcimg.LockBits(
                new Rectangle(0, 0, width, height),
                ImageLockMode.ReadOnly, srcFmt);

            int srcOffset = srcData.Stride - ((srcFmt == PixelFormat.Format8bppIndexed) ? width : width * 3);

            // do the job
            unsafe
            {
                byte* src = (byte*)srcData.Scan0.ToPointer();

                {
                    // RGB binarization
                    for (int y = 0; y < height; y++)
                    {
                        for (int x = 0; x < width; x++, src += 3)
                            color24bppRgb.Add( src[RGB.R] * 256 * 256 + src[RGB.G] * 256 + src[RGB.B]);
                        src += srcOffset;
                    }
                }
            }
            srcimg.UnlockBits(srcData);
            return color24bppRgb;
        }

Usage Example

 public void GenerateColorFromNegativeExamples(Bitmap srcimg)
 {
     ColorHistogram color_histogram = new ColorHistogram();
     HashSet<int> color = color_histogram.GetColorHashSet(srcimg);
     foreach (int c in color)
         negtive_color.Add(c);
 }