title: Blackmagic frame : convert from yuv to RGB to use in openCV
body: <p>I have a problem to convert an image captured from a camera in YUV format to RGB format.</p>
  
  <p>The function which is used to do it is the following : </p>
  
  <pre><code>int uwidth = 1920;
  int uheight= 1080;
  
  int i = 0,j = 0, r = 0, g = 0, b = 0;
  typedef unsigned char BYTE;
  IplImage* m_RGB = cvCreateImage(cvSize(uwidth, uheight), IPL_DEPTH_8U, 3);
  unsigned char* pData = (unsigned char *) frameBytes;
  
  for(i = 0, j=0; i &lt; uwidth * uheight*3 ; i+=6, j+=4)
  {
     unsigned char u = pData[j];
     unsigned char y = pData[j+1];
     unsigned char v = pData[j+2];
  
     b = 1.0*y + 8 + 1.402*(v-128);
     g = 1.0*y - 0.34413*(u-128) - 0.71414*(v-128);
     r = 1.0*y + 1.772*(u-128);
  
     if(r&gt;255) r =255;
     if(g&gt;255) g =255;
     if(b&gt;255) b =255;
     if(r&lt;0)   r =0;
     if(g&lt;0)   g =0;
     if(b&lt;0)   b =0;
  
     m_RGB-&gt;imageData[i] = (BYTE)(r*220/256);
     m_RGB-&gt;imageData[i+1] = (BYTE)(g*220/256);
     m_RGB-&gt;imageData[i+2] =(BYTE)(b*220/256);
  
  }
  
  cvNamedWindow("ck", CV_WINDOW_AUTOSIZE);
  cvShowImage( "ck", m_RGB );
  cvReleaseImage(&amp;m_RGB);
  </code></pre>
  
  <p>The problem is that we have not one but two images in the window on my screen, and that we have good colors but not the good ratio.</p>
  
  <p>Does anyone have an idea about those problems ?</p>
  
  <p><strong>Edit:</strong>  <a href="http://i.stack.imgur.com/2oky6.jpg" rel="nofollow">Image output</a></p>