Attention! This forum will be made read-only by Dec-20. Please migrate to https://forum.opencv.org. Most of existing active users should've received invitation by e-mail.
Ask Your Question

carpet's profile - activity

2019-01-23 08:34:43 -0500 received badge  Popular Question (source)
2014-09-29 10:50:12 -0500 asked a question Overlaying/merging two (and more) YUV images in OpenCV

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?

2014-09-18 08:24:37 -0500 commented question Is there a way to avoid conversion from YUV to BGR?

I use cv::resize and copyTo functions (from cv::Mat class), to scale and copy frames on the merged one. It's like the destination, the canvas.

2014-09-17 09:57:37 -0500 asked a question Is there a way to avoid conversion from YUV to BGR?

Hi guys, I am now in the middle of the project, which purpose is to merge frames received from the web (via OPAL Library).

I have written a class Merger, which accepts all of the frames (from different clients). It's possible to get the merged one with the getFrame method.

Frames which i got are in the YUV420p format, but AFAIK, OpenCV's methods support BGR format. So before feeding my function with them I convert: YUV420p -> BGR. Received frame is also converted back, to YUV format.

Can I avoid doing this (and consequently speed up the process? Here is my (pseudo) code:

    unsigned char* receivedFrame()
    {
            // tHeader is raw data with header (it was taken via OPAL library) 
            //...
            //...

            Mat tInputYUV = Mat(tHeader->height * 1.5f, tHeader->width, CV_8UC1, OPAL_VIDEO_FRAME_DATA_PTR(tHeader));
            Mat tInputBGR;
            cvtColor(tInputYUV, tInputBGR, CV_YUV420p2BGR);
            mMerger.addFrame(pID, tInputBGR);
            Mat tMergedFrame = mMerger.getFrame();
            Mat tOutputYUV;
            cvtColor(tMergedFrame, tMergedFrameYUV, CV_BGR2YUV_YV12);

            // do stuff with tOutputYUV -> eg. send it to the client
            //...               
            //...
    }

In getFrame I am using cv::resize and copyTo (from the current frame to the output frame).

Is there a way to speed up this procedure or avoid converting? I tried not doing this, but then after merging my frame is black & white (probably only Y channel remains). Without converting to BGR: image description