How to get background subtraction in video?

13 visualizaciones (últimos 30 días)
Sam Johnson
Sam Johnson el 18 de Sept. de 2018
I'm trying to get background subtraction to work on a video. I have an mp4 file and an image but when running the code it gives this error -
*_Index in position 2 is invalid. Array indices must be positive integers or logical values.
Error in test1 (line 12) bg = mean( J(4, vid{:}), 4 );_*
The code is:
apple=imread('apple2.jpg');
%// read the video:
reader = VideoReader(apple4.mp4');
vid = {};
while hasFrame(reader)
vid{end+1} = im2single(readFrame(reader));
end
%// simple background estimation
bg = mean( apple(4, vid{:}), 4 );
%// estimate foreground as deviation from estimated background:
fIdx = 43; %// do it for frame 43
fg = sum( abs( vid{fIdx} - bg ), 3 ) > 0.25;
figure;
subplot(131); imshow( bg );
subplot(132); imshow( vid{fIdx} );
subplot(133); imshow( fg );
How can I get this code to work?
Thanks

Respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by