Overlaying/merging two (and more) YUV images in OpenCV [closed]

asked 2014-09-29 10:50:12 -0600

carpet gravatar image

updated 2020-10-11 14:30:45 -0600

I investigated and stripped down my previous question (http://answers.opencv.org/question/42201/is-there-a-way-to-avoid-conversion-from-yuv-to-bgr/). 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.

Here is the code:

    Mat tYUV;
    Mat tClonedYUV;
    Mat tBGR;
    Mat tMergedFrame;
    int tMergedFrameWidth = 1000;
    int tMergedFrameHeight = 800;
    int tMergedFrameHalfWidth = tMergedFrameWidth / 2;

    tYUV = Mat(tHeader->height * 1.5f, tHeader->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 > tMergedFrameWidth ? tMergedFrameWidth : tYUV.cols, tYUV.rows > tMergedFrameHeight ? tMergedFrameHeight : tYUV.rows)));
    tClonedYUV.copyTo(tMergedFrame(cv::Rect(tMergedFrameHalfWidth, 0, tYUV.cols > tMergedFrameHalfWidth ? tMergedFrameHalfWidth : tYUV.cols, tYUV.rows > tMergedFrameHeight ? tMergedFrameHeight : tYUV.rows)));


    namedWindow("merged frame", 1);
    imshow("merged frame", tMergedFrame);
    waitKey(10);

The result of above code looks like this: enter image description here

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 (http://en.wikipedia.org/wiki/YUV):

enter image description here

and: http://upload.wikimedia.org/wikipedia/en/0/0d/Yuv420.svg

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?

edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-10-11 14:31:17.454163