OpenCV 寻道功能/倒带

发布于 2024-09-04 02:10:31 字数 117 浏览 5 评论 0原文

我一直在尝试使用 C++ 中的 OpenCV 查找/实现搜索和倒带功能(用于视频(.avi)),但除了一次浏览整个文件并保存每个图像之外,我找不到其他方法。还有其他办法吗?

任何帮助将不胜感激;提前致谢!

I've been trying to find/implement a seek and rewind function (for video (.avi)) using OpenCV in C++, but I cant find a way of doing it, other than going through the entire file once and saving each image. Is there any other way?

Any help would be much appreciated; Thanks ahead of time!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情绪 2024-09-11 02:10:31

使用 cvSetCaptureProperty(),您可以循环帧,可以以毫秒为单位,也可以按顺序帧编号。

int cvSetCaptureProperty( CvCapture* capture, int property_id, double value );

property_id 是您需要使用的属性。它可以是以下之一:

  1. CV_CAP_PROP_POS_MSEC - 从文件开头开始的位置(以毫秒为单位)
  2. CV_CAP_PROP_POS_FRAMES - 以帧为单位的位置
  3. CV_CAP_PROP_POS_AVI_RATIO - 以相对单位表示的位置(0 - 文件开头, 1 - 文件末尾)
  4. CV_CAP_PROP_FRAME_WIDTH - 视频流中帧的宽度(仅适用于摄像机)
  5. CV_CAP_PROP_FRAME_HEIGHT - 视频流中帧的高度(仅适用于摄像机)
  6. CV_CAP_PROP_FPS - 帧速率(仅适用于摄像机)
  7. CV_CAP_PROP_FOURCC - 4 字符代码编解码器(仅适用于相机)。

前两个是您感兴趣的。

编辑:更多信息:)

您只需使用各种帧索引重复调用上述函数即可循环浏览帧。

cvSetCaptureProperty(capture, CV_CAP_PROP_POS_FRAMES, frameIndex);

示例:

IplImage*  frame;
CvCapture* capture = cvCreateFileCapture("test.avi");

/* iterate through first 10 frames */
for (int i = 0; i < 10; i++)
{
   /* set pointer to frame index i */
   cvSetCaptureProperty(capture, CV_CAP_POS_FRAMES, i);

   /* capture the frame and do sth with it */
   frame = cvQueryFrame(capture);
}

您可以放置​​类似的代码,以便在每次用户单击按钮前进/后退视频时执行。

C++ 方法(OpenCV 2 及更高版本)将使用此方法来代替相同的 property_id 和值。

bool VideoCapture::set(int property_id, double value)

Using cvSetCaptureProperty() you can cycle through frames, either in miliseconds or by ordinal frame number.

int cvSetCaptureProperty( CvCapture* capture, int property_id, double value );

property_id is a property you would need to use. It can be one of the following:

  1. CV_CAP_PROP_POS_MSEC - position in milliseconds from the file beginning
  2. CV_CAP_PROP_POS_FRAMES - position in frames
  3. CV_CAP_PROP_POS_AVI_RATIO - position in relative units (0 - start of the file, 1 - end of the file)
  4. CV_CAP_PROP_FRAME_WIDTH - width of frames in the video stream (only for cameras)
  5. CV_CAP_PROP_FRAME_HEIGHT - height of frames in the video stream (only for cameras)
  6. CV_CAP_PROP_FPS - frame rate (only for cameras)
  7. CV_CAP_PROP_FOURCC - 4-character code of codec (only for cameras).

The first two is of your interest.

EDIT: more info :)

You can cycle through frames just by repeatedly calling the mentioned function with various frame indices.

cvSetCaptureProperty(capture, CV_CAP_PROP_POS_FRAMES, frameIndex);

Example:

IplImage*  frame;
CvCapture* capture = cvCreateFileCapture("test.avi");

/* iterate through first 10 frames */
for (int i = 0; i < 10; i++)
{
   /* set pointer to frame index i */
   cvSetCaptureProperty(capture, CV_CAP_POS_FRAMES, i);

   /* capture the frame and do sth with it */
   frame = cvQueryFrame(capture);
}

You could put similar code to execute each time user clicks a button to forward/rewind the video.

The C++ method (OpenCV 2 and higher) would be to use this method instead with the same property_id and value.

bool VideoCapture::set(int property_id, double value)
Saygoodbye 2024-09-11 02:10:31

我认为您必须将整个文件读入 IplImages 数组,然后进行处理。原因是,cvQueryFrame 是一个单向过程,它按顺序一次读取一帧。我想不出其他办法了。根据视频的长度,初始化时间可能不会太差。

cvTrackbars 正如你所说,主要用于更改参数。它们更改变量的值(以指针形式作为参数给出)并抛出回调函数。不幸的是,据我所知,它们是 HighGUI 中唯一的按钮样式元素

I think you'd have to read in the entire file into an array of IplImages, then work through that. The reason is, cvQueryFrame is a one-way process, it reads one frame at a time in order. I can't think of any other way. Depending on the length of the video the initialisation time may not be too bad.

The cvTrackbars are as you say, mainly used for altering parameters. They alter the value of a variable (given as a parameter in pointer form) and throw a callback function. Unfortunately they are the only button-style elements in HighGUI as far as I know

遗心遗梦遗幸福 2024-09-11 02:10:31

对于C++和opencv3.4,frame_index是你想要寻找的位置。

Mat frame;
VideoCapture capture("test.avi");
capture.set(CAP_PROP_POS_FRAMES, frame_index);
capture>>frame;

for C++ and opencv3.4,frame_index is the position you want to seek to.

Mat frame;
VideoCapture capture("test.avi");
capture.set(CAP_PROP_POS_FRAMES, frame_index);
capture>>frame;
梦罢 2024-09-11 02:10:31

highgui 库中,您将找到搜索栏的函数(cvCreateTrackbar 和朋友) 。

In the highgui library you'll find functions for a seek bar (cvCreateTrackbar and friends).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文