How to use image background subtraction in video

조회 수: 27 (최근 30일)
Kong
Kong 2020년 3월 12일
편집: Kong 2020년 3월 19일
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 );

채택된 답변

Raynier Suresh
Raynier Suresh 2020년 3월 19일
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
Kong
Kong 2020년 3월 19일
편집: Kong 2020년 3월 19일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by