Example #1
0
        // Gather statistics
        private void ProcessImage(BitmapData imageData, int width, int height)
        {
            pixels = pixelsWithoutBlack = 0;

            int[] s   = new int[256];
            int[] l   = new int[256];
            int[] swb = new int[256];
            int[] lwb = new int[256];
            RGB   rgb = new RGB( );
            HSL   hsl = new HSL( );

            int offset = imageData.Stride - width * 3;

            // do the job
            unsafe
            {
                byte *p = (byte *)imageData.Scan0.ToPointer( );

                // for each line
                for (int y = 0; y < height; y++)
                {
                    // for each pixel
                    for (int x = 0; x < width; x++, p += 3)
                    {
                        rgb.Red   = p[RGB.R];
                        rgb.Green = p[RGB.G];
                        rgb.Blue  = p[RGB.B];

                        // convert to HSL color space
                        AForge.Imaging.ColorConverter.RGB2HSL(rgb, hsl);

                        s[(int)(hsl.Saturation * 255)]++;
                        l[(int)(hsl.Luminance * 255)]++;
                        pixels++;

                        if ((hsl.Hue != 0.0) || (hsl.Luminance != 0.0) || (hsl.Saturation != 0.0))
                        {
                            swb[(int)(hsl.Saturation * 255)]++;
                            lwb[(int)(hsl.Luminance * 255)]++;
                            pixelsWithoutBlack++;
                        }
                    }
                    p += offset;
                }
            }

            // create histograms
            saturation = new HistogramD(s, new RangeD(0, 1));
            luminance  = new HistogramD(l, new RangeD(0, 1));

            saturationWithoutBlack = new HistogramD(swb, new RangeD(0, 1));
            luminanceWithoutBlack  = new HistogramD(lwb, new RangeD(0, 1));
        }
Example #2
0
        public unsafe ImageStatistics(Bitmap image, bool collectHSL)
        {
            int width  = image.Width;
            int height = image.Height;

            this.pixels = width * height;
            if (this.grayscale = image.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                BitmapData bitmapdata = image.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
                int[]      values     = new int[0x100];
                int        num3       = bitmapdata.Stride - width;
                byte *     numPtr     = (byte *)bitmapdata.Scan0.ToPointer();
                for (int i = 0; i < height; i++)
                {
                    int num5 = 0;
                    while (num5 < width)
                    {
                        values[numPtr[0]]++;
                        num5++;
                        numPtr++;
                    }
                    numPtr += num3;
                }
                image.UnlockBits(bitmapdata);
                this.gray = new Histogram(values);
            }
            else
            {
                BitmapData data2     = image.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
                int[]      numArray2 = new int[0x100];
                int[]      numArray3 = new int[0x100];
                int[]      numArray4 = new int[0x100];
                int[]      numArray5 = new int[0x100];
                int[]      numArray6 = new int[0x100];
                RGB        rgb       = new RGB();
                HSL        hsl       = new HSL();
                int        num6      = data2.Stride - (width * 3);
                byte *     numPtr2   = (byte *)data2.Scan0.ToPointer();
                for (int j = 0; j < height; j++)
                {
                    int num8 = 0;
                    while (num8 < width)
                    {
                        numArray2[numPtr2[2]]++;
                        numArray3[numPtr2[1]]++;
                        numArray4[numPtr2[0]]++;
                        if (collectHSL)
                        {
                            rgb.Red   = numPtr2[2];
                            rgb.Green = numPtr2[1];
                            rgb.Blue  = numPtr2[0];
                            if (collectHSL)
                            {
                                GodLesZ.Library.Imaging.ColorConverter.RGB2HSL(rgb, hsl);
                                numArray5[(int)(hsl.Saturation * 255.0)]++;
                                numArray6[(int)(hsl.Luminance * 255.0)]++;
                            }
                        }
                        num8++;
                        numPtr2 += 3;
                    }
                    numPtr2 += num6;
                }
                image.UnlockBits(data2);
                this.red   = new Histogram(numArray2);
                this.green = new Histogram(numArray3);
                this.blue  = new Histogram(numArray4);
                if (collectHSL)
                {
                    this.saturation = new HistogramD(numArray5, new RangeD(0.0, 1.0));
                    this.luminance  = new HistogramD(numArray6, new RangeD(0.0, 1.0));
                }
            }
        }
Example #3
0
        // Gather statistics
        private void ProcessImage(BitmapData imageData, int width, int height)
        {
            pixels = pixelsWithoutBlack = 0;

            int[] yhisto  = new int[256];
            int[] cbhisto = new int[256];
            int[] crhisto = new int[256];

            int[] yhistoWB  = new int[256];
            int[] cbhistoWB = new int[256];
            int[] crhistoWB = new int[256];

            RGB   rgb   = new RGB( );
            YCbCr ycbcr = new YCbCr( );

            int offset = imageData.Stride - width * 3;

            // do the job
            unsafe
            {
                byte *p = (byte *)imageData.Scan0.ToPointer( );

                // for each line
                for (int y = 0; y < height; y++)
                {
                    // for each pixel
                    for (int x = 0; x < width; x++, p += 3)
                    {
                        rgb.Red   = p[RGB.R];
                        rgb.Green = p[RGB.G];
                        rgb.Blue  = p[RGB.B];

                        // convert to YCbCr color space
                        AForge.Imaging.ColorConverter.RGB2YCbCr(rgb, ycbcr);

                        yhisto [(int)(ycbcr.Y * 255)]++;
                        cbhisto[(int)((ycbcr.Cb + 0.5) * 255)]++;
                        crhisto[(int)((ycbcr.Cr + 0.5) * 255)]++;

                        pixels++;

                        if ((ycbcr.Y != 0.0) || (ycbcr.Cb != 0.0) || (ycbcr.Cr != 0.0))
                        {
                            yhistoWB [(int)(ycbcr.Y * 255)]++;
                            cbhistoWB[(int)((ycbcr.Cb + 0.5) * 255)]++;
                            crhistoWB[(int)((ycbcr.Cr + 0.5) * 255)]++;

                            pixelsWithoutBlack++;
                        }
                    }
                    p += offset;
                }
            }

            // create histograms
            yHistogram  = new HistogramD(yhisto, new RangeD(0.0, 1.0));
            cbHistogram = new HistogramD(cbhisto, new RangeD(-0.5, 0.5));
            crHistogram = new HistogramD(crhisto, new RangeD(-0.5, 0.5));

            yHistogramWithoutBlack  = new HistogramD(yhistoWB, new RangeD(0.0, 1.0));
            cbHistogramWithoutBlack = new HistogramD(cbhistoWB, new RangeD(-0.5, 0.5));
            crHistogramWithoutBlack = new HistogramD(crhistoWB, new RangeD(-0.5, 0.5));
        }