/// <summary>
        /// Gather statistics about specified image
        /// </summary>
        ///
        /// <param name="imageData">Image data</param>
        ///
        private void ProcessImage(BitmapData imageData)
        {
            // get image dimension
            int width  = imageData.Width;
            int height = imageData.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
                        BaseControls.ImageBox.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 ContinuousHistogram(s, new DoubleRange(0, 1));
            luminance  = new ContinuousHistogram(l, new DoubleRange(0, 1));

            saturationWithoutBlack = new ContinuousHistogram(swb, new DoubleRange(0, 1));
            luminanceWithoutBlack  = new ContinuousHistogram(lwb, new DoubleRange(0, 1));
        }
        private unsafe void ProcessImage(UnmanagedImage image)
        {
            this.CheckSourceFormat(image.PixelFormat);
            int width  = image.Width;
            int height = image.Height;

            this.pixels = this.pixelsWithoutBlack = 0;
            int[] numArray  = new int[0x100];
            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();
            YCbCr ycbcr     = new YCbCr();
            int   num3      = (image.PixelFormat == PixelFormat.Format24bppRgb) ? 3 : 4;
            int   num4      = image.Stride - (width * num3);
            byte *numPtr    = (byte *)image.ImageData.ToPointer();

            for (int i = 0; i < height; i++)
            {
                int num6 = 0;
                while (num6 < width)
                {
                    rgb.Red   = numPtr[2];
                    rgb.Green = numPtr[1];
                    rgb.Blue  = numPtr[0];
                    YCbCr.FromRGB(rgb, ycbcr);
                    numArray[(int)(ycbcr.Y * 255.0)]++;
                    numArray2[(int)((ycbcr.Cb + 0.5) * 255.0)]++;
                    numArray3[(int)((ycbcr.Cr + 0.5) * 255.0)]++;
                    this.pixels++;
                    if (((ycbcr.Y != 0.0) || (ycbcr.Cb != 0.0)) || (ycbcr.Cr != 0.0))
                    {
                        numArray4[(int)(ycbcr.Y * 255.0)]++;
                        numArray5[(int)((ycbcr.Cb + 0.5) * 255.0)]++;
                        numArray6[(int)((ycbcr.Cr + 0.5) * 255.0)]++;
                        this.pixelsWithoutBlack++;
                    }
                    num6++;
                    numPtr += num3;
                }
                numPtr += num4;
            }
            this.yHistogram              = new ContinuousHistogram(numArray, new DoubleRange(0.0, 1.0));
            this.cbHistogram             = new ContinuousHistogram(numArray2, new DoubleRange(-0.5, 0.5));
            this.crHistogram             = new ContinuousHistogram(numArray3, new DoubleRange(-0.5, 0.5));
            this.yHistogramWithoutBlack  = new ContinuousHistogram(numArray4, new DoubleRange(0.0, 1.0));
            this.cbHistogramWithoutBlack = new ContinuousHistogram(numArray5, new DoubleRange(-0.5, 0.5));
            this.crHistogramWithoutBlack = new ContinuousHistogram(numArray6, new DoubleRange(-0.5, 0.5));
        }
