c++ - 在 OpenCV 中将一个视频序列插入到另一个视频中

标签 c++ visual-studio opencv image-processing video-processing

如何使用 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 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;
}

关于c++ - 在 OpenCV 中将一个视频序列插入到另一个视频中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40840157/

相关文章:

python - 来自 C (Visual Studio) 的 CMD 命令

.net - QuickWatch 无法正确显示可空属性的 ".ToString()"

python - opencv中的色彩空间转换(RGB-> LAB)-红色不会产生期望值

c++ - 十六进制的 OpenCV RGB 值?

c++ - 如何从 .cc 文件访问 tcl 脚本中的值

c++ - QGraphicsItem 中出现奇怪的 "incomplete type"错误

c++ - 如何将 gdi+ 类位图结构转换为 HDC?

C++ AES OpenSSL

visual-studio - Azure/VS Code/Hive/HDInsight - 如何停止交互式查询

python - 打开CV 3.0 findHomography引发错误