RGB components.

The class encapsulates RGB color components.

PixelFormat.Format24bppRgb actually means BGR format.

Пример #1
0
 public LayerProperties()
 {
     colorReplacement = Color.White;
     hslReplacementColor = new HSL();
     RGB c = new RGB(colorReplacement);
     AForge.Imaging.ColorConverter.RGB2HSL(c,hslReplacementColor);
 }
Пример #2
0
        public static bool CheckLines(Bitmap image, Color filterColor)
        {
            EuclideanColorFiltering ColorFilter = new EuclideanColorFiltering();

            // set center colour and radius
            AForge.Imaging.RGB color = new AForge.Imaging.RGB(filterColor.R, filterColor.G, filterColor.B, filterColor.A);
            ColorFilter.CenterColor = color;
            ColorFilter.Radius      = 100;
            // Apply the filter
            ColorFilter.ApplyInPlace(image);

            // Define the Blob counter and use it!
            BlobCounter blobCounter = new BlobCounter();

            blobCounter.MinWidth    = 5;
            blobCounter.MinHeight   = 5;
            blobCounter.FilterBlobs = true;
            //blobCounter.ObjectsOrder = ObjectsOrder.Size;
            blobCounter.ProcessImage(image);
            System.Drawing.Rectangle[] rects = blobCounter.GetObjectsRectangles();
            if (rects.Length > 0)
            {
                return(true);
            }
            return(false);
        }
Пример #3
0
 public void SetColor(byte r, byte g, byte b)
 {
     RGB rgbcolor = new RGB(r,g,b);
     this.color = YCbCr.FromRGB(rgbcolor);
     this.colorFilter.Cb = new AForge.Range(this.color.Cb - 0.02f, this.color.Cb + 0.02f);
     this.colorFilter.Cr = new AForge.Range(this.color.Cr - 0.02f, this.color.Cr + 0.02f);
     this.colorFilter.Y = new AForge.Range(this.color.Y - 0.05f, this.color.Y + 0.05f);
     this.applyFilters = true;
 }
Пример #4
0
        public LayerColor(HSL _color, IntRange _hue_range, DoubleRange _sat_range, DoubleRange _lum_range)
        {
            color = _color;
            hue_range = _hue_range;
            sat_range = _sat_range;
            lum_range = _lum_range;

            RGB rgb = new RGB();
            AForge.Imaging.ColorConverter.HSL2RGB(color, rgb);
            RGBColor = rgb.Color;
        }
Пример #5
0
        /// <summary>
        /// Convert from RGB to HSL color space.
        /// </summary>
        /// 
        /// <param name="rgb">Source color in <b>RGB</b> color space.</param>
        /// <param name="hsl">Destination color in <b>HSL</b> color space.</param>
        /// 
        /// <remarks><para>See <a href="http://en.wikipedia.org/wiki/HSI_color_space#Conversion_from_RGB_to_HSL_or_HSV">HSL and HSV Wiki</a>
        /// for information about the algorithm to convert from RGB to HSL.</para></remarks>
        /// 
        public static void FromRGB( RGB rgb, HSL hsl )
        {
            float r = ( rgb.Red   / 255.0f );
            float g = ( rgb.Green / 255.0f );
            float b = ( rgb.Blue  / 255.0f );

            float min = Math.Min( Math.Min( r, g ), b );
            float max = Math.Max( Math.Max( r, g ), b );
            float delta = max - min;

            // get luminance value
            hsl.Luminance = ( max + min ) / 2;

            if ( delta == 0 )
            {
                // gray color
                hsl.Hue = 0;
                hsl.Saturation = 0.0f;
            }
            else
            {
                // get saturation value
                hsl.Saturation = ( hsl.Luminance <= 0.5 ) ? ( delta / ( max + min ) ) : ( delta / ( 2 - max - min ) );

                // get hue value
                float hue;

                if ( r == max )
                {
                    hue = ( ( g - b ) / 6 ) / delta;
                }
                else if ( g == max )
                {
                    hue = ( 1.0f / 3 ) + ( ( b - r ) / 6 ) / delta;
                }
                else
                {
                    hue = ( 2.0f / 3 ) + ( ( r - g ) / 6 ) / delta;
                }

                // correct hue if needed
                if ( hue < 0 )
                    hue += 1;
                if ( hue > 1 )
                    hue -= 1;

                hsl.Hue = (int) ( hue * 360 );
            }
        }
Пример #6
0
 //int redMin = 130;
 //int redMax = 255;
 public MotionDetector(int sh, int sw)
 {
     screenHeight=sh;
     screenWidth=sw;
     GetCameras();
     colorFilter.Red = new IntRange(Convert.ToInt32(130), Convert.ToInt32(255));
     colorFilter.Green = new IntRange(Convert.ToInt32(30), Convert.ToInt32(120));
     colorFilter.Blue = new IntRange(Convert.ToInt32(30), Convert.ToInt32(120));
     AForge.Imaging.RGB fc = new RGB(Color.White);
     //colorFilter.FillColor = fc;
     blobCounter.MinWidth =10;
     blobCounter.MinHeight = 10;
     blobCounter.MaxWidth =200;
     blobCounter.MaxHeight =200;
     blobCounter.FilterBlobs = true;
     blobCounter.ObjectsOrder = ObjectsOrder.Size;
 }
