2016-11-28 41 views
1

如何使用OpenCV將另一個視頻序列添加到另一個視頻?在OpenCV中將視頻序列交叉到另一個視頻

爲了詳細說明,假設我有一個視頻播放功能,可以讓用戶在觀看視頻時手勢播放某些內容,並在現有視頻的底部或角落播放短片段。

回答

0

對於每一幀,您需要在視頻幀中複製包含所需內容的圖像。的步驟是:

  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; 
}