Question

I am trying to convert. This is my code:

private void RGBtoYIQbutton_Click(object sender, EventArgs e)
        {
            int[] vector_yiq = new int[3];
            double r, g, b, y, i, q;

            Bitmap bmp = new Bitmap(pictureBox1.Image);

            for (int x = 0; x < bmp.Width; x++)
            {
                for (int y = 0; y < bmp.Height; y++)
                {
                    Color clr = bmp.GetPixel(x, y);
                    r = (double)clr.R / 255;
                    g = (double)clr.G / 255;
                    b = (double)clr.B / 255;

                    y = Convert.ToInt32((0.299 * r + 0.587 * g + 0.114 * b) * 100);
                    i = Convert.ToInt32((0.596 * r - 0.274 * g - 0.322 * b) * 100);
                    if (i < 0)
                        i = Convert.ToInt32((0.596 * r - 0.274 * g - 0.322 * b + 0.5957) * 100);
                    q = Convert.ToInt32((0.211 * r - 0.522 * g + 0.311 * b ) * 100);
                    if (q < 0)
                        q = Convert.ToInt32((0.211 * r - 0.522 * g + 0.311 * b + 0.5226) * 100);

                    Color pixelColor;
                    pixelColor = Color.FromArgb(y, i, q);
                    bmp.SetPixel(x, y, pixelColor);
                }
            }
            pictureBox1.Image = bmp;

I know that the Y parameter has the range [0,1], the I parameter has the range [-0.523,0.523], and the Q parameter has the range [-0.596,0.596]. Also I know that R, G and B have the range [0,255]. I am using "if" to get positive values​​ for G and B. Because of this I getting wrong image. How can I do this conversion?

No correct solution

OTHER TIPS

You can use ColorHelper library for this:

using ColorHelper;

RGB rgb = new RGB(100, 100, 100);
YIQ yiq = ColorConverter.RgbToYiq(rgb);

Links:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top