how to extract and update background from video sequence

8 views (last 30 days)
I have a video about traffic scene. now, I want to calculate the percentage of vehicle area on the road area( or the percentage of area foreground on the area background). and the first step for this work is background extraction. I have read many document and Scientific articles about it, one of them recommend me use the mean filter following this formula.
this is link of that article: Scientific article and the results of them are very good, it is exactlly what I want.
I follow his formula and I try to write my code. but It not work! who can help me of give me some advices. this is my code:
vob = VideoReader('NKKN.avi');
frame = vob.read(inf);
vidHeight = vob.Height;
vidWidth = vob.Width;
nFrames = vob.NumberOfFrames;
%%%First-iteration background frame
background_frame = double(frame*0);
redbackground_frame = background_frame(:,:,1);
greenbackground_frame = background_frame(:,:,2);
bluebackground_frame = background_frame(:,:,3);
%calculate background
i = 0;
for k = 1:10 %get background from 10 frame (J=10)
thisframe = double(read(vob, k));
%background_frame = background_frame + thisframe;
redbackground_frame = redbackground_frame + thisframe(:,:,1);
greenbackground_frame = greenbackground_frame + thisframe(:,:,2);
bluebackground_frame = bluebackground_frame + thisframe(:,:,3);
i=i+1;
disp(i);
end
A = redbackground_frame/i;
B = greenbackground_frame/i;
C = bluebackground_frame/i;
this is my video sequence: video_object

Answers (1)

Image Analyst
Image Analyst on 3 Aug 2016
I do something similar to this in my attached demo. But my demo is a weighted average of prior frames instead of calculated in advance. Let me know if you have any questions.
  11 Comments
Malik Zulqarnain
Malik Zulqarnain on 5 May 2018
Edited: Malik Zulqarnain on 5 May 2018
i uses the code that you given above ExtractMoviesFrame.m(i have attached the file) and vedio i uses is taken by stationary camera
Image Analyst
Image Analyst on 5 May 2018
Well maybe the algorithm that used for determining the background is not what you want. Try the mode method, if you have enough memory. Or, if you have the Computer Vision System Toolbox, try some of their examples that do that: https://www.mathworks.com/products/computer-vision/code-examples.html

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!