Nodes/ComfyUI CV/cv2.borderInterpolate
ComfyUI Node

cv2.borderInterpolate

The one-number answer for 'where does OpenCV think that pixel is?'

By bmad4ever·Created 4 months ago·Updated 15 days ago· 1
cv2.borderInterpolate
    • int
    ◄p0►
    ◄len0►
    ◄borderTypeBORDER_DEFAULT►

    This is the pack's most honest node: it's a calculator. cv2.borderInterpolate doesn't touch an image, doesn't produce one, and has no place in a normal workflow. It answers one question - "given an out-of-range coordinate and a border rule, which in-range index would OpenCV sample instead?" - and it answers it as a plain integer.

    It lives in comfyui_cv, which exposes ~470 raw cv2.* functions as ComfyUI nodes. Most of those wrappers are image plumbing in a trench coat, but a few, like this one, are the raw signal-processing primitives OpenCV uses internally, surfaced because the pack's whole premise is "everything cv2 can do, as a socket." Its output is a scalar, so it belongs in the plumbing layer, next to the value and tuple nodes rather than the pixel filters.

    What it's actually for

    When a filter, a warp or a remap reads a pixel outside the image, OpenCV extrapolates: it clamps to the edge (BORDER_REPLICATE), mirrors the strip back (BORDER_REFLECT), mirrors without repeating the edge pixel (BORDER_REFLECT_101, the default), or wraps around. You mostly don't need to know which - until you're writing your own sampling math in a graph (a custom relative remap, a coordinate grid you built from number nodes, a pixel loop), and then the difference between reflect and reflect-101 at the first pixel decides whether your maths matches what the neighbouring node does.

    This node is how you check that without guessing. It's also the least glamorous debugging tool in the pack: set p to -1, sweep the border modes, and watch the returned index change. BORDER_REFLECT_101 sends -1 to 1; BORDER_REPLICATE sends it to 0; BORDER_REFLECT sends it to 0 too - which is exactly why those last two are so easy to confuse, since they differ by one pixel at the edge and nowhere else. That's arithmetic that is genuinely easier to look up than to derive at 1am.

    Inputs and output

    • p (required, default 0) - the 0-based coordinate along one axis. Negative values are fine and are the entire point; the tooltip notes values beyond len are handled too.
    • len (required, default 0) - the length of the array along that axis. Set it to your actual image dimension, or the mapping is fictional.
    • borderType (required, default BORDER_DEFAULT) - the border rule, from the usual BORDER_* dropdown.

    One output: int - the index OpenCV would sample. It's an ordinary INT socket, so it can feed any INT input, be displayed, or be compared with a primitive.

    Both scalars default to zero, which is meaningless (a zero-length axis). Nothing about this node's defaults is a working starting point - you set all three values every time.

    The behaviour that surprises people

    Two of them, both stated in the author's own tooltip. BORDER_CONSTANT always returns -1, regardless of p and len - that's how OpenCV signals "outside the image, use the constant." And the dropdown offers BORDER_TRANSPARENT and BORDER_ISOLATED, but the OpenCV function rejects both, so selecting them gives you a runtime error rather than an answer. The tooltip says "except for #BORDER_TRANSPARENT and #BORDER_ISOLATED"; the combo box just doesn't enforce it.

    Install

    cd ComfyUI/custom_nodes
    git clone https://github.com/bmad4ever/comfyui_cv
    

    Restart, or install via ComfyUI Manager and search "comfyui_cv". Python ≥ 3.12, a recent ComfyUI on the V3 node API, and OpenCV 5 contrib as the only real dependency:

    pip install "opencv-contrib-python-headless~=5.0.0.93"
    

    The pack pins that version because the curated behaviour is validated against it; other versions may behave differently. No models involved.

    Common issues

    The node errors on BORDER_TRANSPARENT / BORDER_ISOLATED. Expected - see above. Those two are in the generic border dropdown because the pack shares one enum across every node that takes a border mode, and this particular function is one of the few that refuses them.

    The number looks wrong. Check len. OpenCV computes the extrapolated index relative to the axis length, so a len that doesn't match your actual array gives you a correct answer to the wrong question.

    You can't find a use for it. Fair. If you're not hand-rolling sampling coordinates, you don't have one - and needing this node is usually the moment you discover that remap has a borderType and you should have just set it there.

    Categoryimage/CV/low-level/cv2 B

    Inputs (3)

    NameTypeDefaultDescription
    pINT0-2147483648–21474836470-based coordinate of the extrapolated pixel along one of the axes, likely \= len
    lenINT0-2147483648–2147483647Length of the array along the corresponding axis.
    borderTypeCOMBOBORDER_DEFAULTBorder type, one of the #BorderTypes, except for #BORDER_TRANSPARENT and #BORDER_ISOLATED. When borderType==#BORDER_CONSTANT, the function always returns -1, regardless of p and len.

    Outputs (1)

    NameTypeDescription
    intINT—