mssb
mssb

Reputation: 878

How to convert RGB value to YCbCr using AForge

I am trying to convert RGB values to YCbCr color format. using Afroge library. but it gives result like this, for Y : 0.611, Cb : 0.15, Cr : -0.18. that means all the values < 1. but I need the values between 0 - 255. this is my code.

        Bitmap bitmap = (Bitmap)pictureBox1.Image;

        double xRatio = (double)bitmap.Width / (double)pictureBox1.Width;
        double yRatio = (double)bitmap.Height / (double)pictureBox1.Height;

        Color gotColor = bitmap.GetPixel((int)((double)mX * xRatio), (int)((double)mY * yRatio));
        label1.Text = (" R : " + gotColor.R);
        label2.Text = (" G : " + gotColor.G);
        label3.Text = (" B : " + gotColor.B);

        YCbCr ycrcb = new YCbCr();
        RGB rgbcolor = new RGB(gotColor);

        ycrcb.Y = YCbCr.FromRGB(rgbcolor).Y;
        ycrcb.Cr = YCbCr.FromRGB(rgbcolor).Cr;
        ycrcb.Cb = YCbCr.FromRGB(rgbcolor).Cb;

        label4.Text = ycrcb.Y.ToString();
        label5.Text = ycrcb.Cr.ToString();
        label6.Text = ycrcb.Cb.ToString();

please help me.

Upvotes: 0

Views: 2476

Answers (2)

The formula is:

Y = (byte)((0.299 * red) + (0.587 * green) + (0.114 * blue));
Cb = (byte)(128 - (0.168736 * red) + (0.331264 * green) + (0.5 * blue));
Cr = (byte)(128 + (0.5 * red) + (0.418688 * green) + (0.081312 * blue));

And you can the YCbCr of jpg format. The result values are betewn 0 and 255.

Upvotes: 0

James
James

Reputation: 9985

Those are the standard ranges for YCrCb

However, you could just rescale the result into the 0 - 255 range

    label4.Text = (ycrcb.Y *255).ToString();
    label5.Text = ((ycrcb.Cr + 0.5)*255).ToString();
    label6.Text = ((ycrcb.Cb + 0.5)*255).ToString();

Upvotes: 1

Related Questions