analysis of non-parametric background subtraction algorithm
TRANSCRIPT
-
7/23/2019 Analysis of Non-parametric Background Subtraction Algorithm
1/26
Project Report: Analysis of Non-parametric Background
Subtraction Algorithm
Matthew Watson, Department of Electrical & Computer Engineering, University of Arizona,
Tucson, AZ 85721
-
7/23/2019 Analysis of Non-parametric Background Subtraction Algorithm
2/26
I. INTRODUCTION
Background subtraction is useful for detecting moving targets in a scene that is observed by a
stationary camera by marking pixels that observe no motion as the background and pixels that
are capturing motion as the foreground. Possible applications of background subtraction include
the detection of motion for surveillance cameras or for traffic measurement. Backgroundsubtraction algorithms, however, are often limited when there is un-modeled motion in the
background.
The non-parametric approach described in [1] allows for some unknown motion in the
background to be tolerated and accounted for, which is particularly useful in outdoor scenes.
The approach assumes that each individual pixel value over time may have its probability
distribution function modeled as Gaussian, and pixels are assigned to the background if the
probability that it belongs to the background surpasses a preset threshold. The probability
distribution function is continually updated by using the most recent sample of pixel values,
which allows it to adapt to long-term changes that may occur in the background.
As mentioned previously, the probability distribution of background pixels over time is assumed
to be normal, so the standard deviation of the normal distribution is estimated based on the
median of the set of values as described by equation 1 (from p6 of [1]). Finally, the set of
absolute pixel intensity differences between consecutive pixels, | xi - xi-1 |, is assumed to be
normally distributed since the pixel intensities are normally distributed. These absolute pixel
differences are computed and the median of this set is used in computation of equation 1 since
pixel differences are used in the computation of probability in equation 2.
(1)
If the assumption is made that each color channel is independent, the probability of a pixel
having intensity value xtis described by equation 2 (eq. 5 in [1]).
(2)
The variables in equation 2 are defined as follows:
N is the number of previous samples that are being used to form the distribution
d is the number of color channels being used (typically 3 for an RGB image)
jis the standard deviation of the sample of N values from the color channel indexed by j,
j2is also referred to as the pixel bandwidth for color channel j
xtjis the pixel intensity at the current time that is being considered in the color channel
indexed by j
-
7/23/2019 Analysis of Non-parametric Background Subtraction Algorithm
3/26
xijis the pixel intensity at time i in the color channel indexed by j
[1] also proposes a method for suppression of false detections that may be implemented. This
change involves adding an additional constraint to pixels that are considered to be motion pixels.
This modification to the algorithm requires that for a pixel to be considered a motion pixel, it's
value must have sufficiently low probability of occurrence in its own pixel as well as in a
neighborhood of pixels surrounding it. The intention of this modification is to allow for small
amounts of motion in the background to go undetected, which is ideal for ignoring the motion of
tree branches and leaves moved by wind.
The purpose of this paper is to analyze how the performance of the algorithm is affected by
variation in the number of pixels used to create the distribution, the threshold value used to make
background/foreground decisions, and the addition of the false detection suppression
modification described previously
II. PRACTICAL IMPLEMENTATION
The video that was used for testing the algorithm was captured using the camera of an iPhone 6
that was placed in a stationary position observing a sidewalk, street, parking lot, and surrounding
area. The video consisted of about 13 seconds at 32 fps in which the only motion that would be
ideally detected was that of a student walking down the sidewalk. To reduce the computational
complexity of the algorithm, the video images were down sampled twice by taking averages of
2x2 pixel blocks in two separate stages. This reduced the size of the video frame from
1920x1080 to 480x270 for easier processing.
A. Test Frame
The algorithms were tested on the entire video, but an ideal frame for performance
quantification was chosen midway through the video for easier analysis. That frame can be
viewed in figure 1a below. As mentioned before, the only motion that should ideally be detected
by the algorithm is that of the student on the sidewalk in the center of the frame.
For quantitative performance analysis, a "ground truth" motion mask had to be constructed. This
was done by manual inspection of the image, and the pixel areas that were considered to contain
true motion are denoted by the highlighted areas and can be viewed in figure 1b. Note that this
includes the student's shadow, which is detected by the algorithms used for this report, but [1]
proposes a method to eliminate detection of shadows that was not implemented for this report.
-
7/23/2019 Analysis of Non-parametric Background Subtraction Algorithm
4/26
(a) (b)
Fig. 1. (a) Frame used for performance quantification (b) Frame used for performance quantification with "groundtruth" change mask highlighted
It should also be noted that this method of performance quantification is imperfect due to the fact
that the "ground truth" motion mask does not completely adhere to the motion observed. [1]
proposes a method of performance quantification by applying a contrast shift to a known
subsection of pixels on a stationary background to test the efficacy of the algorithm, which
would allow for a perfect "ground truth" change mask to be constructed. This was attempted on
a similar video of the background that was captured with no moving student, but the contrast
could not be changed enough to obtain usable results with a uniform contrast change to an areaof pixels. This was most likely due to the background containing more unwanted motion than
this method could handle. Nonetheless, this imperfect motion mask still allows changes to the
parameters and algorithm to be accurately compared.
-
7/23/2019 Analysis of Non-parametric Background Subtraction Algorithm
5/26
B. Performance Metrics
The performance metrics used for analysis of the algorithm were obtained from [2]. The metrics
that are used in this analysis are precision, recall, accuracy, and F measure. The equations for
these are given as follows:
(3)
(4)
(5)
(6)
C. MatLab Implementation
The primary issues regarding practical implementation of the algorithm will be discussed here.
The first step in the algorithm is to create a list of the differences between consecutive pixels, as
the median of this list is necessary for a calculation of the standard deviation of the distribution.
In order to achieve this, a vector is created to store all of the pixel differences, the vector is
sorted so that a median may be easily extracted, and finally the standard deviation is calculated
from this set of medians for each pixel.
One practical issue that had to be overcome in the computation of these standard deviations is the
fact that for many pixels, the median change over the previous N samples is 0. This causes
problems for the probability calculation in equation 2, because a pixel bandwidth of 0 in any
color channel will result in an infinite probability, which will automatically determine that the
pixel is part of the background.
From this, a situation arises where a pixel may in reality be a motion pixel, but if the motionoccurred recently enough and the pixel had been very calm previously any pixel differences
caused by the motion will be above the median when put into ascending order and the median
may still be equal to 0. This represents a breakdown of the assumption that pixel values are
distributed in a Gaussian manner. In order to overcome this and allow a calculation of
probability to occur, all standard deviation values that were calculated to be 0 were replaced with
-
7/23/2019 Analysis of Non-parametric Background Subtraction Algorithm
6/26
a small number that was reasonable and could be expected to occur based on other pixels (0.1)
that would allow accurate calculation to occur.
Once pixel bandwidths have been determined, the probabilities of the pixels in the frame may be
computed. This was accomplished by iterating over every pixel and applying equation 2. If a
pixel exceeds the probability threshold, it is assigned as a stationary pixel, and is otherwise is
assigned to be a motion pixel. One practical way suggested in [1] to speed up computation is to
exit the summation in equation 2 as soon as the threshold is reached to avoid unnecessary
computation. This approach was implemented and may be seen as part of the code block in
figure 2 that displays the algorithm.
%iterate through each pixel and calculate the probability that the%observed pixel value at the current frame should have occurred.%Compare that probability to a threshold value, and if it is%sufficiently high that pixel can be considered part of the background
%by setting motionMatrix to 0 and moving to the next pixel. If the%probability is low enough, set the corresponding motionMatrix pixel to%255 to indicate that it is a foreground pixelforii = 1:480
forjj = 1:270probability = 0;product =
1/(sqrt(8*pi*pi*pi*bandwidths(ii,jj,1)^2*bandwidths(ii,jj,2)^2*bandwidths(ii,
jj,3)^2));fori = (testFrame-N+1):testFrame
probability = probability + (1/N)*product*...exp(-(double(video(ii,jj,1,testFrame))-
double(video(ii,jj,1,i)))^2/2/bandwidths(ii,jj,1)^2)...
* exp(-(double(video(ii,jj,2,testFrame))-double(video(ii,jj,2,i)))^2/2/bandwidths(ii,jj,2)^2) ...* exp(-(double(video(ii,jj,3,testFrame))-
double(video(ii,jj,3,i)))^2/2/bandwidths(ii,jj,3)^2);
if(probability > threshold)motionMatrix(ii,jj,testFrame) = 0;break
elseif(i == testFrame)motionMatrix(ii,jj,testFrame) = 255;
endend
endend
Fig. 2. The background subtraction probability calculation
An additional practical suggestion in [1] that was not implemented but can be used to speed up
computation is to create a lookup table for values of the exponential term in equation 2 to
expedite the probability calculation.
-
7/23/2019 Analysis of Non-parametric Background Subtraction Algorithm
7/26
For the implementation of the algorithm including false detection suppression, an additional pair
of for loops had to be added inside the probability calculation loops so that the neighborhood of
each pixel could be checked as well. This added considerable computational complexity to the
program and caused it to perform much more slowly. The background subtraction with false
detection suppression probability code can be viewed in figure 3.
%This time additional conditions are imposed on positive detections.%Each pixel is compared not only to its own probability estimation, but%also to the probability estimation of the 5x5 matrix around the pixel.%If the pixel value is determined to be part of the background for any%one of those neighborhood pixels, it is considered a background pixel.forii = 3:478
forjj = 3:268nbhdProbabilities = zeros(5,5);flag = 0;foriii = 1:5
forjjj = 1:5
product = 1/(sqrt(8*pi*pi*pi*bandwidths((ii-3+iii),(jj-3+jjj),1)^2*bandwidths((ii-3+iii),(jj-3+jjj),2)^2*bandwidths((ii-3+iii),(jj-
3+jjj),3)^2));fori = (testFrame-N+1):testFrame
nbhdProbabilities(iii,jjj) =
nbhdProbabilities(iii,jjj) + (1/N)*product*exp(-
(double(video(ii,jj,1,testFrame))-double(video((ii-3+iii),(jj-
3+jjj),1,i)))^2/2/bandwidths((ii-3+iii),(jj-3+jjj),1)^2)...* exp(-(double(video(ii,jj,2,testFrame))-
double(video((ii-3+iii),(jj-3+jjj),2,i)))^2/2/bandwidths((ii-3+iii),(jj-
3+jjj),2)^2) ...* exp(-(double(video(ii,jj,3,testFrame))-
double(video((ii-3+iii),(jj-3+jjj),3,i)))^2/2/bandwidths((ii-3+iii),(jj-
3+jjj),3)^2);
if(nbhdProbabilities(iii,jjj) > threshold)flag = 1;break
endend
endendif(max(nbhdProbabilities(:)) < threshold && flag == 0)
motionMatrix(ii,jj,testFrame) = 255;end
endend
Fig. 3. The background subtraction probability calculation with false detection suppression
After this processing was complete the next step was to compute the number of true positives
and negatives and false positives and negatives, as these values are necessary to compute the
final quality metrics. This is done relatively simply by first computing the total number of
-
7/23/2019 Analysis of Non-parametric Background Subtraction Algorithm
8/26
positives in the image and the total number of positives that are within the predefined motion
mask. Finally, the results are plotted for analysis.
III. RESULTS
A. Effect of False Detection Suppression
As has been discussed previously, some false detections may be suppressed by comparing pixel
values with a neighborhood and determining to determine if their probability is out of the
ordinary for that neighborhood. The test frame with near-optimal threshold and number of
samples used, with and without false detection suppression may be viewed in figure 4 below.
(a) (b)
Fig. 4. (a) Background subtraction at near-optimal th and N (b) Background subtraction at near-optimal th and N
with false detection suppression
These images were created using a 5x5 neighborhood for the false detection suppression. It can
be observed that the image in 4b has far fewer false detections than the image in 4a, especially in
the low-contrast areas like the sky and the sidewalk, but at a cost of considerable computational
-
7/23/2019 Analysis of Non-parametric Background Subtraction Algorithm
9/26
complexity (a 5x5 neighborhood will require 25 times as much computation compared to no
suppression).
It should also be noted that while the false detection suppression helped considerably with large
portions of the background, portions of the background that had considerable consistent motion,
such as the palm trees, were not able to be suppressed by this method. This is one of the
shortcomings of this algorithm. If there are pixels that experience continuous, approximately
periodic motion, such as the buffeting of the palm trees in the wind, this background motion will
be fairly difficult to suppress because the list of pixel changes over the previous N samples will
contain a high number of large changes, which will lead to a high median and in turn a high
bandwidth and a low calculated probability that will often be underneath the threshold.
Further quantification of these results will be presented in the following subsections.
B. Effect of Threshold Value
The threshold value is a number that must be chosen to make decisions about whether a given
pixel will be part of the background or foreground. Pixels that are determined to have a
probability of occurrence greater than the threshold value are considered to be part of the
background, while pixels that do not are considered to be part of the foreground.
Increasing the threshold value will obviously cause more pixels to be detected as in motion. The
optimal threshold value was explored using 130 previous samples for computation, or N =130.
The precision, recall, accuracy, and F Measure for a range of threshold values with falsedetection suppression disabled and enabled may be viewed in figures 5, 6, 7, and 8, respectively.
(a)
-
7/23/2019 Analysis of Non-parametric Background Subtraction Algorithm
10/26
(b)
Fig. 5. (a) Precision vs. Threshold with constant N (b) Precision vs. Threshold with constant N and falsesuppression
It can be observed that the precision begins at 0, reaches its maximum at a certain value (this
maximum will vary with N), and then declines as it is continually increased. This is due to the
fact that at very low values of threshold, no pixels will be detected as positive anywhere in the
image, which leads to 0 true positives and therefore 0 precision. When the threshold is increased
to a point where the real motion is detected, the precision jumps to its maximum value because a
maximum number of true positives are detected. As it is continually increased, however, more
background pixels are detected as in motion which increases the number of false positives, which
in turn causes a decline in precision.
It should be noted that the precision maximum was about 0.15 with false suppression and about
0.095 without, or about a 58% improvement in precision due to false suppression. This indicates
that false positives are reduced by false suppression at a much greater rate than true positives.
-
7/23/2019 Analysis of Non-parametric Background Subtraction Algorithm
11/26
(a)
(b)
Fig. 6. (a) Recall vs. Threshold with constant N (b) Recall vs. Threshold with constant N and false suppression
Recall is 0 for small thresholds for the same reason as precision: true positives have not been
detected yet. When the threshold moves to the point where it detects the majority of the true
positives, it jumps higher and continues to rise slightly as more true positives are detected. In
fact, the slight increase of recall with increasing threshold on these graphs is likely misleading
because of the imperfect ground truth used to determine what pixels were truly in motion. In
reality, recall would behave more closely to a step function than what is shown in figure 6.
It is also interesting to note that recall performance is actually slightly harmed by false
suppression. This is due to the fact that recall does not use false positives as a factor so it could
-
7/23/2019 Analysis of Non-parametric Background Subtraction Algorithm
12/26
not be aided by false suppression and false suppression leads to a slight increase in false
negatives instead, which harms recall.
(a)
(b)
Fig. 7. (a) Accuracy vs. Threshold with constant N (b) Accuracy vs. Threshold with constant N and false
suppression
Accuracy steadily declines as the threshold is increased in this simulation. This is an unfortunate
result of the fact that for the scene that was observed, the number of true negatives dominates
equation 5 because the amount of true motion is much smaller than the size of the scene.
-
7/23/2019 Analysis of Non-parametric Background Subtraction Algorithm
13/26
Because of this, increasing the threshold decreases the number of true negatives much more
quickly than it increases the number of true positives.
False suppression benefits accuracy as expected, because it maintains a comparatively higher
number of true negatives by reducing false positives for a given threshold value.
(a)
(b)
Fig. 8. (a) F Measure vs. Threshold with constant N (b) F Measure vs. Threshold with constant N and false
suppression
-
7/23/2019 Analysis of Non-parametric Background Subtraction Algorithm
14/26
F measure combines precision and recall for the most reliable and comprehensive performance
metric of the four. Since recall may be approximated as a step function, the behavior of F
measure vs. threshold value mimics the behavior of precision vs. threshold value.
F measure is noticeably improved by false suppression: the maximum with it is about 0.25 and
without it is about 0.17, for an overall performance increase of about 47% attributed to false
suppression.
C. Effect of the Number of Previous Samples Used for Computation
Changing the number of previous samples used to compute the pixel probabilities will affect the
results in a number of ways. A low number of samples allows motion to be detected more easily,
because it becomes more likely that the effects of that motion will be seen at the median which in
turn affects the pixel bandwidth calculation. However, if the sample number is too low, morefalse positives will be detected because pixel noise in the background will have a larger effect on
the calculation of probability. The precision, recall, accuracy, and F measure as a function of N
with threshold = 2 may be viewed in figures 9, 10, 11, and 12, respectively.
(a)
-
7/23/2019 Analysis of Non-parametric Background Subtraction Algorithm
15/26
(b)
Fig. 9. (a) Precision vs. #samples with constant threshold (b) Precision vs. #samples with constant threshold and
false suppression
For very low N, the precision starts at a local maxima because some true positives are detected
due to the motion appearing at the median and yielding relatively high bandwidth. However, this
is not the global maxima because there is also a large number of false positives detected at low N
because small, random variations in the scene that should be unnoticed are able to appear at the
median. As N is increased from this point, precision falls because the rate at which true positives
and false positives fall is approximately equal, but there are so many more false positives that
they begin to occupy a greater percentage of the total positives detected.
When the N at which most of the motion is detected is reached, however, a vast majority of the
true motion is detected with a minimal number of false positives, yielding the global maxima for
precision. This occurs at the optimum location where pixel differences due to pixel noise that
would lead to false positives are moved away from the median of the set of recent values and are
also minimized due to their averaging with a larger sample size. This optimum location is also
that which allows motion pixels to have the maximum impact by influencing the median and
being a large portion of the recent distribution.
As N is increased past this optimum value, precision begins to again fall, this time because real
motion is moved away from the median where it can influence the bandwidth calculation and the
real motion also becomes a smaller part of the probability calculation.
-
7/23/2019 Analysis of Non-parametric Background Subtraction Algorithm
16/26
The saw-tooth pattern as the precision falls is due to the false positive rate temporarily
decreasing and then again increasing, which is most likely a result of approximately periodic
motion in the background caused by wind. The false positive rate decreases as the motion is
moved away from the median, but at certain tipping points another period of motion is added to
the list which increases the false positive rate further.
The precision increase at the optimum N due to false suppression is identical to that of the
threshold sweep, at about 53%, for the same reasons discussed there.
(a)
-
7/23/2019 Analysis of Non-parametric Background Subtraction Algorithm
17/26
(b)
Fig. 10. (a) Recall vs. #samples with constant threshold (b) Recall vs. #samples with constant threshold and false
suppression
Recall begins at a very small local minima and decreases until the optimum N is reached f
because only a very small number of true positives are registered and initially fall off with
increasing N for the same reasons as with precision. At the optimum N most of the true positives
are detected and the true positive rate continues to faux-increase (see discussion of recall forthreshold sweep). There is no new phenomena occurring in the recall vs. N results that has not
already been discussed.
There is a small performance loss due to false suppression for the same reasons discussed in the
threshold sweep discussion.
-
7/23/2019 Analysis of Non-parametric Background Subtraction Algorithm
18/26
(a)
(b)
Fig. 11. (a) Accuracy vs. #samples with constant threshold (b) Accuracy vs. #samples with constant threshold and
false suppression
Accuracy also behaves very similarly to the way it did in the threshold sweep except that it is not
monotonically decreasing as it did in the threshold sweep. The increases are due to temporary
-
7/23/2019 Analysis of Non-parametric Background Subtraction Algorithm
19/26
increases in the true negative rate that occur due to periodic motion being minimized during that
portion of the sweep with the same mechanics discussed with precision.
Accuracy is also improved with false suppression in a similar manner as the threshold sweep.
(a)
(b)
Fig. 12. (a) F Measure vs. #samples with constant threshold (b) F Measure vs. #samples with constant threshold
and false suppression
-
7/23/2019 Analysis of Non-parametric Background Subtraction Algorithm
20/26
The F measure, being a combination of precision and recall, again behaves mostly as precision
does because recall acts as approximately a step function.
The maximum F measure attained by the algorithm without false suppression is about 0.135,
whereas with false suppression is about 0.2, yielding a performance increase of 48%, once again
almost identical to the performance increase previously determined with the threshold sweep.
IV. CONCLUSIONS
A. Algorithm Shortcomings and Potential Improvements
One of the primary shortcomings of the background subtraction algorithm as it was implemented
is that consistent periodic motion in the background is difficult to remove because it leads to high
pixel bandwidths at the pixels where it occurs and therefore relatively low probability numbers.One approach to removing the detection of this motion would be to increase the neighborhood
size for false detection suppression so it would hopefully include the full range of the
periodically moving object. The problem with this approach, however, is that larger
neighborhoods will lead to more suppression of true positive pixels. There is a remedy to this
problem suggested in [1] which involves using blob detection to determine what the in-motion
object is and to prevent pixels that are a part of that object from being suppressed.
Additionally, there is another limitation on the algorithm in that the optimum threshold and N
values for a given situation will vary depending on the speed of the motion that it is desirable to
detect. For example, a very fast-moving object will not register very much if there is a high Nbecause the pixels changes due to motion will all exist above the median of previous sample
changes and will therefore not affect the bandwidth calculation and will also be drowned out in
the probability calculation due to the previous N samples containing mostly background pixels.
On the other hand, if N is very small, fast motion would be detected but there will also be a large
number of false positives due to pixel noise and other random variations being magnified.
Additionally, slow motion may not register because the moving object will become part of the
background.
A solution to this problem is also presented in [1] by a selective update mechanism, which only
adds new samples to the list of N values if they fit into two separate models simultaneously. The
first model contains only recent sample values taken consecutively but does not include pixels
that are considered to be in motion. The second model contains N values that are taken
periodically, skipping a predefined number of samples in between to create a stable
representation of the background. The N previous sample values that are used for calculation,
then, are those that are registered in both models.
-
7/23/2019 Analysis of Non-parametric Background Subtraction Algorithm
21/26
B. Computational Speed
The algorithm as it was implemented is relatively inefficient, with approximately 26N
multiplications, 4N adds, N square root calculations, and 3N exponential calculations per pixel.
As mentioned previously, the exponential calculations can be performed via a lookup table with
pre-computed values to reduce computation. The addition of a false suppression algorithm
brings the computational complexity to M2times the original complexity if M is the size of the
neighborhood used.
Methods for reducing this complexity include branching out of the summation in equation 2 as
soon as the probability is exceeded, and in the case of the false suppression algorithm to branch
out of the entire neighborhood calculation as soon as probability is exceeded on one pixel.
C. Test Results
It was shown that for the video that was tested there exist combinations of optimum N and
threshold values that will vary with each other. The effects on precision, recall, accuracy, and F
measure outside of these optimum values was also explored.
These optimum values will depend on a variety of factors, such as the characteristics of
unwanted motion in the background and characteristics of the motion that is desired to be
detected. Because of this, optimum N and threshold values will vary with application.
The implementation of a false suppression modification to the algorithm allowed for a 53%increase in precision and a 48% increase in F measure at optimum N and threshold, due to an
observable drop in false positive values that is visualized in figure 4.
REFERENCES
[1] Elgammal, Ahmed, David Harwood, and Larry Davis. "Non-parametric Model for
Background Subtraction." Lecture Notes in Computer Science Computer Vision ECCV 2000
(2000): 751-67. Web.
[2] "Precision and Recall." Wikipedia. Wikimedia Foundation, n.d. Web. 08 Dec. 2015.
[3] "Background Subtraction." Wikipedia. Wikimedia Foundation, n.d. Web. 08 Dec. 2015.
-
7/23/2019 Analysis of Non-parametric Background Subtraction Algorithm
22/26
APPENDICES
Appendix A. Sample MatLab Simulation Code
%ECE 533 Project%Matt Watson
%import the video that will be used for processingload('533VideoMotion.mat'); %the variable name is video
%determine the number of frames that are present in the whole videonumFrames = size(video);numFrames = numFrames(4);
%set up the vectors to store results as well as constants for simulationNVec = 4:4:112;trueNeg = zeros(1,28);truePos = zeros(1,28);
falseNeg = zeros(1,28);falsePos = zeros(1,28);threshold = 2;index = 1;testFrame = 151;
%the outermost for loop will iterate over all of the N values%defined by NVec and record the number of true negatives, true%positives, false negatives, and false positivesforN = NVec
clear differenceVector;clear sortedVec;clear medians;clear bandwidths;
%The probability distributions for each pixel are based on the%differences between consecutive pixels. differenceVector will be used%to store those values for each pixeldifferenceVector = zeros(480,270,3,N-1);
%Determine an initial vector of pixel differences for each color channel
at each%pixel location
fort = (testFrame-N):testFramedifferenceVector(:,:,:,t-(150-N)) = abs(video(:,:,:,t-(149-N))-video(:,:,:,t-(150-N)));
end
%The next step in the algorithm uses the median of the various pixel%differences to perform a bandwidth estimation for each pixel. The%median is determined by sorting the differenceVector and selecting the%average of the middle 2 elements (assumes N is even)sortedVec = sort(differenceVector,4);
-
7/23/2019 Analysis of Non-parametric Background Subtraction Algorithm
23/26
medians(:,:,:) = (sortedVec(:,:,:,N/2) + sortedVec(:,:,:,N/2-1))/2;bandwidths(:,:,:) = medians/0.68/sqrt(2);
%A bandwidth of 0 is not realistic and will cause the probability%estimation to become infinite. to avoid this, assign a reasonably%small bandwidth value to all bandwidths that were previously 0bandwidths(bandwidths==0) = 0.1;
%create a matrix that will contain the detection results at each pixelmotionMatrix = uint8(zeros(480,270,numFrames)); %used to determine if
motion occurred at each pixel
%The difference vector must be updated at each new frame and new%bandwidths must be calculated based on the updated value that has been%shifted into the vector of most recent sample values
%circularly shift the differenceVector so that the last element is the%oldest element and may be replaced by the newest element
differenceVector = circshift(differenceVector, -1, 4);
%calculate the newest value to be added to the differenceVectordifferenceVector(:,:,:,N-1) = abs(video(:,:,:,testFrame+1)-
video(:,:,:,testFrame));
%resort the vector, find the new medians and the new bandwidthssortedVec = sort(differenceVector,4);medians(:,:,:) = (sortedVec(:,:,:,N/2) + sortedVec(:,:,:,N/2-1))/2;bandwidths(:,:,:) = medians/0.68/sqrt(2);bandwidths(bandwidths==0) = 0.1;
%iterate through each pixel and calculate the probability that the%observed pixel value at the current frame should have occurred.%Compare that probability to a threshold value, and if it is%sufficiently high that pixel can be considered part of the background%by setting motionMatrix to 0 and moving to the next pixel. If the%probability is low enough, set the corresponding motionMatrix pixel to%255 to indicate that it is a foreground pixel
%This time additional conditions are imposed on positive detections.%Each pixel is compared not only to its own probability estimation, but%also to the probability estimation of the 5x5 matrix around the pixel.%If the pixel value is determined to be part of the backgroundfor any%one of those neighborhood pixels, it is considered a background pixel.
forii = 3:478forjj = 3:268
nbhdProbabilities = zeros(5,5);flag = 0;foriii = 1:5
forjjj = 1:5product = 1/(sqrt(8*pi*pi*pi*bandwidths((ii-3+iii),(jj-
3+jjj),1)^2*bandwidths((ii-3+iii),(jj-3+jjj),2)^2*bandwidths((ii-3+iii),(jj-
3+jjj),3)^2));fori = (testFrame-N+1):testFrame
-
7/23/2019 Analysis of Non-parametric Background Subtraction Algorithm
24/26
nbhdProbabilities(iii,jjj) =
nbhdProbabilities(iii,jjj) + (1/N)*product*exp(-
(double(video(ii,jj,1,testFrame))-double(video((ii-3+iii),(jj-
3+jjj),1,i)))^2/2/bandwidths((ii-3+iii),(jj-3+jjj),1)^2)...* exp(-(double(video(ii,jj,2,testFrame))-
double(video((ii-3+iii),(jj-3+jjj),2,i)))^2/2/bandwidths((ii-3+iii),(jj-
3+jjj),2)^2) ...* exp(-(double(video(ii,jj,3,testFrame))-
double(video((ii-3+iii),(jj-3+jjj),3,i)))^2/2/bandwidths((ii-3+iii),(jj-
3+jjj),3)^2);
if(nbhdProbabilities(iii,jjj) > threshold)flag = 1;break
endend
endendif(max(nbhdProbabilities(:)) < threshold && flag == 0)
motionMatrix(ii,jj,testFrame) = 255;
endend
end
%set up the variables to count the various metrics that will be used to%quatify the resultstruePositives = 0;trueNegatives = 0;falsePositives = 0;falseNegatives = 0;totalPositives = 0;
%calculate the total number of positive motion results in the detection
forii = 1:480forjj = 1:270
if(motionMatrix(ii,jj,testFrame) == 255)totalPositives = totalPositives + 1;
endend
end
%calculate the number of true positives (positives in the predefined%region that should be detected)forii = 268:282
forjj = 150:160if(motionMatrix(ii,jj,testFrame) == 255)
truePositives = truePositives + 1;endend
end
forii = 283:305forjj = 138:157
if(motionMatrix(ii,jj,testFrame) == 255)truePositives = truePositives + 1;
end
-
7/23/2019 Analysis of Non-parametric Background Subtraction Algorithm
25/26
endend
forii = 306:356forjj = 145:165
if(motionMatrix(ii,jj,testFrame) == 255)truePositives = truePositives + 1;
endend
end
forii = 357:360forjj = 155:163
if(motionMatrix(ii,jj,testFrame) == 255)truePositives = truePositives + 1;
endend
end
forii = 361:373forjj = 160:181
if(motionMatrix(ii,jj,testFrame) == 255)truePositives = truePositives + 1;
endend
end
forii = 374:386forjj = 161:190
if(motionMatrix(ii,jj,testFrame) == 255)truePositives = truePositives + 1;
endend
end
forii = 384:392forjj = 191:202
if(motionMatrix(ii,jj,testFrame) == 255)truePositives = truePositives + 1;
endend
end
%calculate the value of each measuretruePos(index) = truePositives;falsePos(index) = totalPositives - truePositives;
trueNeg(index) = 127084 - falsePos(index);falseNeg(index) = 2516 - truePositives;
%display the current index so we can see the progress of the program as%it runsdisp(index)index = index+1;
end
-
7/23/2019 Analysis of Non-parametric Background Subtraction Algorithm
26/26
%calculate the final quantification metricsprecision = truePos./(truePos + falsePos);recall = truePos./(truePos + falseNeg);accuracy = (truePos + trueNeg)./(truePos + trueNeg + falsePos + falseNeg);fMeasure = 2*precision.*recall./(precision + recall);
%plot the quantification metricsfigureplot(NVec, precision);title('Precision vs. Number of Previous Samples Used, Threshold = 2');xlabel('Number of Previous Samples Used');ylabel('Precision');
figureplot(NVec, recall);title('Recall vs. Number of Previous Samples Used, Threshold = 2');xlabel('Number of Previous Samples Used');ylabel('Recall');
figureplot(NVec, accuracy);title('Accuracy vs. Number of Previous Samples Used, Threshold = 2');xlabel('Number of Previous Samples Used');ylabel('Accuracy');
figureplot(NVec, fMeasure);title('F Measure vs. Number of Previous Samples Used, Threshold = 2');xlabel('Number of Previous Samples Used');ylabel('F Measure');