Пример #7
0
        // Convert from RGB to HSL color space
        public static void RGB2HSL(RGB rgb, HSL hsl)
        {
            double	r = (rgb.Red / 255.0);
            double	g = (rgb.Green / 255.0);
            double	b = (rgb.Blue / 255.0);

            double	min = Math.Min(Math.Min(r, g), b);
            double	max = Math.Max(Math.Max(r, g), b);
            double	delta = max - min;

            // get luminance value
            hsl.Luminance = (max + min) / 2;

            if (delta == 0)
            {
                // gray color
                hsl.Hue = 0;
                hsl.Saturation = 0.0;
            }
            else
            {
                // get saturation value
                hsl.Saturation = (hsl.Luminance < 0.5) ? (delta / (max + min)) : (delta / (2 - max - min));

                // get hue value
                double	del_r = (((max - r) / 6) + (delta / 2)) / delta;
                double	del_g = (((max - g) / 6) + (delta / 2)) / delta;
                double	del_b = (((max - b) / 6) + (delta / 2)) / delta;
                double	hue;

                if (r == max)
                    hue = del_b - del_g;
                else if (g == max)
                    hue = (1.0 / 3) + del_r - del_b;
                else
                    hue = (2.0 / 3) + del_g - del_r;

                // correct hue if needed
                if (hue < 0)
                    hue += 1;
                if (hue > 1)
                    hue -= 1;

                hsl.Hue = (int) (hue * 360);
            }
        }
        public void markerBasedDetectorTest()
        {
            MarkerBasedDetectorFactory factory = new MarkerBasedDetectorFactory();
            MarkerBasedDetector sut = (MarkerBasedDetector)factory.CreateDetector();
            sut.ResetSettings();

            ColorDiscriminator discr = new ColorDiscriminator();
            RGB color = new RGB(237, 28, 36);
            discr.RGB_color= color;
            LocationSourceManager.Instance.Shutdown();
            LocationSourceManager.Instance.createLocationSource("foo", discr);

            Bitmap frame = new Bitmap(Properties.Resources.markerBasedDetectorTestFrame);
            sut.Detect(ref frame);
            IntPoint pos = LocationSourceManager.Instance.LocationSources[0].ScreenPos;
            Assert.AreEqual(87, pos.X);
            Assert.AreEqual(92, pos.Y);
        }
Пример #9
0
        // Convert from HSL to RGB color space
        public static void HSL2RGB(HSL hsl, RGB rgb)
        {
            if (hsl.Saturation == 0)
            {
                // gray values
                rgb.Red = rgb.Green = rgb.Blue = (byte) (hsl.Luminance * 255);
            }
            else
            {
                double	v1, v2;
                double	hue = (double) hsl.Hue / 360;

                v2 = (hsl.Luminance < 0.5) ? (hsl.Luminance * (1 + hsl.Saturation)) : ((hsl.Luminance + hsl.Saturation) - (hsl.Luminance * hsl.Saturation));
                v1 = 2 * hsl.Luminance - v2;

                rgb.Red		= (byte)(255 * Hue_2_RGB(v1, v2, hue + (1.0 / 3)));
                rgb.Green	= (byte)(255 * Hue_2_RGB(v1, v2, hue));
                rgb.Blue	= (byte)(255 * Hue_2_RGB(v1, v2, hue - (1.0 / 3)));
            }
        }
