Nodes/ComfyUI CV/cv2.rectangleIntersectionArea
ComfyUI Node

cv2.rectangleIntersectionArea

Two rectangles, one number

By bmad4ever·Created 3 months ago·Updated 14 days ago· 1
cv2.rectangleIntersectionArea
    • float
    ◄a_x0►
    ◄a_y0►
    ◄a_w0►
    ◄a_h0►
    ◄b_x0►
    ◄b_y0►
    ◄b_w0►
    ◄b_h0►

    What it computes

    The overlap area of two axis-aligned integer rectangles. Two boxes in, one FLOAT out. Under the hood it's cv2's own Rect2i intersection helper - max(0, min(a_right, b_right) - max(a_left, b_left)) * max(0, min(a_bottom, b_bottom) - max(a_top, b_top)), which is the formula you'd write yourself, minus the chance of getting a sign wrong.

    That "max(0, …)" is the whole value of the node, by the way. Disjoint rectangles return 0.0, not a negative area - so it's safe to sum, threshold, or use as a comparison key without guarding.

    Inputs

    Eight integer widgets, no sockets: a_x, a_y, a_w, a_h, b_x, b_y, b_w, b_h. All default to 0, all Int. No NPARRAY, no images - this is a calculator, not an image operation.

    Because they're widgets, everything defaults to a degenerate zero-size rect at the origin and a fresh node returns 0.0 until you type real numbers in. If you need the values to come from elsewhere in the graph, right-click each widget and Convert widget to input, then wire a Primitive-style node into it - the standard ComfyUI mechanic for turning a baked-in number into a wire (and from the KB's plumbing notes, the reason value nodes exist at all: one source, fanned out).

    Output: a single float socket. Plain number, no array wrapper.

    Why you'd want it

    The classic use is overlap-based decisions on a small, fixed set of boxes: is this detection inside the crop I already made? Does the tracking window still cover the object? How much of A is also in B? Overlap-normalised-by-area is IoU, and IoU is how non-max suppression decides whether two detections are the same object.

    Which brings up the honest advice: if your boxes are arrays, don't do it here. The pack has a curated CV Box IoU Matrix that takes ComfyUI BOUNDING_BOX batches (the things CV YuNet Face Detect and friends emit) and produces a full pairwise overlap matrix, ready for NMS. Eight typed integers is the wrong shape of tool when you have 200 boxes. This node is for the hand-authored case: two rectangles you already reasoned about, where you want the number in the graph - a threshold, a comparison, a printed diagnostic.

    It's also a nice debugging instrument. Compute the overlap of the region you asked for against the region you got (bbox from CV Masks to BBoxes), and 0.0 tells you the mask detector is looking somewhere you weren't expecting. No shipped example workflow uses this node - grep the pack's workflows/ folder and it's absent - so there's no picture to copy; it's a scratchpad utility, and it wears that on its sleeve.

    Install

    Manager → "ComfyUI CV" (bmad4ever), or the manual route:

    cd ComfyUI/custom_nodes
    git clone https://github.com/bmad4ever/comfyui_cv
    pip install "opencv-contrib-python-headless~=5.0.0.93"
    

    Requires Python ≥ 3.12 and a ComfyUI recent enough for the V3 node API. Restart ComfyUI, then reload the page.

    Troubleshooting

    It always returns 0.0. Either both rects are the defaults (all zeros), or the rects genuinely don't overlap. The node gives you no clue which, because it has no tooltips of its own - this is one of the wrappers where the pack contributes the widget plumbing and nothing else, so x, y, w, h mean exactly what cv2's Rect means: top-left origin, y down, width and height positive.

    Mixed-up width/height. Easy typo with four numbers per box and no labels in your head. If an area looks like the transpose of what you expected, that's it.

    You wanted a ratio, not an area. Two number nodes afterwards - the intersection divided by the union (or by one rect's area, if that's the semantic you actually want) - since this node deliberately returns only the raw intersection.

    A note on the pack's general reliability. Its README is unusually forthright: the code was written with heavy LLM assistance, the author doesn't recommend production use without reviewing it yourself, and updates aren't planned. For a pure arithmetic helper like this that's a mild caveat; treat the number as you would any one-line formula and sanity-check it once against a case you know.

    Categoryimage/CV/low-level/cv2 R

    Inputs (8)

    NameTypeDefaultDescription
    a_xINT0-2147483648–2147483647Rectangle top-left corner X in pixels.
    a_yINT0-2147483648–2147483647Rectangle top-left corner Y in pixels.
    a_wINT00–2147483647Rectangle width in pixels (>= 0).
    a_hINT00–2147483647Rectangle height in pixels (>= 0).
    b_xINT0-2147483648–2147483647Rectangle top-left corner X in pixels.
    b_yINT0-2147483648–2147483647Rectangle top-left corner Y in pixels.
    b_wINT00–2147483647Rectangle width in pixels (>= 0).
    b_hINT00–2147483647Rectangle height in pixels (>= 0).

    Outputs (1)

    NameTypeDescription
    floatFLOAT—