How to use image background subtraction in video

66 views (last 30 days)
I want to background subtraction in the video.
The problem is that the first frame of some videos is not empty. I mean, there is a person in the first frame of video.
So when I use background subtraction, the result is not as good as below picture.
Do you have a great idea to solve it?
I think that I can use background image.
(Bend dataset)
I want to get this kind result. (the dataset is different)
(Side dataset)
clear all
close all
%// read the video:
reader = VideoReader('shahar_bend.avi');
vid = {};
while hasFrame(reader)
vid{end+1} = im2single(readFrame(reader));
end
%// simple background estimation using mean:
bg = mean( cat(4, vid{:}), 4);
%// estimate foreground as deviation from estimated background:
fIdx = 40; %// do it for frame 43
fg1 = sum( abs( vid{fIdx} - bg ), 3 ) > 0.25;
fg2 = imresize(fg1, 0.5);
figure;
subplot(141); imshow( bg );
subplot(142); imshow( vid{fIdx} );
subplot(143); imshow( fg2 );

Accepted Answer

Raynier Suresh
Raynier Suresh on 19 Mar 2020
To do background subtraction but without knowing the background you could try image segmentation to remove the background. Many different image segmentation algorithms are available in MATLAB.
For Image Segmentation in MATLAB you could refer this link :
The vision.ForegroundDetector might help you to get the foreground object using the Gaussian Mixture Models” .
For “vision.ForegroundDetector” in MATLAB you could refer this link:
Referring to the following links might be helpful:
Image Segmentation using K-Means Clustering:
  1 Comment
Kong
Kong on 19 Mar 2020
Edited: Kong on 19 Mar 2020
Hello. Thank you so much!
vision.ForegroundDetector is so amazing!
Could you let me know how to get several images inbox as a matrix?
I want to save these images as matrix.
I want to get this image.
videoSource = VideoReader('shahar_run.avi');
detector = vision.ForegroundDetector(...
'NumTrainingFrames', 5, ...
'InitialVariance', 30*30);
blob = vision.BlobAnalysis(...
'CentroidOutputPort', false, 'AreaOutputPort', false, ...
'BoundingBoxOutputPort', true, ...
'MinimumBlobAreaSource', 'Property', 'MinimumBlobArea', 250);
shapeInserter = vision.ShapeInserter('BorderColor','White');
videoPlayer = vision.VideoPlayer();
while hasFrame(videoSource)
frame = readFrame(videoSource);
fgMask = detector(frame);
bbox = blob(fgMask);
out = shapeInserter(fgMask,bbox);
videoPlayer(out);
pause(0.1);
end

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!