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

2 次查看(过去 30 天)
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);

采纳的回答

Ameer Hamza
Ameer Hamza 2020-3-11
  4 个评论
Kong
Kong 2020-3-11
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 2020-3-11
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.

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by