Background Subtraction¶
Class: BackgroundSubtractionBlockV1
Create motion masks from video streams using OpenCV's background subtraction algorithm.
How This Block Works¶
This block uses background subtraction (specifically the MOG2 algorithm) to identify pixels that differ from a learned background model and outputs a mask image highlighting motion areas. The block maintains state across frames to build and update the background model:
- Initializes background model - on the first frame, creates a background subtractor using the specified history and threshold parameters
- Processes each frame - applies background subtraction to identify pixels that differ from the learned background model
- Creates motion mask - generates a foreground mask where white pixels represent motion areas and black pixels represent the background
- Converts to image format - converts the single-channel mask to a 3-channel image format required by workflows
- Returns mask image - outputs the motion mask as an image that can be visualized or processed further
The output mask image shows motion areas as white pixels against a black background, making it easy to visualize where motion occurred in the frame. This mask can be used for further analysis, visualization, or as input to other processing steps.
Common Use Cases¶
- Motion Visualization: Create visual motion masks to see where movement occurs in video streams for monitoring, analysis, or debugging purposes
- Preprocessing for Motion Models: Generate motion masks as input data for training or inference with motion-based models that require mask data
- Motion Area Extraction: Extract regions of motion from video frames for further processing, analysis, or feature extraction
- Video Analysis: Analyze motion patterns by processing mask images to identify movement trends, activity levels, or motion characteristics
- Background Removal: Use motion masks to separate foreground (moving) objects from static background for segmentation or isolation tasks
- Motion-based Filtering: Use motion masks to filter or focus processing on areas where motion occurs, ignoring static background regions
Connecting to Other Blocks¶
The motion mask image from this block can be connected to:
- Visualization blocks to display the motion mask overlayed on original images or as standalone visualizations
- Object detection blocks to run detection models only on motion regions identified by the mask
- Image processing blocks to apply additional transformations, filters, or analysis to motion mask images
- Data storage blocks (e.g., Local File Sink, Roboflow Dataset Upload) to save motion masks for training data, analysis, or documentation
- Conditional logic blocks to route workflow execution based on the presence or absence of motion in mask images
- Model training blocks to use motion masks as training data for motion-based models or segmentation tasks
Type identifier¶
Use the following identifier in step "type" field: roboflow_core/background_subtraction@v1to add the block as
as step in your workflow.
Properties¶
| Name | Type | Description | Refs |
|---|---|---|---|
name |
str |
Enter a unique identifier for this step.. | ❌ |
threshold |
int |
Threshold value for the squared Mahalanobis distance used by the MOG2 background subtraction algorithm. Controls sensitivity to motion - smaller values increase sensitivity (detect smaller changes) but may produce more false positives, larger values decrease sensitivity (only detect significant changes) but may miss subtle motion. Recommended range is 8-32. Default is 16.. | ✅ |
history |
int |
Number of previous frames used to build the background model. Controls how quickly the background adapts to changes - larger values (e.g., 50-100) create a more stable background model that's less sensitive to temporary changes but adapts slowly to permanent background changes. Smaller values (e.g., 10-20) allow faster adaptation but may treat moving objects as background if they stop moving. Default is 30 frames.. | ✅ |
The Refs column marks possibility to parametrise the property with dynamic values available
in workflow runtime. See Bindings for more info.
Available Connections¶
Compatible Blocks
Check what blocks you can connect to Background Subtraction in version v1.
- inputs:
Camera Focus,Label Visualization,Image Threshold,Stitch Images,Grid Visualization,Background Subtraction,Camera Focus,Polygon Zone Visualization,Color Visualization,Mask Visualization,Circle Visualization,Heatmap Visualization,SIFT,Template Matching,Crop Visualization,Line Counter,Image Slicer,Stability AI Outpainting,QR Code Generator,Bounding Box Visualization,Polygon Visualization,Text Display,Pixelate Visualization,Line Counter,Pixel Color Count,Image Blur,Relative Static Crop,SIFT Comparison,Stability AI Image Generation,Morphological Transformation,Dynamic Crop,Stability AI Inpainting,Background Color Visualization,Image Slicer,Detection Event Log,SIFT Comparison,Line Counter Visualization,Icon Visualization,Image Preprocessing,Distance Measurement,Blur Visualization,Dot Visualization,Ellipse Visualization,Halo Visualization,Polygon Visualization,Triangle Visualization,Model Comparison Visualization,Trace Visualization,Corner Visualization,Image Convert Grayscale,Reference Path Visualization,Absolute Static Crop,Image Contours,Classification Label Visualization,Depth Estimation,Halo Visualization,Camera Calibration,Contrast Equalization,Perspective Correction,Keypoint Visualization - outputs:
Moondream2,Image Threshold,Stitch Images,OpenAI,Byte Tracker,Multi-Label Classification Model,Keypoint Detection Model,Mask Visualization,Time in Zone,Instance Segmentation Model,Circle Visualization,EasyOCR,Seg Preview,Crop Visualization,Multi-Label Classification Model,SAM 3,Stability AI Outpainting,Text Display,Anthropic Claude,Detections Stabilizer,QR Code Detection,Relative Static Crop,Clip Comparison,OpenAI,Segment Anything 2 Model,Stability AI Image Generation,Halo Visualization,Google Gemini,Image Slicer,Keypoint Detection Model,VLM As Detector,VLM As Classifier,Google Gemini,Qwen3.5-VL,Object Detection Model,Ellipse Visualization,Dot Visualization,Halo Visualization,Anthropic Claude,Model Comparison Visualization,OpenAI,Corner Visualization,Motion Detection,Buffer,Absolute Static Crop,Image Contours,Classification Label Visualization,Instance Segmentation Model,Roboflow Dataset Upload,Depth Estimation,Dominant Color,Contrast Equalization,Image Slicer,Detections Stitch,Camera Focus,Label Visualization,Barcode Detection,Llama 3.2 Vision,Background Subtraction,Polygon Zone Visualization,Camera Focus,Color Visualization,Qwen2.5-VL,SAM 3,Heatmap Visualization,OpenAI,SIFT,CogVLM,Template Matching,Florence-2 Model,Roboflow Dataset Upload,Bounding Box Visualization,Polygon Visualization,Pixelate Visualization,Pixel Color Count,Image Blur,SIFT Comparison,Morphological Transformation,Dynamic Crop,Stability AI Inpainting,Background Color Visualization,Perception Encoder Embedding Model,LMM,Object Detection Model,Clip Comparison,Line Counter Visualization,Image Preprocessing,Icon Visualization,VLM As Classifier,Qwen3-VL,SmolVLM2,Blur Visualization,Triangle Visualization,Google Vision OCR,Polygon Visualization,Gaze Detection,OCR Model,Google Gemini,Trace Visualization,Email Notification,Anthropic Claude,Twilio SMS/MMS Notification,CLIP Embedding Model,SAM 3,Single-Label Classification Model,Image Convert Grayscale,Reference Path Visualization,YOLO-World Model,Single-Label Classification Model,LMM For Classification,Camera Calibration,Florence-2 Model,VLM As Detector,Perspective Correction,Keypoint Visualization
Input and Output Bindings¶
The available connections depend on its binding kinds. Check what binding kinds
Background Subtraction in version v1 has.
Bindings
-
input
image(image): The input image or video frame to process for background subtraction. The block processes frames sequentially to build a background model - each frame updates the background model and creates a motion mask showing areas that differ from the learned background. Can be connected from workflow inputs or previous steps..threshold(integer): Threshold value for the squared Mahalanobis distance used by the MOG2 background subtraction algorithm. Controls sensitivity to motion - smaller values increase sensitivity (detect smaller changes) but may produce more false positives, larger values decrease sensitivity (only detect significant changes) but may miss subtle motion. Recommended range is 8-32. Default is 16..history(integer): Number of previous frames used to build the background model. Controls how quickly the background adapts to changes - larger values (e.g., 50-100) create a more stable background model that's less sensitive to temporary changes but adapts slowly to permanent background changes. Smaller values (e.g., 10-20) allow faster adaptation but may treat moving objects as background if they stop moving. Default is 30 frames..
-
output
image(image): Image in workflows.
Example JSON definition of step Background Subtraction in version v1
{
"name": "<your_step_name_here>",
"type": "roboflow_core/background_subtraction@v1",
"image": "$inputs.image",
"threshold": 16,
"history": 30
}