background_subtraction
Separates the foreground from the background with the background subtractors of OpenCV: MOG2 and KNN, and MOG, GMG, CNT, GSOC and LSBP of opencv_contrib.
opencv_apps |
3.0.0 and later |
|---|---|
ROS |
ROS One, jazzy, kilted, lyrical and later |
OpenCV |
the bgsegm module of opencv_contrib for MOG, GMG, CNT, GSOC and LSBP |
Group |
Segmentation, the nodes of which publish the mask or label image, |
Usage
# ROS 2
ros2 launch opencv_apps background_subtraction.launch.xml image:=/camera/image_raw debug_view:=false
# ROS 1
roslaunch opencv_apps background_subtraction.launch image:=/camera/image_raw debug_view:=false
Try it with the demo video
Download the input video of the demo above, publish it as image with image_publisher, and run the node on it, in the terminals:
wget https://ros-perception.github.io/opencv_apps/media/clips/pool.mp4
# terminal 1: publish the video as image in a loop
ros2 run image_publisher image_publisher_node pool.mp4 --ros-args -p publish_rate:=10.0 -r image_raw:=image
# terminal 2: run the node with the parameters of the demo
ros2 launch opencv_apps background_subtraction.launch.xml image:=image method:=0 node_name:=background_subtraction_mog2
# terminal 3: view the result
ros2 run rqt_image_view rqt_image_view /background_subtraction_mog2/image
MOG2; the camera is fixed and the balls move.
In ROS 1:
# terminal 1
rosrun image_publisher image_publisher pool.mp4 _publish_rate:=10 __name:=camera
# terminal 2
roslaunch opencv_apps background_subtraction.launch image:=camera/image_raw method:=0 node_name:=background_subtraction_mog2
See Credits of the demo videos for the license of the video.
Subscribed topics
Topic |
Type |
|---|---|
|
|
Published topics
Topic |
Type |
|---|---|
|
|
|
|
Parameters
See also the common parameters.
Name |
Type |
Default (range) |
Changed at run time |
Description |
|---|---|---|---|---|
|
int |
900 (1 - 10000) |
yes |
CNT: Maximum allowed credit for a pixel in history |
|
int |
15 (1 - 1000) |
yes |
CNT: Number of frames with same pixel color to consider stable |
|
bool |
true |
yes |
CNT: Give a pixel credit for being stable for a long time |
|
double |
0.8 (0 - 1) |
yes |
GMG: Prior probability that any given pixel is a background pixel |
|
double |
0.8 (0 - 1) |
yes |
GMG: Threshold of the probability above which a pixel is foreground |
|
int |
120 (1 - 1000) |
yes |
GMG: Number of frames used to initialize the background models |
|
int |
64 (1 - 512) |
yes |
GMG: Total number of distinct colors to maintain in histogram (recreates the model) |
|
int |
16 (1 - 128) |
yes |
GMG: Number of discrete levels in each channel to be used in histograms (recreates the model) |
|
int |
7 (0 - 31) |
yes |
GMG: Kernel size of the median filter to clean up the foreground mask, 0 (no filter) or odd |
|
int |
32 (1 - 1000) |
yes |
GSOC: How many positives the sample must get before it will be considered as a possible replacement (recreates the model) |
|
int |
20 (2 - 100) |
yes |
GSOC: Number of samples to maintain at each point of the frame (recreates the model) |
|
double |
0.01 (0 - 1) |
yes |
GSOC: Probability of propagating to neighbors (recreates the model) |
|
double |
0.003 (0 - 1) |
yes |
GSOC: Probability of replacing the old sample (recreates the model) |
|
bool |
true |
yes |
KNN: Detect shadows and mark them (127) in the foreground mask |
|
double |
400.0 (1 - 5000) |
yes |
KNN: Threshold on the squared distance between the pixel and the sample to decide whether a pixel is close to that sample |
|
int |
500 (1 - 5000) |
yes |
KNN: Length of the history |
|
double |
-1.0 (-1 - 1) |
yes |
Learning rate of the background model, negative: automatically chosen, 0: not updated, 1: reinitialized from the last frame (CNT, GSOC and LSBP may still update the model at 0, and GMG, GSOC and LSBP do not reinitialize it at 1) |
|
int |
2 (1 - 100) |
yes |
LSBP: Minimal number of matches for a pixel to be background (recreates the model) |
|
int |
20 (2 - 100) |
yes |
LSBP: Number of samples to maintain at each point of the frame (recreates the model) |
|
int |
16 (1 - 64) |
yes |
LSBP: Radius of the LSBP descriptor (recreates the model) |
|
int |
8 (0 - 32) |
yes |
LSBP: Threshold of the LSBP descriptor distance (recreates the model) |
|
int |
0 (0 - 6) |
yes |
Background subtraction method, 0: MOG2, 1: KNN, 2: MOG, 3: GMG, 4: CNT, 5: GSOC, 6: LSBP (MOG, GMG, CNT, GSOC and LSBP need the bgsegm module of opencv_contrib, otherwise MOG2 is used) |
|
bool |
true |
yes |
MOG2: Detect shadows and mark them (127) in the foreground mask |
|
int |
500 (1 - 5000) |
yes |
MOG2: Length of the history |
|
int |
5 (1 - 10) |
yes |
MOG2: Number of Gaussian components in the background model (recreates the model) |
|
double |
16.0 (1 - 256) |
yes |
MOG2: Threshold on the squared Mahalanobis distance between the pixel and the model to decide whether a pixel is well described by the background model |
|
double |
0.7 (0 - 1) |
yes |
MOG: Background ratio |
|
int |
200 (1 - 5000) |
yes |
MOG: Length of the history |
|
int |
5 (1 - 8) |
yes |
MOG: Number of Gaussian mixtures, at most 8 by OpenCV (recreates the model) |
|
double |
0.0 (0 - 100) |
yes |
MOG: Noise strength, 0: automatic |
|
int |
3 |
no, restart the node |
Specify queue_size of input image subscribers |
Source
src/nodelet/background_subtraction_nodelet.cpp, launch/background_subtraction.launch.xml