AForge.Imaging.YCbCr.FromRGB C# (CSharp) Method

FromRGB() public static method

Convert from RGB to YCbCr color space (Rec 601-1 specification).
public static FromRGB ( RGB rgb, YCbCr ycbcr ) : void
rgb RGB Source color in RGB color space.
ycbcr YCbCr Destination color in YCbCr color space.
return void
        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 );
        }

Same methods

YCbCr::FromRGB ( RGB rgb ) : YCbCr

Usage Example

        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));
        }
All Usage Examples Of AForge.Imaging.YCbCr::FromRGB