How to extract videos after every 1 min interval and then how to subtract specific number of frames from background image.

5 views (last 30 days)
The Code is :
clear all clc;
Z = imread('background.tif');
VR=VideoReader('traffic02.avi');
NumInFrames=get(VR,'NumberOfFrames');
count=0;
for (frame=1:60:150)
diff1 =read(VR,frame)-Z;
idx=diff1==0;
out=sum(idx(:));
% if (diff1==0)
% count=count+1;
%end
break;
end
disp(out);

Accepted Answer

Image Analyst
Image Analyst on 24 May 2015
I don't understand why your code is not doing what you want. Please clarify.
I attach my demo for an example.
  6 Comments
Ilan Moshe
Ilan Moshe on 3 Jun 2020
Hi :)
How can I control the interval time between each frame. It seem that with your demo, we're getting 30 pics per sec, is'nt ?
If I want 1 pic per sec, how can I change it ?
thanks
Image Analyst
Image Analyst on 3 Jun 2020
If you want to change the frame rate in a new movie you create, then
  1. Create the VideoReader object
  2. Create a new VideoWriter object
  3. Specify the frameRate property of the VideoWriter object to be what you want (1 frame per second)
  4. Read in the frames one by one from the the VideoReader object
  5. Write the frames one by one to the ouput VideoWriter object
  6. Close both the VideoReader object and the VideoWriter object

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 24 May 2015
You need to pass read() the frame number, but instead you are trying to pass it a time in seconds. You need to get the FrameRate property and multiply that by 60 seconds to get the frame number.
Caution: in general, the frame rate will not necessarily be an integer, so the frames corresponding to "1 minute apart" will not necessarily always be the exact same number of frames apart. You can still use
for frame = 1 : 60*FrameRate : 1000
but you need to round() or ceil() the frame number to be sure it is integral. Another way is:
minutes = 0 : 5; %capture ending at 5 minutes
framenums = unique(1 + floor(minutes * 60 * FrameRate));
for frame = framenums
...
end
The unique() avoids reading the same frame twice in a row for situations in which the FrameRate is less than two per minute.
  2 Comments
Image Analyst
Image Analyst on 25 May 2015
When he says
for (frame=1:60:150)
Why do you think that's the time in seconds rather than the frame number? Of course it's kind of weird that he'd read only 3 frames: frame #1, #61, and #121. Maybe he should do frame=1:NumInFrames to do all the frames.
Walter Roberson
Walter Roberson on 25 May 2015
I assume it is in seconds because the poster asked to read at 1 minute intervals. That would only correspond to 60 frames if the FrameRate was 1 per second exactly, which is not impossible but seems unlikely.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!