Nodes/opencv-comfyui/OpenCV distanceTransform_0
ComfyUI Node

OpenCV distanceTransform_0

Turn a hard mask into a soft one with OpenCV's distance transform

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV distanceTransform_0
  • src
  • dst
  • nparray
distanceType
maskSize
dstType

distanceTransform_0 is the node that turns a hard binary mask into a soft, feathered one - and it's one of the genuinely useful things in this pack. Feed it a mask where every foreground pixel is 1 and background is 0, and it computes, for each foreground pixel, how far it is to the nearest background pixel. The result is a grayscale "heatmap" of distances: white at the mask's core, fading smoothly to black at its edges. That's a soft mask, and soft masks are the difference between a seamless inpaint and one with a visible cut line.

Why you'd reach for it, in the wider workflow: the mask-and-detailing world runs on hard masks from segmentation, but compositing and inpainting want gradients. When you paste an upscaled face back onto an image, or re-render a masked region, a binary edge is what creates those telltale seams - the KB's detailing doc calls the paste step the seam factory. The distance transform gives you feathering for free: take a mask, run it through here, and the output is ready to drive soft edges, alpha blending, or a falloff for the inpaint region. It's the deterministic, millisecond version of a job people otherwise solve by hand-waving a blur.

The mechanism is the classic two-pass chamfer algorithm - it walks the image from both corners propagating distance estimates until they converge on the true nearest-neighbor distance. You don't need to care about that beyond knowing it's fast and exact-ish.

The inputs that matter:

  • src - NPARRAY, and here's the #1 failure point: it must be an 8-bit single-channel binary image. The pack README calls out the exact error you'll hit otherwise: error: (-215:Assertion failed) img.type() == CV_8UC1. If your mask comes in as BGR, convert with cvtColor (code 6 for BGR2GRAY) first.
  • distanceType - INT. 2 = DIST_L2 (Euclidean - the one you want for feathering), 1 = DIST_L1 (Manhattan, faster, blocky), 3 = DIST_C (Chebyshev).
  • maskSize - INT, 3 or 5. 3 is faster and slightly less accurate; 5 is smoother. Either is fine for mask feathering.
  • dstType - INT. 5 = CV_32F (float distances - the useful one), 0 = CV_8U (byte, only supported with DIST_L1).

Optional dst is an out-parameter - skip it. Output: one nparray of distance values, which you'd feed through Nparrays2Image to get a Comfy IMAGE soft mask.

And the pack-wide plumbing: everything in here is NPARRAY, not Comfy IMAGE - Image2Nparray before, Nparrays2Image after, batch_size==1 only (ImageFromBatch length=1 if needed).

Install: ships in geroldmeisinger/opencv-comfyui - Manager → search "OpenCV", or git clone https://github.com/geroldmeisinger/opencv-comfyui into custom_nodes, restart, with opencv-contrib-python installed. Cannot import name 'guidedFilter' = conflicting OpenCV wheels blocking the whole pack. distanceTransform_1 is the identical overload twin.

Categoryimage/OpenCV

Inputs (5)

NameTypeDefaultDescription
srcNPARRAY
distanceTypeINT
maskSizeINT
dstTypeINT
dstoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY