简体   繁体   中英

How to extract frames at particular intervals from video using matlab

I am using matlab 2013a software for my project.

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. In the code I have used step() to slice the video into frames.

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. I want to process frames at the rate of 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.

It cannot be done directly in Matlab 2013a, because the video access library does not provide the feature you want. 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)

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.

The ffmpeg command might look something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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