Пример #10
0
        // On mouse position over image changed
        private void document_MouseImagePosition(object sender, SelectionEventArgs e)
        {
            if (e.Location.X >= 0)
            {
                this.PositionToolStripStatusLabel.Text = string.Format(" ({0}, {1})", e.Location.X, e.Location.Y);

                // get current color
                Bitmap image = ((ImageHandlerForm)sender).Image;
                if (image.PixelFormat == PixelFormat.Format24bppRgb)
                {
                    Color color = image.GetPixel(e.Location.X, e.Location.Y);
                    RGB rgb = new RGB(color);
                    YCbCr ycbcr = new YCbCr();

                    AForge.Imaging.ColorConverter.RGB2YCbCr(rgb, ycbcr);

                    // RGB
                    this.colorPanel.Text = string.Format("RGB: {0}, {1}, {2}", color.R, color.G, color.B);
                    // HSL
                    this.hslPanel.Text = string.Format("HSL: {0}, {1:F3}, {2:F3}", (int)color.GetHue(), color.GetSaturation(), color.GetBrightness());
                    // YCbCr
                    this.ycbcrPanel.Text = string.Format("YCbCr: {0:F3}, {1:F3}, {2:F3}", ycbcr.Y, ycbcr.Cb, ycbcr.Cr);
                }
                else if (image.PixelFormat == PixelFormat.Format8bppIndexed)
                {
                    Color color = image.GetPixel(e.Location.X, e.Location.Y);
                    this.colorPanel.Text = "Gray: " + color.R.ToString();
                    this.hslPanel.Text = "";
                    this.ycbcrPanel.Text = "";
                }
            }
            else
            {
                this.PositionToolStripStatusLabel.Text = "";
                this.colorPanel.Text = "";
                this.hslPanel.Text = "";
                this.ycbcrPanel.Text = "";
            }
        }
        // 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 ) );
        }
        public Bitmap ProcessFrame(Bitmap frame)
        {
            var filter = new EuclideanColorFiltering();
            _color = new RGB(GlobalVar.Red, GlobalVar.Blue, GlobalVar.Green);
            filter.CenterColor = _color;
            filter.Radius = 20;
            filter.ApplyInPlace(frame);

            //var filter = new Grayscale(0.2125, 0.7154, 0.0721);
            //var thresh = new Threshold(thresh_val);
            //frame = filter.Apply(frame);
            //thresh.ApplyInPlace(frame);

            bc.ProcessImage(frame);
            Rectangle[] rects = bc.GetObjectsRectangles();

            if (rects.Length > 0)
            {
                AForge.Point closest = new AForge.Point(100000, 100000);

                foreach (var r in rects)
                {
                    var p = new AForge.Point(r.Left + r.Width / 2, r.Top + r.Height / 2);

                    var d2 = center.SquaredDistanceTo(closest);
                    var d1 = center.SquaredDistanceTo(p);

                    if (d1 < d2)
                    {
                        closest = p;
                    }
                }

                var closestDistance = center.SquaredDistanceTo(closest);
                var shouldFire = closestDistance < 100; //start firing when close (defualt=20)
                var delta = closest - center;
             //   _controller.Update(delta.X, delta.Y, shouldFire);

                GlobalVar.objectX = closest.X;
                GlobalVar.objectY = closest.Y;
                //send command to move

                if (GlobalVar.calibrationMode)
                {
                    if (GlobalVar.calibrationMoveComplete)
                    {

                        GlobalVar.xRatio = delta.X / 100;
                        //GlobalVar.xRatio = delta.X;

                        if (GlobalVar.xRatio < 0) { GlobalVar.xRatio *= -1; }
                    }
                    else
                    {
                        _controller.Calibrate();
                        GlobalVar.calibrationMoveComplete = true;
                    }
                }
                else
                {
                    _controller.Update(delta.X, delta.Y, shouldFire);
                    alreadyZeroized = false;
                }

                var g = Graphics.FromImage(frame);
                using (Pen p = new Pen(Color.Red))
                {
                    foreach (Rectangle r in rects)
                    {
                        g.DrawRectangle(p, r);
                        g.DrawString("+", new Font("Consolas", 10), Brushes.Red, r.X, r.Y);
                    }
                }
                using (Pen p = new Pen(Color.Green))
                {
                    g.DrawRectangle(p, closest.X - 2, closest.Y - 2, 5, 5);
                }

                GlobalVar.nothingTime = 0;
            }
            else
            {
                //     _controller.Hunt();
                GlobalVar.nothingTimerEnabled = true;
                if (GlobalVar.nothingTime == 150)
                {//10 = 1 second
                    if (!alreadyZeroized)
                    {
                        _controller.FastZeroize();
                        alreadyZeroized = true;
                    }
                }
                if (GlobalVar.nothingTime == 300)
                {//10 = 1 second
                    //GlobalVar.nothingTime = 0;

                    _controller.SlowZeroize();
                    alreadyZeroized = true;
                    GlobalVar.nothingTime = 0;

                }
                /*
                if (GlobalVar.nothingTime == 45)
                {//10 = 1 second
                    _controller.Hunt();
                    alreadyZeroized = false;
                }
                if (GlobalVar.nothingTime == 55)
                    _controller.Hunt();

                if (GlobalVar.nothingTime == 65)
                    _controller.Hunt();

                if (GlobalVar.nothingTime == 75)
                    _controller.Hunt();

                if (GlobalVar.nothingTime == 85)
                    _controller.Hunt();

                if (GlobalVar.nothingTime == 90)
                  {
                    GlobalVar.nothingTime = 0;
                     _controller.Hunt();
                  }

                 */

            }

            return frame;
        }
 public void SetColor(Color color)
 {
     _color = new RGB(color);
 }
Пример #14
0
        private void fillColorCombo_SelectedIndexChanged(object sender, EventArgs e)
        {
            RGB selected_color=new RGB((Color)fillColorCombo.Items[fillColorCombo.SelectedIndex]);
            HSL hsl = new HSL();
            AForge.Imaging.ColorConverter.RGB2HSL(selected_color, hsl);
            filter.FillColor = hsl;

            filterPreview.RefreshFilter();
        }
