ComfyUI Node

Distance Map

Find the true middle of a mask (or its edge)

By Quasimondo·Created 2 years ago·Updated about a year ago· 15
Distance Map
  • mask
  • image
  • x
  • y
xy_mode

Where do you put a subject inside an oddly-shaped mask? Which pixel is most interior? Distance Map answers that kind of question. It takes a binary mask, computes the distance transform - how far each white pixel is from the nearest black one - and then hands you both the distance map itself and the coordinates of a point you pick by mode: the pixel furthest from any edge, the one closest to an edge, or the one at the median distance.

It's a small node that solves a surprisingly common fiddly problem: "give me a sensible point inside this region." If you've got a hand-drawn mask and you want to know where a detailer should focus, or where a composite should be anchored, this is the cheap deterministic answer - no model, no guessing.

How it works

The code takes the first mask in the batch, thresholds it to binary, then runs OpenCV's distanceTransform with DIST_L2 and the precise mask. Every white pixel now carries a float: its Euclidean distance to the nearest black pixel. The result is normalized to 0–1. Then it picks one pixel based on xy_mode:

  • max - the pixel furthest from the nearest edge. For a solid blob, that's effectively the deepest interior point (roughly the center, though not a geometric centroid - it's the point of maximum clearance).
  • min - the pixel closest to an edge. On a normal mask that lands on the boundary.
  • median - the pixel whose distance value is the median of all distances in the map. A kind of "typical interior point."

Outputs are the distance map itself (as a MASK), plus x and y as integers. The coordinates are the real payload for most uses; the map is a nice visualization and can double as a softness/falloff mask since it peaks at the interior.

The inputs that matter

There are exactly two, which is the whole charm:

  • mask - the region. Only the first mask of a batch is processed, so if you feed a batch you'll get results for the first frame only.
  • xy_mode - max, min, or median, as above. Defaults to max.

Install

From the Quasimondo pack. ComfyUI Manager → search ComfyUI-QuasimondoNodes, or by hand:

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

No models to download. The node leans on OpenCV (from the pack's opencv-contrib-python dependency) for the distance transform.

Where people get burned

The min mode is the one that surprises people. On a well-formed mask it just returns an edge pixel, which is usually not what you wanted - most people reach for max. And if your mask is entirely white (no black at all), the distance transform is all zeros, and max will still return a pixel but the "furthest point" is meaningless. The median mode is the least intuitive; it answers "what's the distance at the midpoint of all distances," which is a weird statistic unless you specifically want it.

Also, remember it operates on the first mask only. If you're iterating over a batch of masks expecting per-mask coordinates, you'll get the first one's answer every time. For a one-off "where's the center of this region," though, it's exactly right.

Categorygenerator

Inputs (2)

NameTypeDefaultDescription
maskMASK
xy_modeCOMBO3 options: max, min, median

Outputs (3)

NameTypeDescription
imageMASK
xINT
yINT