Nodes/ComfyDL/Box IoU
ComfyUI Node

Box IoU

Pairwise IoU between two sets of boxes, computed in one shot

By Cynthia-lxx·Created 2 months ago·Updated 2 days ago· 6
Box IoU
  • boxes1
  • boxes2
  • iou

CdlBoxIou computes the intersection-over-union between every box in one set and every box in another, and hands you back the full matrix. IoU is the workhorse of object detection: it's how you decide two boxes overlap "enough" to be the same object, how NMS picks which duplicate detections to kill, and how mAP scores are built. This node is that metric as a pure, inspectable operation - the same box_iou from Dive into Deep Learning that ComfyDL is built on.

How it works

You give it two sets of boxes, both in (x1, y1, x2, y2) corner format. For every pair it:

  1. computes each box's area;
  2. finds the intersection rectangle - the upper-left corner is the max of the two top-left corners, the lower-right is the min of the two bottom-rights, clamped at zero so non-overlapping boxes give 0;
  3. divides intersection area by union area (area1 + area2 − intersection).

The result is an [N1, N2] matrix where row i, column j is how much box i from the first set overlaps box j from the second, from 0.0 (no overlap) to 1.0 (identical). It's vectorized - the intersection math is done with broadcasting, so a few dozen boxes each side costs microseconds.

Inputs and output

  • boxes1 - cdlTensor of [N1, 4], corner format.
  • boxes2 - cdlTensor of [N2, 4], corner format.

The output is iou, a cdlTensor of [N1, N2]. There's no threshold here - you get every raw pairwise score and decide where to cut. (The sibling nodes that use a threshold, like CdlAssignAnchorToBbox and CdlNms, run their own internal IoU.)

Where it fits

Three places in a detection workflow: matching predictions to ground truth for evaluation, feeding the anchor-assignment step (which scores anchors against ground-truth boxes), and understanding NMS - which is "sort by confidence, then delete any remaining box whose IoU with the winner exceeds the threshold." If you want to see the metric rather than trust it, wire a small tensor of boxes in and read the matrix - with two boxes each side you can verify the numbers by hand, which is a genuinely good way to build intuition for what 0.5 IoU actually looks like.

Installing it

It's part of ComfyDL, one install for all 106 nodes:

cd ComfyUI/custom_nodes
git clone https://github.com/Cynthia-lxx/ComfyDL
pip install -r ./ComfyDL/requirements.txt

Restart ComfyUI, or search "ComfyDL" in ComfyUI Manager. No extra dependency beyond what ComfyUI already ships.

Gotchas

  • Format matters. This node assumes (x1, y1, x2, y2) corners. Feed it center-width-height boxes and you'll get garbage with no error - the math runs happily on wrong numbers. Convert with CdlBoxCenterToCorner first if your boxes aren't already corners.
  • Inverted boxes (negative width from a flipped x2 < x1) produce nonsensical areas. Same fix as the converters: validate upstream.
  • cdlTensor ports only connect to other ComfyDL nodes - a standard ComfyUI image or bbox type won't plug in here.
Categoryd2l/ObjectDetection

Inputs (2)

NameTypeDefaultDescription
boxes1TENSOR
boxes2TENSOR

Outputs (1)

NameTypeDescription
iouTENSOR