Пример #15
0
        /// <summary>
        /// Convert from RGB to YCbCr color space (Rec 601-1 specification). 
        /// </summary>
        /// 
        /// <param name="rgb">Source color in <b>RGB</b> color space.</param>
        /// <param name="ycbcr">Destination color in <b>YCbCr</b> color space.</param>
        /// 
        public static void FromRGB( RGB rgb, YCbCr ycbcr )
        {
            float r = (float) rgb.Red / 255;
            float g = (float) rgb.Green / 255;
            float b = (float) rgb.Blue / 255;

            ycbcr.Y =  (float) (  0.2989 * r + 0.5866 * g + 0.1145 * b );
            ycbcr.Cb = (float) ( -0.1687 * r - 0.3313 * g + 0.5000 * b );
            ycbcr.Cr = (float) (  0.5000 * r - 0.4184 * g - 0.0816 * b );
        }
Пример #16
0
        // Paint control
        protected override void OnPaint( PaintEventArgs pe )
        {
            Graphics g = pe.Graphics;
            Rectangle rc = this.ClientRectangle;
            Rectangle rcPie;
            Brush brush;
            RGB rgb = new RGB( );
            HSL hsl = new HSL( );

            // get pie rectangle
            rcPie = new Rectangle( 4, 4, Math.Min( rc.Right, rc.Bottom ) - 8, Math.Min( rc.Right, rc.Bottom ) - 8 );

            // init HSL value
            hsl.Luminance = 0.5;
            hsl.Saturation = 1.0;

            if ( type == HuePickerType.Value )
            {
                // draw HSL pie
                for ( int i = 0; i < 360; i++ )
                {
                    hsl.Hue = i;
                    // convert from HSL to RGB
                    AForge.Imaging.ColorConverter.HSL2RGB( hsl, rgb );
                    // create brush
                    brush = new SolidBrush( rgb.Color );
                    // draw one hue value
                    g.FillPie( brush, rcPie, -i, -1 );

                    brush.Dispose( );
                }
            }
            else
            {
                // draw HSL pie
                for ( int i = 0; i < 360; i++ )
                {
                    if ( ( ( min < max ) && ( i >= min ) && ( i <= max ) ) ||
                        ( ( min > max ) && ( ( i >= min ) || ( i <= max ) ) ) )
                    {
                        hsl.Hue = i;
                        // convert from HSL to RGB
                        AForge.Imaging.ColorConverter.HSL2RGB( hsl, rgb );
                        // create brush
                        brush = new SolidBrush( rgb.Color );
                    }
                    else
                    {
                        brush = new SolidBrush( Color.FromArgb( 128, 128, 128 ) );
                    }

                    // draw one hue value
                    g.FillPie( brush, rcPie, -i, -1 );

                    brush.Dispose( );
                }
            }

            //
            double halfWidth = (double) rcPie.Width / 2;
            double angleRad = -min * Math.PI / 180;
            double angleCos = Math.Cos( angleRad );
            double angleSin = Math.Sin( angleRad );

            double x = halfWidth * angleCos;
            double y = halfWidth * angleSin;

            ptCenter.X = rcPie.Left + (int) ( halfWidth );
            ptCenter.Y = rcPie.Top + (int) ( halfWidth );
            ptMin.X = rcPie.Left + (int) ( halfWidth + x );
            ptMin.Y = rcPie.Top + (int) ( halfWidth + y );

            // draw MIN pointer
            g.FillEllipse( blackBrush,
                rcPie.Left + (int) ( halfWidth + x ) - 4,
                rcPie.Top + (int) ( halfWidth + y ) - 4,
                8, 8 );
            g.DrawLine( blackPen, ptCenter, ptMin );

            // check picker type
            if ( type == HuePickerType.Region )
            {
                angleRad = -max * Math.PI / 180;
                angleCos = Math.Cos( angleRad );
                angleSin = Math.Sin( angleRad );

                x = halfWidth * angleCos;
                y = halfWidth * angleSin;

                ptMax.X = rcPie.Left + (int) ( halfWidth + x );
                ptMax.Y = rcPie.Top + (int) ( halfWidth + y );

                // draw MAX pointer
                g.FillEllipse( whiteBrush,
                    rcPie.Left + (int) ( halfWidth + x ) - 4,
                    rcPie.Top + (int) ( halfWidth + y ) - 4,
                    8, 8 );
                g.DrawLine( whitePen, ptCenter, ptMax );
            }

            base.OnPaint( pe );
        }
        // 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
                        AForge.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
                        AForge.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));
        }
Пример #18
0
        /// <summary>
        /// Convert from YCbCr to RGB color space.
        /// </summary>
        /// 
        /// <param name="ycbcr">Source color in <b>YCbCr</b> color space.</param>
        /// <param name="rgb">Destination color in <b>RGB</b> color spacs.</param>
        /// 
        public static void ToRGB( YCbCr ycbcr, RGB rgb )
        {
            // don't warry about zeros. compiler will remove them
            float r = Math.Max( 0.0f, Math.Min( 1.0f, (float) ( ycbcr.Y + 0.0000 * ycbcr.Cb + 1.4022 * ycbcr.Cr ) ) );
            float g = Math.Max( 0.0f, Math.Min( 1.0f, (float) ( ycbcr.Y - 0.3456 * ycbcr.Cb - 0.7145 * ycbcr.Cr ) ) );
            float b = Math.Max( 0.0f, Math.Min( 1.0f, (float) ( ycbcr.Y + 1.7710 * ycbcr.Cb + 0.0000 * ycbcr.Cr ) ) );

            rgb.Red   = (byte) ( r * 255 );
            rgb.Green = (byte) ( g * 255 );
            rgb.Blue  = (byte) ( b * 255 );
        }
