Search code examples
c++visual-studioopencvimage-processingvideo-processing

Inter-laying a video sequence to another video in OpenCV


How can I add a small video sequence to another video using OpenCV?

To elaborate, let's say I have a video playing which is to be interactive where let's say the user viewing the video gestures something and a short sequence plays at the bottom or at the corner of the existing video.


Solution

  • For each frame, you need to copy an image with the content you need inside the video frame. The steps are:

    1. Define the size of the overlay frame
    2. Define where to show the overlay frame
    3. For each frame

      1. Fill the overlay frame with some content
      2. Copy the overlay frame in the defined position in the original frame.

    This small snippet will show a random noise overlay window on bottom right of the camera feed:

    #include <opencv2/opencv.hpp>
    using namespace cv;
    using namespace std;
    
    
    int main()
    {
        // Video capture frame
        Mat3b frame;
        // Overlay frame
        Mat3b overlayFrame(100, 200);
    
        // Init VideoCapture
        VideoCapture cap(0);
    
        // check if we succeeded
        if (!cap.isOpened()) {
            cerr << "ERROR! Unable to open camera\n";
            return -1;
        }
    
        // Get video size
        int w = cap.get(CAP_PROP_FRAME_WIDTH);
        int h = cap.get(CAP_PROP_FRAME_HEIGHT);
    
        // Define where the show the overlay frame 
        Rect roi(w - overlayFrame.cols, h - overlayFrame.rows, overlayFrame.cols, overlayFrame.rows);
    
        //--- GRAB AND WRITE LOOP
        cout << "Start grabbing" << endl
            << "Press any key to terminate" << endl;
        for (;;)
        {
            // wait for a new frame from camera and store it into 'frame'
            cap.read(frame);
    
            // Fill overlayFrame with something meaningful (here random noise)
            randu(overlayFrame, Scalar(0, 0, 0), Scalar(256, 256, 256));
    
            // Overlay
            overlayFrame.copyTo(frame(roi));
    
            // check if we succeeded
            if (frame.empty()) {
                cerr << "ERROR! blank frame grabbed\n";
                break;
            }
            // show live and wait for a key with timeout long enough to show images
            imshow("Live", frame);
            if (waitKey(5) >= 0)
                break;
        }
        // the camera will be deinitialized automatically in VideoCapture destructor
        return 0;
    }