【问题标题】:How to convert an rgb image to yuv using c#如何使用 c# 将 rgb 图像转换为 yuv
【发布时间】:2022-07-31 04:54:12
【问题描述】:

他们给学校布置了这个作业。我找了两天没找到。在我的应用程序中,从计算机中选择的图像应该从rgb 转换为yuv 并转换为灰色。

我在下面进行了yuv的转换,但是还有2个缺失,所以我无法转换为double,所以我做了一个int。颜色不是灰色的。

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();
}`

【问题讨论】:

  • this 是否可以帮助将 RGB 图像转换为 YUV?
  • 是的,它可以提供帮助

标签: c# image visual-studio rgb yuv


【解决方案1】:

如果您希望输出为灰度图像,您只对 Luma (Y) 组件感兴趣。所以你应该将它用于所有 RGB 颜色通道:

 Color.FromArgb(piksel.A, (int)Y, (int)Y, (int)Y);

请注意,U 和 V 分量未使用,因此可以省略。另请参阅 converting a color image to grayscalefast work with bitmap

【讨论】:

    【解决方案2】:

    如果要将RGB图像转换为YUV,可以参考以下代码:

    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);
    }
    

    this

    【讨论】:

      【解决方案3】:

      您可以为此使用ColorHelper 库:

      using ColorHelper;
      
      RGB rgb = new RGB(200, 200, 200);
      YUV yuv = ColorConverter.RgbToYuv(rgb);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-27
        相关资源
        最近更新 更多