Пример #19
0
        // Gather statistics for the specified image
        private unsafe void ProcessImage(UnmanagedImage image, byte *mask, int maskLineSize)
        {
            // get image dimension
            var width  = image.Width;
            var height = image.Height;

            this.pixels = this.pixelsWithoutBlack = 0;

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

            var pixelSize  = (image.PixelFormat == PixelFormat.Format24bppRgb) ? 3 : 4;
            var offset     = image.Stride - width * pixelSize;
            var maskOffset = maskLineSize - width;

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

            if (mask == null)
            {
                // for each line
                for (var y = 0; y < height; y++)
                {
                    // for each pixel
                    for (var 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
                        AForge.Imaging.HSL.FromRGB(rgb, hsl);

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

                        if (hsl.Luminance != 0.0)
                        {
                            swb[(int)(hsl.Saturation * 255)]++;
                            lwb[(int)(hsl.Luminance * 255)]++;
                            this.pixelsWithoutBlack++;
                        }
                    }
                    p += offset;
                }
            }
            else
            {
                // for each line
                for (var y = 0; y < height; y++)
                {
                    // for each pixel
                    for (var 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
                        AForge.Imaging.HSL.FromRGB(rgb, hsl);

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

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

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

            this.saturationWithoutBlack = new ContinuousHistogram(swb, new Range(0, 1));
            this.luminanceWithoutBlack  = new ContinuousHistogram(lwb, new Range(0, 1));
        }
Пример #20
0
 private void fillColorCombo_SelectedIndexChanged(object sender, EventArgs e)
 {
     RGB selected_color = new RGB((Color)fillColorCombo.Items[fillColorCombo.SelectedIndex]);
     filter.FillColor = selected_color;
     filterPreview.RefreshFilter();
 }
Пример #21
0
        /// <summary>
        ///   Creates a color histogram discarding low intensity colors
        /// </summary>
        /// 
        private unsafe float[] createHistogram(UnmanagedImage frame, Rectangle area)
        {
            int width = frame.Width;
            int height = frame.Height;
            int stride = frame.Stride;
            int pixelSize = Bitmap.GetPixelFormatSize(frame.PixelFormat) / 8;
            int offset = stride - area.Width * pixelSize;
            float[] histogram = new float[histogramSize];

            // stay inside the image
            int areaX = Math.Max(area.X, 0);
            int areaY = Math.Max(area.Y, 0);
            int areaWidth = Math.Min(area.Width, width - areaX);
            int areaHeight = Math.Min(area.Height, height - areaY);


            if (mode == CamshiftMode.HSL)
            {
                // Process as HSL
                HSL hsl = new HSL();
                RGB rgb = new RGB();

                byte* src = (byte*)frame.ImageData.ToPointer() + areaX * pixelSize + areaY * stride;
                for (int y = 0; y < areaHeight; y++)
                {
                    for (int x = 0; x < areaWidth; x++, src += 3)
                    {
                        rgb.Red = (*(src + RGB.R));
                        rgb.Green = (*(src + RGB.G));
                        rgb.Blue = (*(src + RGB.B));

                        AForge.Imaging.HSL.FromRGB(rgb, hsl);

                        if ((hsl.Saturation >= hslSaturation.Min) && (hsl.Saturation <= hslSaturation.Max) &&
                            (hsl.Luminance >= hslLuminance.Min) && (hsl.Luminance <= hslLuminance.Max))
                            histogram[hsl.Hue] += 1;
                    }
                    src += offset;
                }
            }
            else if (mode == CamshiftMode.Mixed)
            {
                // Process in mixed mode
                HSL hsl = new HSL();
                RGB rgb = new RGB();

                byte* src = (byte*)frame.ImageData.ToPointer() + areaX * pixelSize + areaY * stride;
                for (int y = 0; y < areaHeight; y++)
                {
                    for (int x = 0; x < areaWidth; x++, src += 3)
                    {
                        rgb.Red = (*(src + RGB.R));
                        rgb.Green = (*(src + RGB.G));
                        rgb.Blue = (*(src + RGB.B));

                        AForge.Imaging.HSL.FromRGB(rgb, hsl);

                        if ((hsl.Saturation >= hslSaturation.Min) && (hsl.Saturation <= hslSaturation.Max) &&
                            (hsl.Luminance >= hslLuminance.Min) && (hsl.Luminance <= hslLuminance.Max))
                            histogram[(int)(hsl.Hue * 10 + hsl.Saturation * 100 + hsl.Luminance * 10)] += 1;
                    }
                    src += offset;
                }
            }
            else
            {
                // Process as RGB
                byte* src = (byte*)frame.ImageData.ToPointer() + areaX * pixelSize + areaY * stride;
                for (int y = 0; y < areaHeight; y++)
                {
                    for (int x = 0; x < areaWidth; x++, src += 3)
                    {
                        // (small values are discarded)
                        int r = (int)(*(src + RGB.R)) >> 4;
                        int g = (int)(*(src + RGB.G)) >> 4;
                        int b = (int)(*(src + RGB.B)) >> 4;
                        histogram[256 * r + 16 * g + b] += 1;
                    }
                    src += offset;
                }
            }

            return histogram;
        }
Пример #22
0
        /// <summary>
        ///   Image histogram back-projection.
        /// </summary>
        /// 
        private unsafe void generateBackprojectionMap(UnmanagedImage frame, float[] ratioHistogram)
        {
            int width = frame.Width;
            int height = frame.Height;
            int stride = frame.Stride;
            int pixelSize = Bitmap.GetPixelFormatSize(frame.PixelFormat) / 8;
            int offset = stride - width * pixelSize;

            fixed (float* map_ptr = map)
            {
                byte* src = (byte*)frame.ImageData.ToPointer();
                float* dst = map_ptr;

                if (mode == CamshiftMode.HSL)
                {
                    // Process as HSL
                    HSL hsl = new HSL();
                    RGB rgb = new RGB();

                    for (int y = 0; y < height; y++)
                    {
                        for (int x = 0; x < width; x++, src += pixelSize, dst++)
                        {
                            // RGB
                            rgb.Red = (*(src + RGB.R));
                            rgb.Green = (*(src + RGB.G));
                            rgb.Blue = (*(src + RGB.B));

                            // Transform into HSL
                            AForge.Imaging.HSL.FromRGB(rgb, hsl);

                            if ((hsl.Saturation >= hslSaturation.Min) && (hsl.Saturation <= hslSaturation.Max) &&
                                (hsl.Luminance >= hslLuminance.Min) && (hsl.Luminance <= hslLuminance.Max))
                                *dst = ratioHistogram[hsl.Hue];
                            else *dst = 0;
                        }
                        src += offset;
                    }
                }
                else if (mode == CamshiftMode.Mixed)
                {
                    // Process in mixed mode
                    HSL hsl = new HSL();
                    RGB rgb = new RGB();

                    for (int y = 0; y < height; y++)
                    {
                        for (int x = 0; x < width; x++, src += pixelSize, dst++)
                        {
                            // RGB
                            rgb.Red = (*(src + RGB.R));
                            rgb.Green = (*(src + RGB.G));
                            rgb.Blue = (*(src + RGB.B));

                            // Transform into HSL
                            AForge.Imaging.HSL.FromRGB(rgb, hsl);

                            if ((hsl.Saturation >= hslSaturation.Min) && (hsl.Saturation <= hslSaturation.Max) &&
                                (hsl.Luminance >= hslLuminance.Min) && (hsl.Luminance <= hslLuminance.Max))
                                *dst = ratioHistogram[(int)(hsl.Hue * 10 + hsl.Saturation * 100 + hsl.Luminance * 10)];
                            else *dst = 0;
                        }
                        src += offset;
                    }
                }
                else
                {
                    // Process as RGB
                    for (int y = 0; y < height; y++)
                    {
                        for (int x = 0; x < width; x++, src += pixelSize, dst++)
                        {
                            // RGB
                            int r = (int)(*(src + RGB.R)) >> 4;
                            int g = (int)(*(src + RGB.G)) >> 4;
                            int b = (int)(*(src + RGB.B)) >> 4;
                            *dst = ratioHistogram[256 * r + 16 * g + b];
                        }
                        src += offset;
                    }
                }
            }
        }
Пример #23
0
        // On mouse position over image changed
        private void document_MouseImagePosition(object sender, SelectionEventArgs e)
        {
            if (e.Location.X >= 0)
            {
                this.selectionPanel.Text = string.Format( "({0}, {1})", e.Location.X, e.Location.Y );

                // get current color
                Bitmap image = ((ImageDoc) sender).Image;
                if (image.PixelFormat == PixelFormat.Format24bppRgb)
                {
                    Color	color = image.GetPixel(e.Location.X, e.Location.Y);
                    RGB		rgb = new RGB( color );
                    YCbCr	ycbcr = new YCbCr( );

                    AForge.Imaging.ColorConverter.RGB2YCbCr( rgb, ycbcr );

                    // RGB
                    this.colorPanel.Text = string.Format( "RGB: {0}; {1}; {2}", color.R, color.G, color.B );
                    // HSL
                    this.hslPanel.Text = string.Format( "HSL: {0}; {1:F3}; {2:F3}", (int) color.GetHue(), color.GetSaturation(), color.GetBrightness() );

                    // YCbCr
                    this.ycbcrPanel.Text = string.Format( "YCbCr: {0:F3}; {1:F3}; {2:F3}", ycbcr.Y, ycbcr.Cb, ycbcr.Cr );

                    //SS: отображение области для расчета среднего значения цвета
                    Rectangle avArea=((ImageDoc)sender).AverageArea;
                    if (!avArea.IsEmpty)
                        this.avColorPanel.Text = string.Format("AVERAGE: [{0}; {1}] - [{2}; {3}]", avArea.Left, avArea.Top, avArea.Right, avArea.Bottom);
                    else

                        this.avColorPanel.Text = "";

                    if (((ImageDoc)sender).SelectingArea)
                        this.infoPanel.Text = "Now select area or press right mouse button to cancel";
                    else if (((ImageDoc)sender).SelectingPoint || ((ImageDoc)sender).SelectingLayerPoint)
                        this.infoPanel.Text = "Now pick a point or press right mouse button to cancel";
                    else
                        this.infoPanel.Text = "";

                }
                else if (image.PixelFormat == PixelFormat.Format8bppIndexed)
                {
                    Color color = image.GetPixel(e.Location.X, e.Location.Y);
                    this.colorPanel.Text	= "Gray: " + color.R.ToString();
                    this.hslPanel.Text		= "";
                    this.ycbcrPanel.Text	= "";
                }
            }
            else
            {
                this.selectionPanel.Text	= "";
                this.colorPanel.Text		= "";
                this.hslPanel.Text			= "";
                this.ycbcrPanel.Text		= "";
            }
        }
Пример #24
0
 /// <summary>
 /// Convert from RGB to HSL color space.
 /// </summary>
 /// 
 /// <param name="rgb">Source color in <b>RGB</b> color space.</param>
 /// 
 /// <returns>Returns <see cref="HSL"/> instance, which represents converted color value.</returns>
 /// 
 public static HSL FromRGB( RGB rgb )
 {
     HSL hsl = new HSL( );
     FromRGB( rgb, hsl );
     return hsl;
 }
Пример #25
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));
        }
