Nodes/comfyui_bmad_nodes/DistanceTransform
ComfyUI Node Runs on cloud

DistanceTransform

How far is every pixel from the edge? This node answers that

By bmad4ever·Created 3 years ago·Updated 9 months ago· 70
DistanceTransform
  • binary_image
  • IMAGE
distance_typeDIST_L2
mask_sizeDIST_MASK_3

DistanceTransform is one of those OpenCV functions that sounds academic until you have the exact problem it solves. Feed it a binary mask and it returns a distance map: every pixel's value is how far that pixel is from the nearest background pixel. Bright pixels are deep inside a shape; dark pixels are right at its edge. It's the tool for "how thick is this mask here?" questions, and it quietly powers a lot of mask-processing tricks.

The classic uses in a ComfyUI masking pipeline:

  • Analyzing a mask - a distance map instantly shows you whether a mask region is uniformly thick or has thin pinch points that will break later.
  • Mask prep - combine a distance transform with a threshold and you can thicken a mask by a controlled number of pixels, or erode only the thin parts while keeping the thick core.
  • Seeding - distance maps are the standard input to watershed-style segmentation, and they're the traditional first step in skeleton/medial-axis work.

How it works

The node wraps cv.distanceTransform. Input is binary_image - a binary mask where foreground pixels (255) are the things you're measuring from. Two settings matter:

  • distance_type - DIST_L2 (Euclidean, the true geometric distance and the one you almost always want), DIST_L1 (Manhattan distance), or DIST_C (Chebyshev). The non-Euclidean ones are cheaper and occasionally useful for grid-aligned shapes.
  • mask_size - DIST_MASK_3 / DIST_MASK_5 / DIST_MASK_PRECISE. This controls how the L2 distance is approximated; PRECISE gives exact distances, the small masks are faster approximations. For most use, DIST_MASK_3 is fine and the difference is rarely visible.

Output is a single IMAGE. The distance values are mapped into the 0–255 range for display - so read it as a relative distance map (brighter = farther in), not as literal pixel counts.

Install

Ships in bmad4ever/comfyui_bmad_nodes under Bmad/CV/Thresholding. Install via ComfyUI Manager (search comfyui_bmad_nodes) or:

cd ComfyUI/custom_nodes
git clone https://github.com/bmad4ever/comfyui_bmad_nodes
cd comfyui_bmad_nodes
pip install -r requirements.txt

Restart. This node is pure OpenCV (opencv-python~=4.8.1.78 is pinned in requirements.txt); nothing else in the pack's dependency list matters for it. No models.

Gotchas

The input really should be binary - 0 and 255 only. Feed it a soft-edged or anti-aliased mask and the "distance to background" math gets fuzzy in a literal sense: mid-gray pixels count as neither fully inside nor outside, and your map comes out muddy. Threshold the mask first if it has soft edges. And remember the output is normalized to 0–255, so if you chain this into a "thicken by exactly 5px" workflow, you'll need to threshold the distance map yourself - the raw output doesn't carry real pixel units.

CategoryBmad/CV/Thresholding

Inputs (3)

NameTypeDefaultDescription
binary_imageIMAGE
distance_typeCOMBODIST_L23 options: DIST_L2, DIST_L1, DIST_C
mask_sizeCOMBODIST_MASK_33 options: DIST_MASK_3, DIST_MASK_5, DIST_MASK_PRECISE

Outputs (1)

NameTypeDescription
IMAGEIMAGE