How to convert video to images (0~255) using background subtraction.

1 view (last 30 days)
Hello.
I wanna convert video to images (0~255).
When I used this code, I just got 0 or 1 (logical).
Can I get 0~255 instead of 0 or 1 (logical)? Thank you!
clear all
close all
%// read the video:
reader = VideoReader('lena_side.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:
for i=1:60
fIdx(i) = i; %// do it for frame 1 ~ 60
fg{i} = sum( abs( vid{fIdx(i)} - bg ), 3 ) > 0.25;
fg{i} = reshape(fg{i},[],1);
end
D1 = cell2mat(fg);

Accepted Answer

Ameer Hamza
Ameer Hamza on 11 Mar 2020
  4 Comments
Kong
Kong on 11 Mar 2020
Thank you so much!
Do you know the meaning (> 0.25;) ? what is the 0.25?
fg{i} = sum( abs( vid{fIdx(i)} - bg ), 3 ) > 0.25;
Ameer Hamza
Ameer Hamza on 11 Mar 2020
I am not sure about it, but it seems like a threshold, below which the values are filtered. By inspecting the matrices, i found that the term sum( abs( vid{fIdx(i)} - bg ), 3 ) is uint8, so its value are in range [0-255]. Therefore comparison with 0.25 does not make much sense. I think 0.25 was meant when pixel intensity is scaled between [0-1]. So it seems that you should multiply it by 255, i.e., use > 0.25*255.

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!