Пример #26
0
 /// <summary>
 /// Convert from RGB to YCbCr color space (Rec 601-1 specification).
 /// </summary>
 /// 
 /// <param name="rgb">Source color in <b>RGB</b> color space.</param>
 /// 
 /// <returns>Returns <see cref="YCbCr"/> instance, which represents converted color value.</returns>
 /// 
 public static YCbCr FromRGB( RGB rgb )
 {
     YCbCr ycbcr = new YCbCr( );
     FromRGB( rgb, ycbcr );
     return ycbcr;
 }
Пример #27
0
        public void SetAverageColor(HSL avgColor)
        {
            averageColor = avgColor;

            Graphics g = Graphics.FromImage(previewFillColor.Image);
            RGB rgbpreview = new RGB();
            AForge.Imaging.ColorConverter.HSL2RGB(averageColor,rgbpreview);

            g.FillRectangle(new SolidBrush(rgbpreview.Color), 0, 0, previewFillColor.Width, previewFillColor.Height);
            g.DrawRectangle(new Pen(new SolidBrush(Color.Black)), new Rectangle(0, 0, previewFillColor.Width - 1, previewFillColor.Height - 3));
            g.Dispose();
            previewFillColor.Invalidate();

            avgColorH.Text = string.Format("{0}", averageColor.Hue);
            avgColorS.Text = string.Format("{0:F3}", averageColor.Saturation);
            avgColorL.Text = string.Format("{0:F3}", averageColor.Luminance);
        }
