我正在编写一个程序,可以读取多个网络摄像头,将图片拼接在一起并将其保存到视频文件中。
我已经发现this thread使用线程进行写入但似乎没有避免竞争条件。
我的程序的伪代码:
camVector //vector with cameras
VideoWriter outputVideo //outputs the video
while(true){
for(camera:camVector){
camera.grab() //send signal to grab picture
}
picVector = getFrames(camVector) //collect frames of all cameras into a Vector
Mat bigImg = stitchPicTogether(picVector)
outputVideo.write(bigImg)
}
答案 0 :(得分:1)
这就是我要做的事情:
camVector //vector with cameras
VideoWriter outputVideo //outputs the video
for(camera:camVector) camera.grab(); // request first frame
while(true){
parallel for(camera:camVector) {
frame = getFrame(camera);
pasteFrame(/* destination */ bigImg,
/* geometry */ geometry[camera],
/* source */ frame
);
camera.grab(); // request next frame, so the webcam driver can start working
// while you write to disk
}
outputVideo.write(bigImg)
}
这种方式并行完成stiching,如果您的网络摄像头驱动程序具有不同的计时,您可以在等待其他网络摄像头的帧时开始计算从一个网络摄像头收到的内容。
关于实施,您可以简单地使用已在OpenCV中使用的OpenMP,例如#pragma omp parallel
。如果您更喜欢C ++之类的东西,请试试英特尔TBB,这很酷。
或者正如你所说,你可以使用原生的C ++ 11线程或者提升。在两者之间进行选择主要取决于您的工作环境:如果您打算使用较旧的编译器,则提升更安全。
在所有情况下,除了最后的连接外,不需要锁定,等待所有线程完成其工作。
如果您的瓶颈是写输出,则限制因素是磁盘IO或视频压缩速度。对于磁盘,您可以尝试更改编解码器和/或压缩更多。对于压缩速度,请查看像this这样的GPU编解码器。