Nodes/opencv-comfyui/OpenCV distanceTransformWithLabels_1
ComfyUI Node

OpenCV distanceTransformWithLabels_1

The twin of the WithLabels distance transform — pick either

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV distanceTransformWithLabels_1
  • src
  • dst
  • labels
  • nparray_0
  • nparray_1
distanceType
maskSize
labelType

distanceTransformWithLabels_1 is the overload twin of distanceTransformWithLabels_0 - identical cv2.distanceTransformWithLabels call, identical inputs, identical two outputs. The suffix is just the opencv-comfyui generator numbering the function's MatLike and UMat declarations separately, and both collapse to the same node here. As with every _0/_1 pair in this pack, there is nothing to choose between; use whichever you grab.

The actual content is on the _0 page. The short version: this node computes, for every foreground pixel in a binary mask, both the distance to the nearest background pixel and the label of which background region that is. Two outputs come out - nparray_0 (the distance field, the same soft-mask feathering you get from the plain distance transform) and nparray_1 (a labels map, an integer image that IDs which background component each foreground pixel is nearest to). The labels output is the extra that makes this useful for matting and nearest-region association; the distance output alone is usually all most people need, in which case distanceTransform_0/_1 is the simpler tool.

Inputs: src (NPARRAY - must be 8-bit single-channel binary; convert BGR with cvtColor code 6 or you'll hit the README's img.type() == CV_8UC1 assertion), distanceType (INT - 2 for Euclidean), maskSize (INT - 3 or 5), labelType (INT - 0 = DIST_LABEL_CCOMP labels each connected background component; 1 = DIST_LABEL_PIXEL gives every background pixel its own label, and that one wants maskSize 3). Skip the optional dst and labels out-parameters - the node hands you everything on its named outputs. Outputs: nparray_0 (distances) and nparray_1 (labels).

Pack-wide plumbing: NPARRAY in and out, so Image2Nparray before, Nparrays2Image after, batch_size==1 only.

Install: part of geroldmeisinger/opencv-comfyui - ComfyUI Manager → search "OpenCV", or:

cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui

restart, with opencv-contrib-python installed. The pack's classic install failure is Cannot import name 'guidedFilter' from 'cv2.ximgproc' from conflicting OpenCV wheels - sort the wheels and the whole pack loads.

Bottom line: if you're here because the search box gave you two nearly identical names, you now know why, and you can stop worrying about which is which. Decide between the WithLabels family and the plain distanceTransform family based on whether you need the labels output at all.

Categoryimage/OpenCV

Inputs (6)

NameTypeDefaultDescription
srcNPARRAY
distanceTypeINT
maskSizeINT
labelTypeINT
dstoptNPARRAY
labelsoptNPARRAY

Outputs (2)

NameTypeDescription
nparray_0NPARRAY
nparray_1NPARRAY