Пример #28
0
 /// <summary>
 /// Convert the color to <b>RGB</b> color space.
 /// </summary>
 /// 
 /// <returns>Returns <see cref="RGB"/> instance, which represents converted color value.</returns>
 /// 
 public RGB ToRGB( )
 {
     RGB rgb = new RGB( );
     ToRGB( this, rgb );
     return rgb;
 }
Пример #29
0
        public void SetColorRange(PictureBox pb, MouseEventArgs e, bool bgselect, Panel p)
        {
            try
            {
                // GET COLOR OF PIXEL
                Bitmap bitmap = new Bitmap((Bitmap)pb.Image);
                var loc = TranslateZoomMousePosition(e.Location, pb);
                var pixelcolor = bitmap.GetPixel(loc.X, loc.Y);

                if (bgselect)
                {
                    // SET COLOR RANGE
                    RangeRed = new IntRange(pixelcolor.R - 50, pixelcolor.R + 50);
                    RangeGreen = new IntRange(pixelcolor.G - 50, pixelcolor.G + 50);
                    RangeBlue = new IntRange(pixelcolor.B - 50, pixelcolor.B + 50);
                }
                else
                {
                    ColonyColor = new RGB(pixelcolor);
                }

                p.BackColor = pixelcolor;

                // DISPOSE OF BITMAP
                bitmap.Dispose();
            }
            catch (Exception) { }
        }
