简体   繁体   English

如何使用Matlab从视频中以特定间隔提取帧

[英]How to extract frames at particular intervals from video using matlab

I am using matlab 2013a software for my project. 我正在为我的项目使用matlab 2013a软件。

I face a problem while splitting video into individual frames. 将视频分割成单独的帧时遇到问题。

I want to know how to get frames at a specific intervals from video.. ie, i want to grab frames at the rate of one frame per second(frame/sec) .My input video has 50 frames/sec. 我想知道如何以特定的间隔从视频中获取帧。即,我想以每秒一帧(帧/秒)的速率抓取帧。我的输入视频具有50帧/秒。 In the code I have used step() to slice the video into frames. 在代码中,我使用step()将视频切成帧。

The following is my code , basically a face detection code(detects multiple faces in a video) . 以下是我的代码,基本上是面部检测代码(检测视频中的多个面部)。 This code captures every frame in the video(ie 50fp approx) and processes it. 此代码捕获视频中的每个帧(即约50fp)并对其进行处理。 I want to process frames at the rate of 1 fps. 我想以1 fps的速率处理帧。 Please help me. 请帮我。

clear classes;
videoFileReader = vision.VideoFileReader('C:\Users\Desktop\project\05.mp4');
**videoFrame      = step(videoFileReader);**
faceDetector = vision.CascadeObjectDetector(); % Finds faces by default
tracker = MultiObjectTrackerKLT;
videoPlayer  = vision.VideoPlayer('Position',[200 100       fliplr(frameSize(1:2)+30)]);

bboxes = [];
while isempty(bboxes)
  **framergb = step(videoFileReader);**
  frame = rgb2gray(framergb);
  bboxes = faceDetector.step(frame); 
end

tracker.addDetections(frame, bboxes);
frameNumber = 0;
keepRunning = true;

while keepRunning

   **framergb = step(videoFileReader);**
   frame = rgb2gray(framergb);

   if mod(frameNumber, 10) == 0
      bboxes = 2 * faceDetector.step(imresize(frame, 0.5));
      if ~isempty(bboxes)
        tracker.addDetections(frame, bboxes);
      end
   else
    % Track faces
    tracker.track(frame);
   end
end

%% Clean up
release(videoPlayer);

But this actually considers every frame. 但这实际上考虑了每个帧。 I want to grab 1fps. 我想抓取1fps。

It cannot be done directly in Matlab 2013a, because the video access library does not provide the feature you want. 由于视频访问库未提供所需的功能,因此无法直接在Matlab 2013a中完成。 Writing the necessary code to implement an efficient frame skipping routine is not really possible using just Matlab code (you would need to look inside the video libraries) 仅使用Matlab代码实际上不可能编写必要的代码来实现有效的跳帧例程(您需要查看视频库的内部)

Working around it, you have two basic options: 要解决此问题,您有两个基本选项:

  1. Do as little work as possible on frames that you do not want to process. 在您不想处理的框架上做尽可能少的工作。

Where you currently have 您目前在哪里

framergb = step(videoFileReader);

Instead do something like 而是做类似的事情

for i=1:49,
  step(videoFileReader);
end
framergb = step(videoFileReader);

(NB this does not allow for going beyond end of input) (注意,这不允许超出输入范围)

  1. Pre-process your file with a tool like ffmpeg , and reduce the frame-rate before you use Matlab. 使用ffmpeg类的工具对文件进行预处理,然后在使用Matlab之前降低帧速率。

The ffmpeg command might look something like this: ffmpeg命令可能看起来像这样:

ffmpeg -i 05.mp4 -r 1 05_at_1fps.mp4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM