Answered step by step
Verified Expert Solution
Question
1 Approved Answer
1 . For this lab you are going to implement a background subtraction solution for the included video clip. 2 . Since we ve been
For this lab you are going to implement a background subtraction solution for the included video
clip.
Since weve been mainly working with images up to this point, well keep things simple and
break down our video into several images. Recall that a video is really a sequence of images that
are rapidly displayed one after another. The following matlab code will break your video into
multiple image files:
mov VideoReaderYourVideoFileNamewmv;
for i:mov.NumberOfFrames
img readmovi;
bwImage rgbgrayimg;
outputFileName sprintfbwImagedjpgi;
imwritebwImage outputFileName, jpg;
end
You will want to run the above code only once on your video file. This step is typically called
preprocessing.
There are several different methods available for background subtraction, and were going to
focus on a basic approach. A basic approach is to track the last pixel values of each video
frame. If the intensity of the pixel changes either brighter or darker by a set threshold, we will
flag this pixel as a foreground pixel. In order to do this, you will need to know the dimensions of
the video you will be using and create a matrix of those dimensions with a third dimension of
value For example, if the video you are using is x you will want to create a matrix
of size xx A matrix of this size will allow us to keep track of the last values for
each pixel.
If youre not sure on the size of your video, you can read in one of the image files and check its
dimensions:
myTest imreadbwImagejpg;
sizemyTest
ans
The image above is by but Matlab flips the horizontal and vertical so it displays it as
shown above.
Read in the first frames to create a matrix that you will track the last frames viewed. This
matrix will be used to calculate the average value at each pixel location.
What you want to do next is create a loop that will run through each of the image files that you
created. In each of these iterations you will want to do the following:
a Compare each of the pixel values height by width to the average value stored in the
average matrix. If the absolute value of the difference is greater than some threshold
start with threshold set the value of that correspond pixel in the newImage to be
white intensity value If its less than the threshold you will set the value of that
corresponding pixel in the newImage to be black intensity value If the pixel value is
white, it is a foreground pixel, if its set to black, its a background pixel.
b Update the average matrix by replacing the oldest image in the matrix with the current
image.
When youve created a new set of images, youll want to combine them back together again
Please note you must first create a directory called modifiedImages
mov VideoReaderbaseballmp;
totalImages ;
videoFrameRate mov.FrameRate;
videoHeight mov.Height;
videoWidth mov.Width;
outputVideo VideoWritermyfileavi';
outputVideo.FrameRate videoFrameRate;
openoutputVideo;
for i:totalImages
outputFileName sprintfmodifiedImagesbwImagedjpg i;
myImage imreadoutputFileName;
writeVideooutputVideo myImage;
end
closeoutputVideo
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