Nodes/opencv-comfyui/OpenCV rectangleIntersectionArea_0
ComfyUI Node

OpenCV rectangleIntersectionArea_0

How much do two boxes overlap? rectangleIntersectionArea_0

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV rectangleIntersectionArea_0
    • float
    a
    b

    rectangleIntersectionArea_0 doesn't touch a single pixel. It takes two rectangles and returns the area of their overlap as a plain float. Boring on the surface, genuinely useful underneath: this is the arithmetic that powers "did my detection box actually land on the region I care about?"

    You feed it two Rect2d literals, a and b, and get a float out. That's the entire input/output story - two STRING widgets in, one FLOAT out. Because it's Rect2d (double-precision), the values can be fractional; (0, 0, 100, 100) and (50, 50, 100, 100) return 2500.0, the classic 50×50 overlap.

    It comes from opencv-comfyui (geroldmeisinger/opencv-comfyui), the auto-generated pack that wraps OpenCV's standalone functions. This one wraps cv2.rectangleIntersectionArea, a newer OpenCV helper that computes the intersection of two axis-aligned rectangles in constant time. No image, no NPARRAY, no Image2Nparray ceremony - the nodes in this pack aren't all image-to-image, and this is the proof.

    Where you'd actually use it

    The obvious one is overlap gating in a pipeline. Say you have a face-detection box and a "safe area" you defined for your workflow: wire both through as literals, compare the intersection against a threshold in a comparison node, and only run the expensive detailer pass when there's meaningful overlap. That's a real pattern in this ecosystem - detection feeding regional generation, as covered in the KB's masking/detection/detailing material - and this node is the cheap gate.

    Worth knowing: it returns intersection area, not IoU. IoU needs the union (area_a + area_b − overlap), so if you want a proper overlap ratio you'll add a math node after this one. The author also flags that auto-generated OpenCV nodes don't always map cleanly onto ComfyUI, and this function is one of the newer, less widely known ones - if your OpenCV version is older and you hit an AttributeError mentioning rectangleIntersectionArea, upgrading opencv-contrib-python fixes it.

    Installing it

    ComfyUI Manager (search "opencv-comfyui") or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/geroldmeisinger/opencv-comfyui
    

    restart, and make sure OpenCV is there:

    pip install opencv-contrib-python
    

    The one gotcha

    Same as every literal-based node in this pack: the rect strings must parse as Python literals. (0, 0, 100, 100) works; 0, 0, 100, 100 throws invalid syntax (<unknown>, line 0). Keep the parens, remember it's (x, y, width, height), and you're done. It's a small node with a small job - but overlap math is the kind of thing you end up hand-rolling in a dozen workflows until you remember a node already does it.

    Categoryimage/OpenCV

    Inputs (2)

    NameTypeDefaultDescription
    aSTRING
    bSTRING

    Outputs (1)

    NameTypeDescription
    floatFLOAT