OpenCV distanceTransformWithLabels_0
Which background is each mask pixel closest to?
- src
- dst
- labels
- nparray_0
- nparray_1
distanceTransformWithLabels_0 is the smarter cousin of the plain distance transform, and it answers a question the plain one can't: not just how far each foreground pixel is from the nearest background, but which background region it's nearest to. It runs cv2.distanceTransformWithLabels and hands you two outputs - the distance field and a labels map - and together they let you do nearest-region assignment on a mask.
Where that earns its keep: any time you need to associate parts of a foreground object with their nearest background islands. Think matting - you have a subject mask floating over a scene, and you want to know which side of the subject sits closest to which background patch, so a re-render or a gradient can fall off in the right direction. Or think of it as a quick Voronoi-style partition of the foreground driven by the background's shape. It's more niche than the plain feathering use case, and honestly most people will never need it - but when the question is "which background is this mask region nearest to," there's no easier way to ask it.
The inputs that matter:
src- NPARRAY, an 8-bit single-channel binary image. Same rule as the plain transform: a BGR or multi-channel input trips theimg.type() == CV_8UC1assertion from the README - convert withcvtColor(code 6) first.distanceType- INT (2=DIST_L2,1=DIST_L1,3=DIST_C).maskSize- INT,3or5. Small wrinkle: if you setlabelTypetoDIST_LABEL_PIXEL(1), OpenCV expectsmaskSize3; keepDIST_LABEL_CCOMP(0) if you want the 5×5 mask.labelType- INT.0=DIST_LABEL_CCOMP- each connected component of background pixels gets its own label, so all the zero pixels in one blob share an ID.1=DIST_LABEL_PIXEL- every background pixel is its own label; more granular, usually overkill.
Optional dst and labels are out-parameters - skip both; the node returns everything on its outputs. Outputs: nparray_0 (the distance field, same as the plain transform) and nparray_1 (the labels - an integer image where the value at each foreground pixel is the label of its nearest background region). Wire the distance output to Nparrays2Image for a soft mask, or pipe the labels somewhere that can use an ID map.
Pack-wide plumbing applies: Image2Nparray in, Nparrays2Image out, batch_size==1 only.
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. The pack-wide Cannot import name 'guidedFilter' conflict error blocks everything if you have duplicate OpenCV wheels. distanceTransformWithLabels_1 is the identical overload twin.
Inputs (6)
| Name | Type | Default | Description |
|---|---|---|---|
| src | NPARRAY | — | |
| distanceType | INT | — | |
| maskSize | INT | — | |
| labelType | INT | — | |
| dstopt | NPARRAY | — | |
| labelsopt | NPARRAY | — |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| nparray_0 | NPARRAY | — |
| nparray_1 | NPARRAY | — |