Nearest Neighbor Detection Match¶
Class: DetectionsNearestNeighborBlockV1
Perform a nearest-neighbor spatial join between two detection sets by finding, for each detection in a query set, the closest detection (or detections, on a tie) in a target set, using plain 2D Euclidean pixel distance between configurable anchor points.
How This Block Works¶
This block matches each detection in a query set to its nearest neighbor(s) in a target set. The block:
- Receives query predictions and target predictions (object detection, instance segmentation, or keypoint detection) - the same set can be passed as both query and target
- Resolves an anchor point for every detection in each set, based on the selected
query_pointandtarget_pointoptions: the center, one of the four corners, one of the four edge midpoints of the bounding box, or a named keypoint (requires keypoint detection predictions andquery_keypoint_name/target_keypoint_name) - Computes a full pairwise pixel-distance matrix between every query anchor point and every target anchor point (brute-force, not a spatial tree - appropriate for the tens-of-detections-per-frame scale this block targets)
- For each query detection, excludes any target detection that shares its
detection_id(self-match exclusion), so passing the same detections as both query and target does not match a detection against itself - If
max_distanceis set, discards any remaining candidate target farther thanmax_distancepixels from the query anchor point before ranking - a target beyond the limit is treated the same as an excluded self-match - Finds the minimum distance for each query detection, then includes every remaining target detection whose distance is within a fixed 1-pixel epsilon of that minimum, so exact or near-exact ties produce multiple matches instead of an arbitrary single winner
- Attaches the nearest distance (in pixels) to each query detection's metadata; detections with no eligible target (empty target set, every target excluded as a self-match, or every remaining target farther than
max_distance) getNone - Returns three flat, aligned outputs: the enriched query predictions, and two same-length/same-order detection sets -
matched_query_detectionsandmatched_target_detections- with one row per query-target pairing. A query detection with a tie appears once per tied match (its row is duplicated); a query detection with no valid match is omitted from both, and only shows up inquery_predictions(withnearest_target_distanceset toNone)
Distance is always plain, uncalibrated 2D Euclidean pixel distance - there is no real-world unit conversion, unlike the Distance Measurement block. This keeps the block simple for use cases that only need relative/pixel-space proximity.
Common Use Cases¶
- Zone/Anchor Assignment: Assign each detected object to its nearest reference point or anchor detection (e.g. match people to the nearest exit sign, match vehicles to the nearest parking spot marker), enabling proximity-based assignment workflows
- Cross-Class Association: Associate detections of one class with the nearest detection of another class (e.g. match detected hands to the nearest detected tool, match players to the nearest ball detection), enabling relationship analysis between different object types
- Duplicate/Overlap Analysis: Compare a detection set against itself to find each detection's closest neighbor (e.g. find the closest other person in a crowd, identify tightly clustered detections), enabling density and clustering analysis
- Tracking Handoff: Match detections between two model outputs on the same frame (e.g. match a fast general detector's output to a slower specialist model's output) to combine results, enabling multi-model fusion workflows
Connecting to Other Blocks¶
This block receives two detection sets and produces the enriched query predictions plus two flat, aligned detection sets representing every query-target pairing - no dimensionality tricks or intermediate merge step required, since matched_query_detections and matched_target_detections are ordinary detection sets of the same shape any detection-producing block returns:
- After object detection, instance segmentation, or keypoint detection model blocks to build the query and target sets to match, enabling detection-to-matching workflows
- Before logic blocks like Continue If or Detections Filter to make decisions based on
nearest_target_distance(e.g. continue if the nearest match is within a threshold distance) or to filtermatched_query_detections/matched_target_detectionsdirectly, enabling proximity-based decision workflows - Before visualization blocks like Label Visualization or Bounding Box Visualization to display the enriched
query_predictions, ormatched_query_detections/matched_target_detectionsside by side to draw matched pairs, enabling distance-annotated visualizations - Before data storage blocks like CSV Formatter to export every query-target pairing as a row, enabling matched-pair logging workflows
Requirements¶
This block requires two sets of detection predictions (object detection, instance segmentation, or keypoint detection); the same set can be used for both query_predictions and target_predictions. To use the KEYPOINT anchor option for either set, that set must be keypoint detection predictions and the corresponding query_keypoint_name/target_keypoint_name must be provided. Self-match exclusion relies on detection_id being present on both sets - this is populated automatically for all Roboflow object detection, instance segmentation, and keypoint detection model blocks. max_distance is optional; leave it unset to match every query detection to its nearest target regardless of distance.
Note that query_predictions is enriched in place - the same sv.Detections object passed in is mutated (a new nearest_target_distance field is added to its .data) and returned, the same convention used by blocks like Velocity and Time in Zone. Avoid feeding the same selector into two independent branches of a workflow if each branch needs to see its own, unmodified nearest_target_distance.
Type identifier¶
Use the following identifier in step "type" field: roboflow_core/detections_nearest_neighbor@v1to add the block as
as step in your workflow.
Properties¶
| Name | Type | Description | Refs |
|---|---|---|---|
name |
str |
Enter a unique identifier for this step.. | ❌ |
query_point |
str |
Anchor point used to represent each query detection's location: CENTER, a corner (TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT), an edge midpoint (TOP_CENTER, BOTTOM_CENTER, CENTER_LEFT, CENTER_RIGHT), or KEYPOINT (requires query_predictions to be keypoint detection predictions and query_keypoint_name to be set).. |
✅ |
target_point |
str |
Anchor point used to represent each target detection's location. Same options as query_point, set independently. Use KEYPOINT to match against a named keypoint on target_predictions (requires target_predictions to be keypoint detection predictions and target_keypoint_name to be set).. |
✅ |
query_keypoint_name |
str |
Name of the keypoint class to use as the query anchor point. Required only when query_point is set to KEYPOINT; query_predictions must then be keypoint detection predictions.. |
✅ |
target_keypoint_name |
str |
Name of the keypoint class to use as the target anchor point. Required only when target_point is set to KEYPOINT; target_predictions must then be keypoint detection predictions.. |
✅ |
max_distance |
int |
Maximum allowed pixel distance between a query detection and a candidate target. Targets farther than this are excluded before ranking, the same way self-matches are excluded. A query detection whose nearest remaining target is still farther than this (or has no remaining target at all) gets nearest_target_distance = None and is omitted from matched_query_detections/matched_target_detections. Leave unset for no limit.. |
✅ |
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 Nearest Neighbor Detection Match in version v1.
- inputs:
Detection Event Log,Roboflow Dataset Upload,ByteTrack Tracker,Instance Segmentation Model,Google Gemini,Mask Edge Snap,Dynamic Crop,Per-Class Confidence Filter,Detections Transformation,Time in Zone,SAM 3 Interactive,Velocity,Detections Classes Replacement,Template Matching,Current Time,Pixel Color Count,Roboflow Custom Metadata,Multi-Label Classification Model,SIFT Comparison,Mask Area Measurement,BoT-SORT Tracker,OpenAI,MoonshotAI Kimi,SAM 3,Segment Anything 2 Model,Google Gemma,Byte Tracker,Object Detection Model,Motion Detection,Cosmos 3,Overlap Filter,OpenAI,Webhook Sink,VLM As Detector,Path Deviation,OPC UA Writer Sink,Detections Stabilizer,Bounding Rectangle,SAM 3,Line Counter,Roboflow Dataset Upload,OpenAI,SAM2 Video Tracker,Google Vision OCR,OpenAI,Florence-2 Model,Detection Offset,SAM3 Video Tracker,VLM As Classifier,Detections List Roll-Up,SORT Tracker,Qwen 3.6 API,PLC Writer,CSV Formatter,Detections Stitch,Track Class Lock,Byte Tracker,Anthropic Claude,PP-OCR,Anthropic Claude,Image Stack,Roboflow Visual Search Classifier,VLM As Detector,OCR Model,SAM 3,PTZ Tracking (ONVIF),Qwen 3.5 API,OpenRouter,Frame Delay,Local File Sink,Roboflow Asset Library Attributes,LMM,Gaze Detection,S3 Sink,MQTT Writer,Qwen-VL,Instance Segmentation Model,Google Gemini,Stitch OCR Detections,Slack Notification,Event Writer,Detections Consensus,Nearest Neighbor Detection Match,Stitch OCR Detections,Google Gemini,Time in Zone,Instance Segmentation Model,Twilio SMS Notification,Single-Label Classification Model,Perspective Correction,Seg Preview,EasyOCR,Google Gemma API,Object Detection Model,Instance Segmentation Model,OC-SORT Tracker,MoonshotAI Kimi,Distance Measurement,Twilio SMS/MMS Notification,Path Deviation,Model Monitoring Inference Aggregator,Microsoft SQL Server Sink,Florence-2 Model,Qwen3.5-VL,Moondream2,Clip Comparison,Dynamic Zone,Email Notification,Roboflow Visual Search,Detections Filter,SIFT Comparison,Keypoint Detection Model,Llama 3.2 Vision,Detections Combine,GLM-OCR,CogVLM,Byte Tracker,Image Contours,Line Counter,Roboflow Vision Events,YOLO-World Model,Llama 3.2 Vision,OpenAI-Compatible LLM,Time in Zone,Keypoint Detection Model,Keypoint Detection Model,Detections Merge,LMM For Classification,Object Detection Model,Email Notification,Anthropic Claude - outputs:
Crop Visualization,Detection Event Log,Roboflow Dataset Upload,ByteTrack Tracker,Mask Edge Snap,Dynamic Crop,SAM 3 Interactive,Detections Transformation,Time in Zone,Per-Class Confidence Filter,Velocity,Detections Classes Replacement,Overlap Analysis,Roboflow Custom Metadata,Mask Area Measurement,BoT-SORT Tracker,Ellipse Visualization,Segment Anything 2 Model,Bounding Box Visualization,Byte Tracker,Overlap Filter,Path Deviation,Size Measurement,Detections Stabilizer,Bounding Rectangle,Roboflow Dataset Upload,SAM2 Video Tracker,Florence-2 Model,Detection Offset,Detections List Roll-Up,Pixelate Visualization,SORT Tracker,Detections Stitch,Track Class Lock,Triangle Visualization,Icon Visualization,Dot Visualization,Byte Tracker,Keypoint Visualization,GeoTag Detection,PTZ Tracking (ONVIF),Halo Visualization,Frame Delay,Color Visualization,Corner Visualization,Stitch OCR Detections,Event Writer,Detections Consensus,Nearest Neighbor Detection Match,Stitch OCR Detections,Time in Zone,Trace Visualization,Perspective Correction,Camera Focus,OC-SORT Tracker,Distance Measurement,Path Deviation,Blur Visualization,Polygon Visualization,Model Monitoring Inference Aggregator,Stability AI Inpainting,Florence-2 Model,Dynamic Zone,Mask Visualization,Polygon Visualization,Detections Filter,Halo Visualization,Detections Combine,Byte Tracker,Circle Visualization,Heatmap Visualization,Roboflow Vision Events,Line Counter,Label Visualization,Time in Zone,Background Color Visualization,Model Comparison Visualization,Detections Merge,Line Counter
Input and Output Bindings¶
The available connections depend on its binding kinds. Check what binding kinds
Nearest Neighbor Detection Match in version v1 has.
Bindings
-
input
query_predictions(Union[instance_segmentation_prediction,object_detection_prediction,keypoint_detection_prediction]): Detections to find nearest-neighbor matches for. Each detection in this set is matched againsttarget_predictions. The same selector can be used for bothquery_predictionsandtarget_predictions(e.g. to find each detection's closest neighbor within one set) - a detection is never matched against itself..target_predictions(Union[instance_segmentation_prediction,object_detection_prediction,keypoint_detection_prediction]): Detections to search for the nearest match within, for every detection inquery_predictions. Can be the same selector asquery_predictions..query_point(string): Anchor point used to represent each query detection's location: CENTER, a corner (TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT), an edge midpoint (TOP_CENTER, BOTTOM_CENTER, CENTER_LEFT, CENTER_RIGHT), or KEYPOINT (requiresquery_predictionsto be keypoint detection predictions andquery_keypoint_nameto be set)..target_point(string): Anchor point used to represent each target detection's location. Same options asquery_point, set independently. Use KEYPOINT to match against a named keypoint ontarget_predictions(requirestarget_predictionsto be keypoint detection predictions andtarget_keypoint_nameto be set)..query_keypoint_name(string): Name of the keypoint class to use as the query anchor point. Required only whenquery_pointis set to KEYPOINT;query_predictionsmust then be keypoint detection predictions..target_keypoint_name(string): Name of the keypoint class to use as the target anchor point. Required only whentarget_pointis set to KEYPOINT;target_predictionsmust then be keypoint detection predictions..max_distance(integer): Maximum allowed pixel distance between a query detection and a candidate target. Targets farther than this are excluded before ranking, the same way self-matches are excluded. A query detection whose nearest remaining target is still farther than this (or has no remaining target at all) getsnearest_target_distance = Noneand is omitted frommatched_query_detections/matched_target_detections. Leave unset for no limit..
-
output
query_predictions(Union[object_detection_prediction,instance_segmentation_prediction,keypoint_detection_prediction]): Prediction with detected bounding boxes in form of sv.Detections(...) object ifobject_detection_predictionor Prediction with detected bounding boxes and segmentation masks in form of sv.Detections(...) object ifinstance_segmentation_predictionor Prediction with detected bounding boxes and detected keypoints in form of sv.Detections(...) object ifkeypoint_detection_prediction.matched_query_detections(Union[object_detection_prediction,instance_segmentation_prediction,keypoint_detection_prediction]): Prediction with detected bounding boxes in form of sv.Detections(...) object ifobject_detection_predictionor Prediction with detected bounding boxes and segmentation masks in form of sv.Detections(...) object ifinstance_segmentation_predictionor Prediction with detected bounding boxes and detected keypoints in form of sv.Detections(...) object ifkeypoint_detection_prediction.matched_target_detections(Union[object_detection_prediction,instance_segmentation_prediction,keypoint_detection_prediction]): Prediction with detected bounding boxes in form of sv.Detections(...) object ifobject_detection_predictionor Prediction with detected bounding boxes and segmentation masks in form of sv.Detections(...) object ifinstance_segmentation_predictionor Prediction with detected bounding boxes and detected keypoints in form of sv.Detections(...) object ifkeypoint_detection_prediction.
Example JSON definition of step Nearest Neighbor Detection Match in version v1
{
"name": "<your_step_name_here>",
"type": "roboflow_core/detections_nearest_neighbor@v1",
"query_predictions": "$steps.model.predictions",
"target_predictions": "$steps.model.predictions",
"query_point": "CENTER",
"target_point": "CENTER",
"query_keypoint_name": "left_shoulder",
"target_keypoint_name": "left_shoulder",
"max_distance": 50
}