将两个不同的设备捕获帧合并为一个帧并编写视频

时间:2019-03-13 08:36:02

标签: c++ opencv

我是opencv的初学者,但精通C#/ C ++。 我创建了一个openCV控制台应用程序,用于捕获帧数据并将其写入来自多个设备或RTSP流的视频中,效果很好。 我需要将两个单独的设备帧的输出合并为一帧,并在下面的代码段中写入视频,但是生成的视频文件已损坏。

视频捕获和视频编写器设置为以1920 x 1080分辨率抓拍。

  for (int i = 0; i < devices; i++) {
  Mat frame;
  Mat f1, f2;
  //Grab frame from camera capture
  if (videoCapturePool[i].grab()) {
    videoCapturePool[i].retrieve(f1);
    f2 = f1.clone();
    if (isPause) {
      circle(f1, Point(f1.size().width / 2, f1.size().height / 2), 10, (255, 255, 10), -1);
      circle(f2, Point(f2.size().width / 2, f2.size().height / 2), 10, (255, 255, 10), -1);
    }
    hconcat(f1, f2, frame);

    imshow("Output", frame);

    waitKey(10);

    frameData[i].camerFrames.push_back(frame);
  }
  f1.release();
  f2.release();

}

for (int i = 0; i < devices; i++) {
  int32_t frame_width(static_cast < int32_t > (videoCapturePool[i].get(CV_CAP_PROP_FRAME_WIDTH)));
  int32_t frame_height(static_cast < int32_t > (videoCapturePool[i].get(CV_CAP_PROP_FRAME_HEIGHT)));

  VideoWriter vidwriter = VideoWriter(videoFilePaths[i], CV_FOURCC('M', 'J', 'P', 'G'), 30, Size(frame_width, frame_height), true);

  videoWriterPool.push_back(vidwriter);
  writer << frameData[i].camerFrames.size() << endl;
  for (size_t j = 0; j < frameData[i].camerFrames.size(); j++) {
    //write the frame
    videoWriterPool[i].write(frameData[i].camerFrames[j]);
  }
  frameData[i].camerFrames.clear();
}

1 个答案:

答案 0 :(得分:0)

通常必须重构您的方法。我不是说代码,而是关于架构。内存有很大问题-让我们计算一下!

  1. 一个RGB帧1920x1080的大小:frame_size = 1920 * 1080 * 3 = 6 Mb

  2. 您要从2个摄像机捕获多少帧?例如,一分钟30 fps的视频:video_size = 2摄像头* 6 Mb /帧* 60秒= 21 Gb!每个进程有这么多内存吗?

我建议在不同线程中为捕获帧创建队列,并在2个线程中从捕获队列中提取帧并将其写入文件。

相关问题