Пример #30
0
        private void imageDisplay_MouseMove(object sender, MouseEventArgs e)
        {
            if (bitmap != null)
            {
                bitmapPoint = CalculateMouseOverImagePosition(e.X, e.Y);
                int posX = bitmapPoint.X, posY = bitmapPoint.Y;

                if (zoomBitmap.Width <= imageDisplay.Width && zoomBitmap.Height <= imageDisplay.Height)
                {
                    if (posX < 0 || posY < 0 || posX >= zoomBitmap.Width || posY >= zoomBitmap.Height ||
                        e.X < position.X || e.X > position.X + zoomBitmap.Width || e.Y < position.Y || e.Y > position.Y + zoomBitmap.Height)
                    {
                        StatusLabelMousePosition.Visible = false;
                        StatusLabelRGB.Visible = false;
                        StatusLabelHSL.Visible = false;
                        this.Cursor = Cursors.Default;
                        return;
                    }
                }
                else
                {
                    if (fitImagesToWindow)
                    {
                        int maxWidth = imageDisplay.Width, maxHeight = imageDisplay.Height;
                        int width = bitmap.Width, height = bitmap.Height;

                        float ratio = (float)width / (float)height;

                        width = maxWidth;
                        height = (int)Math.Floor(maxWidth / ratio);

                        if (height > maxHeight)
                        {
                            height = maxHeight;
                            width = (int)Math.Floor(maxHeight * ratio);
                        }

                        if (posX < 0 || posY < 0 || posX >= width || posY >= height ||
                            e.X < position.X || e.X > position.X + width || e.Y < position.Y || e.Y > position.Y + height)
                        {
                            StatusLabelMousePosition.Visible = false;
                            StatusLabelRGB.Visible = false;
                            StatusLabelHSL.Visible = false;
                            this.Cursor = Cursors.Default;
                            return;
                        }
                    }
                    else
                    {
                        if (posX < 0 || posY < 0 || posX >= zoomBitmap.Width || posY >= zoomBitmap.Height ||
                            e.X < position.X || e.X > position.X + zoomBitmap.Width || e.Y < position.Y || e.Y > position.Y + zoomBitmap.Height)
                        {
                            StatusLabelMousePosition.Visible = false;
                            StatusLabelRGB.Visible = false;
                            StatusLabelHSL.Visible = false;
                            this.Cursor = Cursors.Default;
                            return;
                        }
                    }
                }

                Color color = zoomBitmap.GetPixel(posX, posY);
                RGB rgbColor = new RGB(color);
                HSL hslColor = HSL.FromRGB(rgbColor);

                StatusLabelMousePosition.Visible = true;
                StatusLabelMousePosition.Text = "X: " + posX.ToString() + " Y: " + posY.ToString();

                StatusLabelRGB.Visible = true;
                StatusLabelRGB.Text = "R: " + rgbColor.Red.ToString() + " G: " + rgbColor.Green.ToString() + " B: " + rgbColor.Blue.ToString();

                StatusLabelHSL.Visible = true;
                StatusLabelHSL.Text = "H: " + hslColor.Hue.ToString() + " S: " + hslColor.Saturation.ToString("0.000", CultureInfo.InvariantCulture) + " L: " + hslColor.Luminance.ToString("0.000", CultureInfo.InvariantCulture);

                if (cropping)
                {
                    this.Cursor = Cursors.Cross;
                    if (dragging)
                    {
                        displayEndPoint = e.Location;
                        cropEndPoint = bitmapPoint;

                        selection.Width = displayEndPoint.X - selection.X;
                        selection.Height = displayEndPoint.Y - selection.Y;

                        this.imageDisplay.Invalidate();
                    }
                }
            }
        }
 public void SetColor(Color color)
 {
     _color = new RGB(color);
     //GlobalVar.targetColor = color;
     //ChangeButtonColor();
 }
Пример #32
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
                        AForge.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
                        AForge.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 ) );
        }
Пример #33
0
        /// <summary>
        /// Convert from HSL to RGB color space.
        /// </summary>
        /// 
        /// <param name="hsl">Source color in <b>HSL</b> color space.</param>
        /// <param name="rgb">Destination color in <b>RGB</b> color space.</param>
        /// 
        public static void ToRGB( HSL hsl, RGB rgb )
        {
            if ( hsl.Saturation == 0 )
            {
                // gray values
                rgb.Red = rgb.Green = rgb.Blue = (byte) ( hsl.Luminance * 255 );
            }
            else
            {
                float v1, v2;
                float hue = (float) hsl.Hue / 360;

                v2 = ( hsl.Luminance < 0.5 ) ?
                    ( hsl.Luminance * ( 1 + hsl.Saturation ) ) :
                    ( ( hsl.Luminance + hsl.Saturation ) - ( hsl.Luminance * hsl.Saturation ) );
                v1 = 2 * hsl.Luminance - v2;

                rgb.Red   = (byte) ( 255 * Hue_2_RGB( v1, v2, hue + ( 1.0f / 3 ) ) );
                rgb.Green = (byte) ( 255 * Hue_2_RGB( v1, v2, hue ) );
                rgb.Blue  = (byte) ( 255 * Hue_2_RGB( v1, v2, hue - ( 1.0f / 3 ) ) );
                rgb.Alpha = 255;
            }
        }
Пример #34
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
                        AForge.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
                        AForge.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));
        }
Пример #35
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));
        }