Exemple #3
0
        private unsafe void ProcessImage(UnmanagedImage image)
        {
            this.CheckSourceFormat(image.PixelFormat);
            int width  = image.Width;
            int height = image.Height;

            this.pixels = this.pixelsWithoutBlack = 0;
            int[] numArray  = new int[0x100];
            int[] numArray2 = new int[0x100];
            int[] numArray3 = new int[0x100];
            int[] numArray4 = new int[0x100];
            RGB   rgb       = new RGB();
            HSL   hsl       = new HSL();
            int   num3      = (image.PixelFormat == PixelFormat.Format24bppRgb) ? 3 : 4;
            int   num4      = image.Stride - (width * num3);
            byte *numPtr    = (byte *)image.ImageData.ToPointer();

            for (int i = 0; i < height; i++)
            {
                int num6 = 0;
                while (num6 < width)
                {
                    rgb.Red   = numPtr[2];
                    rgb.Green = numPtr[1];
                    rgb.Blue  = numPtr[0];
                    HSL.FromRGB(rgb, hsl);
                    numArray[(int)(hsl.Saturation * 255.0)]++;
                    numArray2[(int)(hsl.Luminance * 255.0)]++;
                    this.pixels++;
                    if (hsl.Luminance != 0.0)
                    {
                        numArray3[(int)(hsl.Saturation * 255.0)]++;
                        numArray4[(int)(hsl.Luminance * 255.0)]++;
                        this.pixelsWithoutBlack++;
                    }
                    num6++;
                    numPtr += num3;
                }
                numPtr += num4;
            }
            this.saturation             = new ContinuousHistogram(numArray, new DoubleRange(0.0, 1.0));
            this.luminance              = new ContinuousHistogram(numArray2, new DoubleRange(0.0, 1.0));
            this.saturationWithoutBlack = new ContinuousHistogram(numArray3, new DoubleRange(0.0, 1.0));
            this.luminanceWithoutBlack  = new ContinuousHistogram(numArray4, new DoubleRange(0.0, 1.0));
        }
        // Gather statistics for the specified image
        private unsafe void ProcessImage(UnmanagedImage image, byte *mask, int maskLineSize)
        {
            // get image dimension
            int width  = image.Width;
            int height = image.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 pixelSize  = (image.PixelFormat == PixelFormat.Format24bppRgb) ? 3 : 4;
            int offset     = image.Stride - width * pixelSize;
            int maskOffset = maskLineSize - width;

            // do the job
            byte *p = (byte *)image.ImageData.ToPointer();

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

                        // convert to YCbCr color space
                        Accord.Imaging.YCbCr.FromRGB(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;
                }
            }
            else
            {
                // for each line
                for (int y = 0; y < height; y++)
                {
                    // for each pixel
                    for (int x = 0; x < width; x++, p += pixelSize, mask++)
                    {
                        if (*mask == 0)
                        {
                            continue;
                        }

                        rgb.Red   = p[RGB.R];
                        rgb.Green = p[RGB.G];
                        rgb.Blue  = p[RGB.B];

                        // convert to YCbCr color space
                        Accord.Imaging.YCbCr.FromRGB(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;
                    mask += maskOffset;
                }
            }

            // create histograms
            yHistogram  = new ContinuousHistogram(yhisto, new Range(0.0f, 1.0f));
            cbHistogram = new ContinuousHistogram(cbhisto, new Range(-0.5f, 0.5f));
            crHistogram = new ContinuousHistogram(crhisto, new Range(-0.5f, 0.5f));

            yHistogramWithoutBlack  = new ContinuousHistogram(yhistoWB, new Range(0.0f, 1.0f));
            cbHistogramWithoutBlack = new ContinuousHistogram(cbhistoWB, new Range(-0.5f, 0.5f));
            crHistogramWithoutBlack = new ContinuousHistogram(crhistoWB, new Range(-0.5f, 0.5f));
        }
Exemple #5
0
        private unsafe void ProcessImage(UnmanagedImage image, byte *mask, int maskLineSize)
        {
            int width  = image.Width;
            int height = image.Height;

            pixels = (pixelsWithoutBlack = 0);
            int[] array  = new int[256];
            int[] array2 = new int[256];
            int[] array3 = new int[256];
            int[] array4 = new int[256];
            RGB   rGB    = new RGB();
            HSL   hSL    = new HSL();
            int   num    = (image.PixelFormat == PixelFormat.Format24bppRgb) ? 3 : 4;
            int   num2   = image.Stride - width * num;
            int   num3   = maskLineSize - width;
            byte *ptr    = (byte *)image.ImageData.ToPointer();

            if (mask == null)
            {
                for (int i = 0; i < height; i++)
                {
                    int num4 = 0;
                    while (num4 < width)
                    {
                        rGB.Red   = ptr[2];
                        rGB.Green = ptr[1];
                        rGB.Blue  = *ptr;
                        HSL.FromRGB(rGB, hSL);
                        array[(int)(hSL.Saturation * 255f)]++;
                        array2[(int)(hSL.Luminance * 255f)]++;
                        pixels++;
                        if ((double)hSL.Luminance != 0.0)
                        {
                            array3[(int)(hSL.Saturation * 255f)]++;
                            array4[(int)(hSL.Luminance * 255f)]++;
                            pixelsWithoutBlack++;
                        }
                        num4++;
                        ptr += num;
                    }
                    ptr += num2;
                }
            }
            else
            {
                for (int j = 0; j < height; j++)
                {
                    int num5 = 0;
                    while (num5 < width)
                    {
                        if (*mask != 0)
                        {
                            rGB.Red   = ptr[2];
                            rGB.Green = ptr[1];
                            rGB.Blue  = *ptr;
                            HSL.FromRGB(rGB, hSL);
                            array[(int)(hSL.Saturation * 255f)]++;
                            array2[(int)(hSL.Luminance * 255f)]++;
                            pixels++;
                            if ((double)hSL.Luminance != 0.0)
                            {
                                array3[(int)(hSL.Saturation * 255f)]++;
                                array4[(int)(hSL.Luminance * 255f)]++;
                                pixelsWithoutBlack++;
                            }
                        }
                        num5++;
                        ptr += num;
                        mask++;
                    }
                    ptr  += num2;
                    mask += num3;
                }
            }
            saturation             = new ContinuousHistogram(array, new Range(0f, 1f));
            luminance              = new ContinuousHistogram(array2, new Range(0f, 1f));
            saturationWithoutBlack = new ContinuousHistogram(array3, new Range(0f, 1f));
            luminanceWithoutBlack  = new ContinuousHistogram(array4, new Range(0f, 1f));
        }
Exemple #6
0
        // Gather statistics for the specified image
        private unsafe void ProcessImage(UnmanagedImage image, byte *mask, int maskLineSize)
        {
            // get image dimension
            int width  = image.Width;
            int height = image.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 pixelSize  = (image.PixelFormat == PixelFormat.Format24bppRgb) ? 3 : 4;
            int offset     = image.Stride - width * pixelSize;
            int maskOffset = maskLineSize - width;

            // do the job
            byte *p = (byte *)image.ImageData.ToPointer();

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

                        // convert to HSL color space
                        Accord.Imaging.HSL.FromRGB(rgb, ref hsl);

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

                        if (hsl.Luminance != 0.0)
                        {
                            swb[(int)(hsl.Saturation * 255)]++;
                            lwb[(int)(hsl.Luminance * 255)]++;
                            pixelsWithoutBlack++;
                        }
                    }
                    p += offset;
                }
            }
            else
            {
                // for each line
                for (int y = 0; y < height; y++)
                {
                    // for each pixel
                    for (int x = 0; x < width; x++, p += pixelSize, mask++)
                    {
                        if (*mask == 0)
                        {
                            continue;
                        }

                        rgb.Red   = p[RGB.R];
                        rgb.Green = p[RGB.G];
                        rgb.Blue  = p[RGB.B];

                        // convert to HSL color space
                        Accord.Imaging.HSL.FromRGB(rgb, ref hsl);

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

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

            // create histograms
            saturation = new ContinuousHistogram(s, new Range(0, 1));
            luminance  = new ContinuousHistogram(l, new Range(0, 1));

            saturationWithoutBlack = new ContinuousHistogram(swb, new Range(0, 1));
            luminanceWithoutBlack  = new ContinuousHistogram(lwb, new Range(0, 1));
        }
        /// <summary>
        /// Gather statistics about specified image
        /// </summary>
        ///
        /// <param name="imageData">Image data</param>
        ///
        private void ProcessImage(BitmapData imageData)
        {
            // get image dimension
            int width  = imageData.Width;
            int height = imageData.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
                        BaseControls.ImageBox.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 ContinuousHistogram(yhisto, new DoubleRange(0.0, 1.0));
            cbHistogram = new ContinuousHistogram(cbhisto, new DoubleRange(-0.5, 0.5));
            crHistogram = new ContinuousHistogram(crhisto, new DoubleRange(-0.5, 0.5));

            yHistogramWithoutBlack  = new ContinuousHistogram(yhistoWB, new DoubleRange(0.0, 1.0));
            cbHistogramWithoutBlack = new ContinuousHistogram(cbhistoWB, new DoubleRange(-0.5, 0.5));
            crHistogramWithoutBlack = new ContinuousHistogram(crhistoWB, new DoubleRange(-0.5, 0.5));
        }
Exemple #8
0
        private void button2_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image == null)
            {
                MessageBox.Show("请先拖拽图片文件到窗口中!");
                return;
            }

            char[]        chars     = new char[] { 'M', 'Q', 'H', 'N', 'O', 'S', '2', 'C', '?', ')', '>', '!', ':', ',', '.' };
            StringBuilder sb        = new StringBuilder();
            string        html_head = "<!DOCTYPE html><html><body style = \"font-family: Monospace;font-size: 2px;line-height: 50%;\">";
            string        html_tail = "</body></html>";

            sb.Append(html_head);

            Bitmap bmp              = (Bitmap)pictureBox1.Image;
            int    p_fac            = Convert.ToInt32(textBox1.Text);  //最大像素个数
            double w_fac            = Convert.ToDouble(textBox3.Text); //宽度因子
            double c_fac            = Convert.ToDouble(textBox2.Text); //对比度因子
            bool   binarization     = false;
            int    binary_threshold = 127;
            int    m     = Math.Max(bmp.Height, (int)(bmp.Width * w_fac));
            double delta = Math.Max(m * 1.0 / p_fac, 1);
            int    w     = (int)((int)(bmp.Width * w_fac) / delta);
            int    h     = (int)(bmp.Height / delta);

            FiltersSequence filter = new FiltersSequence();

            filter.Add(new ResizeNearestNeighbor(w, h));

            if (checkBox1.Checked)
            {
                filter.Add(new HistogramEqualization());
            }

            if (checkBox2.Checked)
            {
                filter.Add(new Sharpen());
            }

            if (checkBox3.Checked)
            {
                binarization = true;
            }

            using (Bitmap newbmp = filter.Apply(bmp))
            {
                if (binarization)
                {
                    ImageStatisticsYCbCr stat = new ImageStatisticsYCbCr(newbmp);
                    ContinuousHistogram  y    = stat.Y;
                    binary_threshold = (int)(y.Mean * 162);
                }
                BitmapData bmpData =
                    newbmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

                unsafe
                {
                    byte *ptr = (byte *)(bmpData.Scan0);
                    int   tmp;
                    for (int y = 0; y < h; y++)
                    {
                        for (int x = 0; x < w; x++)
                        {
                            tmp = ptr[1];
                            tmp = (int)((tmp - 127) * c_fac + 127);
                            tmp = tmp < 0 ? 0 : (tmp > 255 ? 255 : tmp);
                            int l = (int)(0.299 * ptr[2] + 0.587 * tmp + 0.114 * ptr[0]);
                            if (binarization)
                            {
                                l = l > binary_threshold ? 255 : 0;
                            }
                            sb.Append(chars[l * 14 / 255]);
                            ptr += 3;
                        }
                        sb.Append("<br/>");
                        ptr += bmpData.Stride - bmpData.Width * 3;
                    }
                }
                newbmp.UnlockBits(bmpData);
            }
            sb.Append(html_tail);

            string tempFile = Path.GetTempFileName() + ".html";

            File.WriteAllText(tempFile, sb.ToString().Replace("...", ".. "));
            System.Diagnostics.Process.Start(tempFile);
        }
        // Gather statistics for the specified image
        private unsafe void ProcessImage(UnmanagedImage image, byte* mask, int maskLineSize)
        {
            // get image dimension
            int width = image.Width;
            int height = image.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 pixelSize = (image.PixelFormat == PixelFormat.Format24bppRgb) ? 3 : 4;
            int offset = image.Stride - width * pixelSize;
            int maskOffset = maskLineSize - width;

            // do the job
            byte* p = (byte*)image.ImageData.ToPointer();

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

                        // convert to HSL color space
                        Accord.Imaging.HSL.FromRGB(rgb, hsl);

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

                        if (hsl.Luminance != 0.0)
                        {
                            swb[(int)(hsl.Saturation * 255)]++;
                            lwb[(int)(hsl.Luminance * 255)]++;
                            pixelsWithoutBlack++;
                        }
                    }
                    p += offset;
                }
            }
            else
            {
                // for each line
                for (int y = 0; y < height; y++)
                {
                    // for each pixel
                    for (int x = 0; x < width; x++, p += pixelSize, mask++)
                    {
                        if (*mask == 0)
                            continue;

                        rgb.Red = p[RGB.R];
                        rgb.Green = p[RGB.G];
                        rgb.Blue = p[RGB.B];

                        // convert to HSL color space
                        Accord.Imaging.HSL.FromRGB(rgb, hsl);

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

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

            // create histograms
            saturation = new ContinuousHistogram(s, new Range(0, 1));
            luminance = new ContinuousHistogram(l, new Range(0, 1));

            saturationWithoutBlack = new ContinuousHistogram(swb, new Range(0, 1));
            luminanceWithoutBlack = new ContinuousHistogram(lwb, new Range(0, 1));
        }
        // Gather statistics for the specified image
        private unsafe void ProcessImage(UnmanagedImage image, byte* mask, int maskLineSize)
        {
            // get image dimension
            int width = image.Width;
            int height = image.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 pixelSize = (image.PixelFormat == PixelFormat.Format24bppRgb) ? 3 : 4;
            int offset = image.Stride - width * pixelSize;
            int maskOffset = maskLineSize - width;

            // do the job
            byte* p = (byte*)image.ImageData.ToPointer();

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

                        // convert to YCbCr color space
                        Accord.Imaging.YCbCr.FromRGB(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;
                }
            }
            else
            {
                // for each line
                for (int y = 0; y < height; y++)
                {
                    // for each pixel
                    for (int x = 0; x < width; x++, p += pixelSize, mask++)
                    {
                        if (*mask == 0)
                            continue;

                        rgb.Red = p[RGB.R];
                        rgb.Green = p[RGB.G];
                        rgb.Blue = p[RGB.B];

                        // convert to YCbCr color space
                        Accord.Imaging.YCbCr.FromRGB(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;
                    mask += maskOffset;
                }
            }

            // create histograms
            yHistogram = new ContinuousHistogram(yhisto, new Range(0.0f, 1.0f));
            cbHistogram = new ContinuousHistogram(cbhisto, new Range(-0.5f, 0.5f));
            crHistogram = new ContinuousHistogram(crhisto, new Range(-0.5f, 0.5f));

            yHistogramWithoutBlack = new ContinuousHistogram(yhistoWB, new Range(0.0f, 1.0f));
            cbHistogramWithoutBlack = new ContinuousHistogram(cbhistoWB, new Range(-0.5f, 0.5f));
            crHistogramWithoutBlack = new ContinuousHistogram(crhistoWB, new Range(-0.5f, 0.5f));
        }
Exemple #11
0
        private unsafe void ProcessImage(UnmanagedImage image, byte *mask, int maskLineSize)
        {
            int width  = image.Width;
            int height = image.Height;

            pixels = (pixelsWithoutBlack = 0);
            int[] array  = new int[256];
            int[] array2 = new int[256];
            int[] array3 = new int[256];
            int[] array4 = new int[256];
            int[] array5 = new int[256];
            int[] array6 = new int[256];
            RGB   rGB    = new RGB();
            YCbCr yCbCr  = new YCbCr();
            int   num    = (image.PixelFormat == PixelFormat.Format24bppRgb) ? 3 : 4;
            int   num2   = image.Stride - width * num;
            int   num3   = maskLineSize - width;
            byte *ptr    = (byte *)image.ImageData.ToPointer();

            if (mask == null)
            {
                for (int i = 0; i < height; i++)
                {
                    int num4 = 0;
                    while (num4 < width)
                    {
                        rGB.Red   = ptr[2];
                        rGB.Green = ptr[1];
                        rGB.Blue  = *ptr;
                        YCbCr.FromRGB(rGB, yCbCr);
                        array[(int)(yCbCr.Y * 255f)]++;
                        array2[(int)(((double)yCbCr.Cb + 0.5) * 255.0)]++;
                        array3[(int)(((double)yCbCr.Cr + 0.5) * 255.0)]++;
                        pixels++;
                        if ((double)yCbCr.Y != 0.0 || (double)yCbCr.Cb != 0.0 || (double)yCbCr.Cr != 0.0)
                        {
                            array4[(int)(yCbCr.Y * 255f)]++;
                            array5[(int)(((double)yCbCr.Cb + 0.5) * 255.0)]++;
                            array6[(int)(((double)yCbCr.Cr + 0.5) * 255.0)]++;
                            pixelsWithoutBlack++;
                        }
                        num4++;
                        ptr += num;
                    }
                    ptr += num2;
                }
            }
            else
            {
                for (int j = 0; j < height; j++)
                {
                    int num5 = 0;
                    while (num5 < width)
                    {
                        if (*mask != 0)
                        {
                            rGB.Red   = ptr[2];
                            rGB.Green = ptr[1];
                            rGB.Blue  = *ptr;
                            YCbCr.FromRGB(rGB, yCbCr);
                            array[(int)(yCbCr.Y * 255f)]++;
                            array2[(int)(((double)yCbCr.Cb + 0.5) * 255.0)]++;
                            array3[(int)(((double)yCbCr.Cr + 0.5) * 255.0)]++;
                            pixels++;
                            if ((double)yCbCr.Y != 0.0 || (double)yCbCr.Cb != 0.0 || (double)yCbCr.Cr != 0.0)
                            {
                                array4[(int)(yCbCr.Y * 255f)]++;
                                array5[(int)(((double)yCbCr.Cb + 0.5) * 255.0)]++;
                                array6[(int)(((double)yCbCr.Cr + 0.5) * 255.0)]++;
                                pixelsWithoutBlack++;
                            }
                        }
                        num5++;
                        ptr += num;
                        mask++;
                    }
                    ptr  += num2;
                    mask += num3;
                }
            }
            yHistogram              = new ContinuousHistogram(array, new Range(0f, 1f));
            cbHistogram             = new ContinuousHistogram(array2, new Range(-0.5f, 0.5f));
            crHistogram             = new ContinuousHistogram(array3, new Range(-0.5f, 0.5f));
            yHistogramWithoutBlack  = new ContinuousHistogram(array4, new Range(0f, 1f));
            cbHistogramWithoutBlack = new ContinuousHistogram(array5, new Range(-0.5f, 0.5f));
            crHistogramWithoutBlack = new ContinuousHistogram(array6, new Range(-0.5f, 0.5f));
        }