title: Overlaying/merging two (and more) YUV images in OpenCV
body: <p>I investigated and stripped down my previous question (<a href="https://stackoverflow.com/questions/25908414/is-there-a-way-to-avoid-conversion-from-yuv-to-bgr">Is there a way to avoid conversion from YUV to BGR?</a>). I want to overlay few images (format is YUV) on the resulting, bigger image (think about it like it is a canvas) and send it via network library (OPAL) forward without converting it to to BGR.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>    Mat tYUV;
      Mat tClonedYUV;
      Mat tBGR;
      Mat tMergedFrame;
      int tMergedFrameWidth = 1000;
      int tMergedFrameHeight = 800;
      int tMergedFrameHalfWidth = tMergedFrameWidth / 2;
  
      tYUV = Mat(tHeader-&gt;height * 1.5f, tHeader-&gt;width, CV_8UC1, OPAL_VIDEO_FRAME_DATA_PTR(tHeader));
      tClonedYUV = tYUV.clone();
  
      tMergedFrame = Mat(Size(tMergedFrameWidth, tMergedFrameHeight), tYUV.type(), cv::Scalar(0, 0, 0));
      tYUV.copyTo(tMergedFrame(cv::Rect(0, 0, tYUV.cols &gt; tMergedFrameWidth ? tMergedFrameWidth : tYUV.cols, tYUV.rows &gt; tMergedFrameHeight ? tMergedFrameHeight : tYUV.rows)));
      tClonedYUV.copyTo(tMergedFrame(cv::Rect(tMergedFrameHalfWidth, 0, tYUV.cols &gt; tMergedFrameHalfWidth ? tMergedFrameHalfWidth : tYUV.cols, tYUV.rows &gt; tMergedFrameHeight ? tMergedFrameHeight : tYUV.rows)));
  
  
      namedWindow("merged frame", 1);
      imshow("merged frame", tMergedFrame);
      waitKey(10);
  </code></pre>
  
  <p>The result of above code looks like this:
  <img src="https://i.stack.imgur.com/JoGUc.png" alt="enter image description here"></p>
  
  <p>I guess the image is not correctly interpreted, so the pictures stay black/white (Y component) and below them, we can see the U and V component. There are images, which describes the problem well (<a href="http://en.wikipedia.org/wiki/YUV" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/YUV</a>):</p>
  
  <p><img src="https://i.stack.imgur.com/kEo4o.png" alt="enter image description here"></p>
  
  <p>and: <a href="http://upload.wikimedia.org/wikipedia/en/0/0d/Yuv420.svg" rel="nofollow noreferrer">http://upload.wikimedia.org/wikipedia/en/0/0d/Yuv420.svg</a></p>
  
  <p>Is there a way for these values to be correctly read? I guess I should not copy the whole images (their Y, U, V components) straight to the calculated positions. The U and V components should be below them and in the proper order, am I right? </p>