title: Convert from yuv 420 to image<Bgr,byte>
body: <p>I have byte array with yuv420 data.</p>
  
  <pre><code>byte[] yuv420;//yuv data
  </code></pre>
  
  <p>How can I convert this to an <code>Image&lt;Bgr, byte&gt;</code>?</p>
  
  <p>I found a math formula to convert to RGB and then to <code>Image&lt;Bgr, byte&gt;</code> but it is very slow.  Is there a way to convert it faster?</p>
  
  <p>There is a class in Emgu for converting </p>
  
  <pre><code>COLOR_CONVERSION(enum CV_YUV2RGB    Convert YUV color to RGB)
  </code></pre>
  
  <p>but I can not understand how use this class.  Can anyone help?</p>
  
  <pre><code>static Bitmap ConvertYUV2RGB(byte[] yuvFrame, byte[] rgbFrame, int width, int height)
  {
      int uIndex = width * height;
      int vIndex = uIndex + ((width * height) &gt;&gt; 2);
      int gIndex = width * height;
      int bIndex = gIndex * 2;
  
      int temp = 0;
  
  
      //图片为pic1,RGB颜色的二进制数据转换得的int r,g,b;
      Bitmap bm = new Bitmap(width, height);
  
      int r = 0;
      int g = 0;
      int b = 0;
  
  
      for (int y = 0; y &lt; height; y++)
      {
          for (int x = 0; x &lt; width; x++)
          {
              // R分量
              temp = (int)(yuvFrame[y * width + x] + (yuvFrame[vIndex + (y / 2) * (width / 2) + x / 2] - 128) * YUV2RGB_CONVERT_MATRIX[0, 2]);
              rgbFrame[y * width + x] = (byte)(temp &lt; 0 ? 0 : (temp &gt; 255 ? 255 : temp));
              // G分量
              temp = (int)(yuvFrame[y * width + x] + (yuvFrame[uIndex + (y / 2) * (width / 2) + x / 2] - 128) * YUV2RGB_CONVERT_MATRIX[1, 1] + (yuvFrame[vIndex + (y / 2) * (width / 2) + x / 2] - 128) * YUV2RGB_CONVERT_MATRIX[1, 2]);
              rgbFrame[gIndex + y * width + x] = (byte)(temp &lt; 0 ? 0 : (temp &gt; 255 ? 255 : temp));
              // B分量
              temp = (int)(yuvFrame[y * width + x] + (yuvFrame[uIndex + (y / 2) * (width / 2) + x / 2] - 128) * YUV2RGB_CONVERT_MATRIX[2, 1]);
              rgbFrame[bIndex + y * width + x] = (byte)(temp &lt; 0 ? 0 : (temp &gt; 255 ? 255 : temp));
              Color c = Color.FromArgb(rgbFrame[y * width + x], rgbFrame[gIndex + y * width + x], rgbFrame[bIndex + y * width + x]);
              bm.SetPixel(x, y, c);
          }
      }
      return bm;
  
  }
  
  static double[,] YUV2RGB_CONVERT_MATRIX = new double[3, 3] { { 1, 0, 1.4022 }, { 1, -0.3456, -0.7145 }, { 1, 1.771, 0 } };
  static byte clamp(float input)
  {
      if (input &lt; 0) input = 0;
      if (input &gt; 255) input = 255;
      return (byte)Math.Abs(input);
  }
  </code></pre>