A
A
Artem Ryblov2014-03-02 23:02:53
C++ / C#
Artem Ryblov, 2014-03-02 23:02:53

How to convert an image from RGB to YIQ using c#?

I tried to convert. Here 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 a range of [0,1], the I parameter has a range of [-0.523,0.523], and the Q parameter has a range of [-0.596,0.596]. Also I know that R, G and B have a range of [0,255].
You need to somehow bring the ranges or I don’t understand something. I'm using "if" to get positive values ​​for G and B. Because of this, I'm getting the wrong image. How can I make this transfer?
And in general, is it possible to set the color of a pixel somehow differently?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2014-03-03
@Extremesarova

pixelColor = Color.FromArgb(y, i, q);
as you rightly pointed out on stackoverfow , you only need to use this method for RGB/RGBA. It does not know how to work directly with yiq .NET. Usually non-standard color spaces are used for intermediate transformations.
By the way, parameter i has range [-0.522, 0.522] and i [-0.596, 0.596].
In the course of the algorithm, you don’t need these ifs either, the coefficients do everything for you. For example:
i = 0.596 * r - 0.274 * g - 0.322 * b;
i = 0.596 * 0 - 0.274 * 1 - 0.322 * 1;
i = -0.596
hence you do not need any of these perversions with ifs.
p.s. Why are you multiplying by 100? Are you trying to adjust the values ​​in such a way that FromArgb would eat them? You don't need to do this.
And what's the point if changing the color space doesn't change your color...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question