Question
im trying to make this code to work as a bacground subtraction for motion detection in matlab but it only reads the first frame and
im trying to make this code to work as a bacground subtraction for motion detection in matlab but it only reads the first frame and make it grayscale and put up a difference between the same frame. can someone please help me loop it in away that it would go through all the frames thankyou!
%Algorithm: %Read the input Video. %Start reading the frames and consider first frame image %as background image and convert it to Gray Image format. %Determine the size of the video. %Read the consecutive frames in the looping mode. %Declare the Optimal threshold value. %Determine the absolute difference between the Background image and the current frame. %If the difference in the pixel value is greater than the threshold level, then consider it as a Foreground %Content (assign value 1 for white), else Background %Content (assign value 0 for black).
v = VideoReader('IMG_6976.m4v'); NumOfFrames = v.NumberOfFrames; vidHeight = v.Height; vidWidth = v.Width; vidResize = zeros(vidHeight, vidWidth);
FirstFrame = read(v,1); bg_bw = rgb2gray(FirstFrame); info = get(v);
for ii = 1:v.NumberOfFrames %fr = read(v, [1 Inf]); fr = read(v, ii); fr_bw = rgb2gray(fr); OptimalThreshold = 25; absdiff = abs(double(fr_bw) - double(bg_bw)); for j=1:vidWidth for k=1:vidHeight if ((absdiff(k,j) > OptimalThreshold)) vidResize(k,j) = fr_bw(k,j); else vidResize(k,j) = 0; end end end
end bg_bw = fr_bw; figure(1),subplot(3,1,1),imshow(fr) subplot(3,1,2),imshow(fr_bw) subplot(3,1,3),imshow(uint8(vidResize))
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started