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.
Runtime compatibility¶
-
soft— runtimehosted_serverless,dedicated_deployment; executionremote; inputvideo - Block keeps per-video state in process memory (keyed by video_metadata.video_identifier). With remote step execution on stateless or multi-replica HTTP runtimes, successive requests may be served by different worker processes, so the state resets between calls and the output is meaningless for tracking / counting / aggregation. Use local step execution in an InferencePipeline for stable cross-frame results.
-
soft— inputimage - Block depends on temporal context from video or repeated-frame workflows. With a still image/photo, there is no meaningful history to track, compare, aggregate, or visualize, so the block provides little or no benefit.
Available Connections¶
Compatible Blocks
Check what blocks you can connect to Background Subtraction in version v1.
- inputs:
Heatmap Visualization,Image Slicer,Polygon Zone Visualization,Line Counter,Contrast Enhancement,Stability AI Image Generation,Image Threshold,Line Counter Visualization,Trace Visualization,Distance Measurement,Image Stack,Blur Visualization,Camera Calibration,Depth Estimation,QR Code Generator,Stability AI Outpainting,Detection Event Log,Icon Visualization,SIFT Comparison,Morphological Transformation,Background Subtraction,Keypoint Visualization,Color Visualization,Perspective Correction,Bounding Box Visualization,Corner Visualization,Stitch Images,Halo Visualization,Image Blur,Image Convert Grayscale,Morphological Transformation,Camera Focus,Contrast Equalization,Halo Visualization,Stability AI Inpainting,Line Counter,Roboflow Visual Search,Classification Label Visualization,Triangle Visualization,Grid Visualization,Background Color Visualization,Mask Visualization,Ellipse Visualization,Pixelate Visualization,Reference Path Visualization,SIFT,Label Visualization,Image Slicer,Text Display,SIFT Comparison,Dot Visualization,Polygon Visualization,Crop Visualization,Dynamic Crop,Absolute Static Crop,Circle Visualization,Image Contours,Image Preprocessing,Polygon Visualization,Relative Static Crop,Camera Focus,Template Matching,Pixel Color Count,Model Comparison Visualization - outputs:
VLM As Classifier,MoonshotAI Kimi,Stability AI Image Generation,Trace Visualization,Qwen2.5-VL,Image Stack,Anthropic Claude,Icon Visualization,SIFT Comparison,Morphological Transformation,Color Visualization,SmolVLM2,LMM For Classification,Single-Label Classification Model,Perspective Correction,Corner Visualization,Clip Comparison,Halo Visualization,Qwen-VL,Keypoint Detection Model,Halo Visualization,Object Detection Model,Google Gemma,Background Color Visualization,Ellipse Visualization,Email Notification,Twilio SMS/MMS Notification,Text Display,Polygon Visualization,Crop Visualization,Absolute Static Crop,Image Preprocessing,Template Matching,Relative Static Crop,OpenRouter,OpenAI,Florence-2 Model,VLM As Detector,OpenAI,Motion Detection,Heatmap Visualization,OCR Model,Perception Encoder Embedding Model,Blur Visualization,Barcode Detection,Depth Estimation,Instance Segmentation Model,Stability AI Outpainting,Anthropic Claude,YOLO-World Model,Google Gemini,Clip Comparison,Google Gemini,Background Subtraction,Keypoint Visualization,Buffer,Stitch Images,Florence-2 Model,Contrast Equalization,Mask Edge Snap,OpenAI,Qwen3-VL,Moondream2,VLM As Detector,Google Gemini,Triangle Visualization,CLIP Embedding Model,Detections Stabilizer,SIFT,Multi-Label Classification Model,Image Contours,Keypoint Detection Model,VLM As Classifier,Pixel Color Count,GLM-OCR,Image Slicer,Polygon Zone Visualization,Contrast Enhancement,Google Gemma API,Time in Zone,Semantic Segmentation Model,Image Threshold,Line Counter Visualization,Semantic Segmentation Model,Multi-Label Classification Model,Camera Calibration,ByteTrack Tracker,Google Vision OCR,Image Blur,Morphological Transformation,Camera Focus,Roboflow Vision Events,Stability AI Inpainting,Classification Label Visualization,SAM2 Video Tracker,Event Writer,Qwen3.5-VL,Mask Visualization,Llama 3.2 Vision,Dominant Color,Reference Path Visualization,Image Slicer,Label Visualization,Byte Tracker,Dot Visualization,Dynamic Crop,Detections Stitch,Circle Visualization,Llama 3.2 Vision,BoT-SORT Tracker,SAM3 Video Tracker,Camera Focus,Gaze Detection,Segment Anything 2 Model,MoonshotAI Kimi,Single-Label Classification Model,QR Code Detection,Qwen3.5,CogVLM,Object Detection Model,SAM 3 Interactive,Qwen 3.6 API,Bounding Box Visualization,Multi-Label Classification Model,LMM,OpenAI,SAM 3,Image Convert Grayscale,Instance Segmentation Model,EasyOCR,Roboflow Visual Search,Roboflow Dataset Upload,SAM 3,Instance Segmentation Model,Keypoint Detection Model,Pixelate Visualization,Roboflow Dataset Upload,SORT Tracker,Instance Segmentation Model,Track Class Lock,Qwen 3.5 API,Object Detection Model,Anthropic Claude,Polygon Visualization,OC-SORT Tracker,SAM 3,Model Comparison Visualization,Single-Label Classification Model,Seg Preview
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
}