是否可以在 OpenCV 中向后播放视频?通过 API 调用或通过缓冲视频帧并将顺序反转为新的视频文件。
谢谢
唯一可行的方法是手动提取帧,缓冲它们(在内存或文件上),然后以相反的顺序重新加载它们。
问题是视频压缩器都在利用时间冗余——两个连续帧大部分时间非常相似的事实。因此,它们不时编码一个完整的帧(通常每几百帧),然后只发送前一个和当前之间的差异。
现在,为了解码工作,必须以相同的顺序完成 - 解码一个关键帧(一个完整的),然后对于每个新帧,添加差异以获得当前图像。
这种策略使得在视频中反向播放变得非常困难。有几种技术,但都涉及缓冲。
现在,您可能已经看到了 Astor 描述的 CV_CAP_PROP_POS_FRAMES 参数。看起来没问题,但由于上述问题,OpenCV 无法正确跳转到特定帧(在这些问题上存在多个错误)。他们(OpenCV 开发人员)正在研究一些解决方案,但即使这些解决方案也会非常慢(它们涉及返回到上一个关键帧,然后解码回选定的关键帧)。如果使用这种技术,每帧必须平均解码数百次,因此速度非常慢。然而,它不起作用。
编辑 如果您追求缓冲方式来反转它,请记住,解码后的视频将很快耗尽普通计算机的内存资源。一个普通的 720p 视频一分钟长,解压后需要4.7GB内存!将帧作为单个文件存储在磁盘上是解决此问题的实用方法。
另一种解决方案,类似于ArtemStorozhuk 的答案是使用 FPS 从帧域移动到时域,然后使用CV_CAP_PROP_POS_MSEC
(不会像CV_CAP_PROP_POS_FRAMES
可以那样锤击 CPU)向后搜索。这是我的示例代码,它在 .mpg 上完美运行,仅使用大约 50% 的 CPU。
#include <opencv2/opencv.hpp>
int main (int argc, char* argv[])
{
cv::VideoCapture cap(argv[1]);
double frame_rate = cap.get(CV_CAP_PROP_FPS);
// Calculate number of msec per frame.
// (msec/sec / frames/sec = msec/frame)
double frame_msec = 1000 / frame_rate;
// Seek to the end of the video.
cap.set(CV_CAP_PROP_POS_AVI_RATIO, 1);
// Get video length (because we're at the end).
double video_time = cap.get(CV_CAP_PROP_POS_MSEC);
cv::Mat frame;
cv::namedWindow("window");
while (video_time > 0)
{
// Decrease video time by number of msec in one frame
// and seek to the new time.
video_time -= frame_msec;
cap.set(CV_CAP_PROP_POS_MSEC, video_time);
// Grab the frame and display it.
cap >> frame;
cv::imshow("window", frame);
// Necessary for opencv's event loop to work.
// Wait for the length of one frame before
// continuing the loop. Exit if the user presses
// any key. If you want the video to play faster
// or slower, adjust the parameter accordingly.
if (cv::waitKey(frame_msec) >= 0)
break;
}
}
是的,这是可能的。请参阅带有注释的代码:
//create videocapture
VideoCapture cap("video.avi");
//seek to the end of file
cap.set(CV_CAP_PROP_POS_AVI_RATIO, 1);
//count frames
int number_of_frames = cap.get(CV_CAP_PROP_POS_FRAMES);
//create Mat frame and window to display
Mat frame;
namedWindow("window");
//main loop
while (number_of_frames > 0)
{
//decrease frames and move to needed frame in file
number_of_frames--;
cap.set(CV_CAP_PROP_POS_FRAMES, number_of_frames);
//grab frame and display it
cap >> frame;
imshow("window", frame);
//wait for displaying
if (waitKey(30) >= 0)
{
break;
}
}
另请阅读这篇文章。