|
|
@ -40,9 +40,7 @@ Code |
|
|
|
In the following you can find the source code. We will let the user chose to process either a video |
|
|
|
In the following you can find the source code. We will let the user chose to process either a video |
|
|
|
file or a sequence of images. |
|
|
|
file or a sequence of images. |
|
|
|
|
|
|
|
|
|
|
|
Two different methods are used to generate two foreground masks: |
|
|
|
We will use @ref cv::BackgroundSubtractorMOG2 in this sample, to generate the foreground mask. |
|
|
|
-# cv::bgsegm::BackgroundSubtractorMOG |
|
|
|
|
|
|
|
-# @ref cv::BackgroundSubtractorMOG2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The results as well as the input data are shown on the screen. |
|
|
|
The results as well as the input data are shown on the screen. |
|
|
|
The source file can be downloaded [here ](https://github.com/opencv/opencv/tree/master/samples/cpp/tutorial_code/video/bg_sub.cpp). |
|
|
|
The source file can be downloaded [here ](https://github.com/opencv/opencv/tree/master/samples/cpp/tutorial_code/video/bg_sub.cpp). |
|
|
@ -54,22 +52,19 @@ Explanation |
|
|
|
|
|
|
|
|
|
|
|
We discuss the main parts of the above code: |
|
|
|
We discuss the main parts of the above code: |
|
|
|
|
|
|
|
|
|
|
|
-# First, three Mat objects are allocated to store the current frame and two foreground masks, |
|
|
|
-# First, two Mat objects are allocated to store the current frame and two foreground masks, |
|
|
|
obtained by using two different BS algorithms. |
|
|
|
obtained by using two different BS algorithms. |
|
|
|
@code{.cpp} |
|
|
|
@code{.cpp} |
|
|
|
Mat frame; //current frame |
|
|
|
Mat frame; //current frame |
|
|
|
Mat fgMaskMOG; //fg mask generated by MOG method |
|
|
|
|
|
|
|
Mat fgMaskMOG2; //fg mask fg mask generated by MOG2 method |
|
|
|
Mat fgMaskMOG2; //fg mask fg mask generated by MOG2 method |
|
|
|
@endcode |
|
|
|
@endcode |
|
|
|
-# Two @ref cv::BackgroundSubtractor objects will be used to generate the foreground masks. In this |
|
|
|
-# A @ref cv::BackgroundSubtractor object will be used to generate the foreground mask. In this |
|
|
|
example, default parameters are used, but it is also possible to declare specific parameters in |
|
|
|
example, default parameters are used, but it is also possible to declare specific parameters in |
|
|
|
the create function. |
|
|
|
the create function. |
|
|
|
@code{.cpp} |
|
|
|
@code{.cpp} |
|
|
|
Ptr<BackgroundSubtractor> pMOG; //MOG Background subtractor |
|
|
|
|
|
|
|
Ptr<BackgroundSubtractor> pMOG2; //MOG2 Background subtractor |
|
|
|
Ptr<BackgroundSubtractor> pMOG2; //MOG2 Background subtractor |
|
|
|
... |
|
|
|
... |
|
|
|
//create Background Subtractor objects |
|
|
|
//create Background Subtractor object |
|
|
|
pMOG = createBackgroundSubtractorMOG(); //MOG approach |
|
|
|
|
|
|
|
pMOG2 = createBackgroundSubtractorMOG2(); //MOG2 approach |
|
|
|
pMOG2 = createBackgroundSubtractorMOG2(); //MOG2 approach |
|
|
|
@endcode |
|
|
|
@endcode |
|
|
|
-# The command line arguments are analysed. The user can chose between two options: |
|
|
|
-# The command line arguments are analysed. The user can chose between two options: |
|
|
@ -101,7 +96,6 @@ We discuss the main parts of the above code: |
|
|
|
set a specific learning rate by passing a third parameter to the 'apply' method. |
|
|
|
set a specific learning rate by passing a third parameter to the 'apply' method. |
|
|
|
@code{.cpp} |
|
|
|
@code{.cpp} |
|
|
|
//update the background model |
|
|
|
//update the background model |
|
|
|
pMOG->apply(frame, fgMaskMOG); |
|
|
|
|
|
|
|
pMOG2->apply(frame, fgMaskMOG2); |
|
|
|
pMOG2->apply(frame, fgMaskMOG2); |
|
|
|
@endcode |
|
|
|
@endcode |
|
|
|
-# The current frame number can be extracted from the @ref cv::VideoCapture object and stamped in |
|
|
|
-# The current frame number can be extracted from the @ref cv::VideoCapture object and stamped in |
|
|
@ -121,7 +115,6 @@ We discuss the main parts of the above code: |
|
|
|
@code{.cpp} |
|
|
|
@code{.cpp} |
|
|
|
//show the current frame and the fg masks |
|
|
|
//show the current frame and the fg masks |
|
|
|
imshow("Frame", frame); |
|
|
|
imshow("Frame", frame); |
|
|
|
imshow("FG Mask MOG", fgMaskMOG); |
|
|
|
|
|
|
|
imshow("FG Mask MOG 2", fgMaskMOG2); |
|
|
|
imshow("FG Mask MOG 2", fgMaskMOG2); |
|
|
|
@endcode |
|
|
|
@endcode |
|
|
|
-# The same operations listed above can be performed using a sequence of images as input. The |
|
|
|
-# The same operations listed above can be performed using a sequence of images as input. The |
|
|
|