They gave this homework for school. I've been searching for two days but couldn't find it. In my application, The image selected from the computer should be converted from rgb
to yuv
and converted to gray color.
I did the conversion to yuv
below, but there are 2 more missing, so I couldn't convert to double
, so I made an int
. The color is not grey.
private void btn_piksel_Click(object sender, EventArgs e) { if (pct_goruntu.Image == null) return;
Bitmap goruntu = (Bitmap)pct_goruntu.Image;
Color ilkPiksel = goruntu.GetPixel(0, 0);
int genislik = goruntu.Width;
int yukseklik = goruntu.Height;
Bitmap yeniGoruntu = new Bitmap(genislik, yukseklik);
for (int sutun = 0; sutun < genislik; sutun )
{
for (int satir = 0; satir < yukseklik; satir )
{
Color piksel = goruntu.GetPixel(sutun, satir);
double Y = (0.257 * piksel.R) (0.504 * piksel.G) (0.098 * piksel.B) 16;
double U = (-0.148 * piksel.R) (-0.291 * piksel.G) (0.439 * piksel.B) 128;
double V = (0.439 * piksel.R) (-0.368 * piksel.G) (-0.071 * piksel.B) 128;
Color hedefPiksel = Color.FromArgb(piksel.A, (int)Y, (int)U, (int)V);
yeniGoruntu.SetPixel(sutun, satir, hedefPiksel);
}
}
pct_hedef.Image = yeniGoruntu;
lbl_kirmizi.Text = "R: " ilkPiksel.R.ToString();
lbl_yesil.Text = "G: " ilkPiksel.B.ToString();
lbl_mavi.Text = "B: " ilkPiksel.G.ToString();
}`
CodePudding user response:
If you want the output to be a grayscale image you are only interested in the Luma (Y) component. So you should just use this for all the RGB color channels:
Color.FromArgb(piksel.A, (int)Y, (int)Y, (int)Y);
Note that the U and V components are not used, so could be omitted. See also converting a color image to grayscale, and fast work with bitmap
CodePudding user response:
If you want to convert an RGB image to YUV, you can refer to the following code:
public struct RGB
{
private byte _r;
private byte _g;
private byte _b;
public RGB(byte r, byte g, byte b)
{
this._r = r;
this._g = g;
this._b = b;
}
public byte R
{
get { return this._r; }
set { this._r = value; }
}
public byte G
{
get { return this._g; }
set { this._g = value; }
}
public byte B
{
get { return this._b; }
set { this._b = value; }
}
public bool Equals(RGB rgb)
{
return (this.R == rgb.R) && (this.G == rgb.G) && (this.B == rgb.B);
}
}
public struct YUV
{
private double _y;
private double _u;
private double _v;
public YUV(double y, double u, double v)
{
this._y = y;
this._u = u;
this._v = v;
}
public double Y
{
get { return this._y; }
set { this._y = value; }
}
public double U
{
get { return this._u; }
set { this._u = value; }
}
public double V
{
get { return this._v; }
set { this._v = value; }
}
public bool Equals(YUV yuv)
{
return (this.Y == yuv.Y) && (this.U == yuv.U) && (this.V == yuv.V);
}
}
public static YUV RGBToYUV(RGB rgb)
{
double y = rgb.R * .299000 rgb.G * .587000 rgb.B * .114000;
double u = rgb.R * -.168736 rgb.G * -.331264 rgb.B * .500000 128;
double v = rgb.R * .500000 rgb.G * -.418688 rgb.B * -.081312 128;
return new YUV(y, u, v);
}
See this.