将视频序列与OpenCV中的另一个视频相加

Inter-laying a video sequence to another video in OpenCV

本文关键字:视频 另一个 OpenCV      更新时间:2023-10-16

如何使用opencv添加一个小视频序列?

要详细说明我有一个视频播放,这是互动的,可以说,可以说查看视频手势的用户在底部或现有视频的角落播放。

<。

对于每个帧,您需要在视频框架内复制带有所需内容的图像。步骤是:

  1. 定义覆盖帧的大小
  2. 定义在哪里显示覆盖帧
  3. 对于每个帧

    1. 用一些内容填充覆盖框架
    2. 在原始帧中定义的位置复制覆盖帧。

这个小片段将在摄像机供稿的右下方显示一个随机的噪声覆盖窗口:

#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 cameran";
        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 grabbedn";
            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;
}
相关文章: