Nodes/comfyui_bmad_nodes/RemapReverseBarrelDistortion
ComfyUI Node Runs on cloud

RemapReverseBarrelDistortion

The proper way to undo a lens bend — root-finding instead of a shortcut formula

By bmad4ever·Created 3 years ago·Updated 9 months ago· 70
RemapReverseBarrelDistortion
    • REMAP
    a0.0000
    b0.0000
    c0.0000
    use_inverse_varianttrue
    d

    When you want to remove lens distortion from a real photo, there are two ways to go about it. RemapBarrelDistortion has a use_inverse_variant toggle that uses a reciprocal formula - quick and good enough for a lot of cases, but mathematically it's a different distortion than the true inverse. This node does it the rigorous way: it takes the same a/b/c/d polynomial model and, for every pixel, actually solves the polynomial backward to find the radius that maps to the target. Where the variant is a shortcut, this is the real undo.

    The cost of that rigor is speed. Per-pixel root-finding on a cubic polynomial is expensive, and the source code shows the author reaching for joblib's parallel execution and numpy polynomial root solvers to make it tolerable. On a large image it's the slowest node in the Remap family, full stop. You use it when the shortcut's approximation is visibly not good enough.

    It's a REMAP generator like all its siblings: RemapReverseBarrelDistortion → Remap → image.

    How it works

    Same lens model as its sibling - radius through the cubic r' = (a·r³ + b·r² + c·r + d) · r - but now you start from the distorted radius and need to find which original radius, when passed through the model, produces it. That means inverting a cubic, which has no clean closed form, so the node approximates the inverse function per pixel and then locates the correct root within a sane range (the code filters roots to real values in a plausible radius band and ignores the rest). The use_inverse_variant toggle behaves a bit differently here - when on, it approximates the inverse of the inverse-variant formula, because that polynomial doesn't have a direct solvable form either.

    The inputs

    • a, b, c (FLOAT, −10 to 10) - the polynomial coefficients describing the distortion you're undoing.
    • d (FLOAT, optional) - derived as 1 - (a+b+c) if not connected.
    • use_inverse_variant (BOOLEAN, default on).

    Output: REMAP.

    When it's the right tool

    Correcting an actual fisheye or wide-angle photo where the barrel is strong enough that the shortcut leaves residual bowing at the edges - that's this node's lane. The workflow mirror of barrel: dial in coefficients that describe the lens's distortion, then the reverse node untwists it. Start small (a around 0.001–0.005), preview through the Remap node, and expect each tweak to take a moment on big images.

    Installing it

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

    restart, or grab comfyui_bmad_nodes in ComfyUI Manager. OpenCV plus the pack's requirements; joblib arrives with scikit-image, so the parallel path comes along. No model downloads.

    The honest bottom line: for mild distortion the sibling's use_inverse_variant is indistinguishable and much faster. This one earns its keep on strong, real-world lens warp where you can see the shortcut's approximation breaking down. If your image is mostly fine and you're just fussing over a subtle bow, don't pay the speed tax.

    CategoryBmad/CV/Transform

    Inputs (5)

    NameTypeDefaultDescription
    aFLOAT0.0000-10–10
    bFLOAT0.0000-10–10
    cFLOAT0.0000-10–10
    use_inverse_variantBOOLEANtrue
    doptFLOAT

    Outputs (1)

    NameTypeDescription
    REMAPREMAP