Nodes/lf-nodes/Region Mask
ComfyUI Node

Region Mask

Turn a detected box into a usable mask

By lucafoscili·Created 2 years ago·Updated 7 days ago· 35
Region Mask
  • image
  • region_meta
  • ui_widget
  • mask
  • mask_list
  • region
region_index-1
shaperectangle
padding0.05
padding_px0.0
feather0.05
invertfalse

The detection nodes find the box. LF_RegionMask is what turns that box into something you can actually use - a real MASK you can pipe into an inpaint, a regional prompt, or a composite. It takes the region_meta output from LF_DetectRegions and carves a mask out of the selected region's bounding box, with padding and feathering built in so the mask doesn't look like a hard-edged rectangle slapped on the image.

This is the step people skip and then regret. A raw detection box is a rectangle; an inpaint wants a mask that covers the subject's shape and blends at the edges. That's exactly what this node exists to produce.

How it works

Required inputs: image (whose dimensions define the mask canvas) and region_meta (from any region-detection node, including LF_DetectRegions). The optional settings are the ones you'll actually tune:

  • region_index - -1 (default) uses the region flagged as selected by the detector; set an explicit index to force another one from the list.
  • shape - rectangle or ellipse, i.e. how aggressively the mask is carved inside the box. Ellipse is good for faces and round subjects; rectangle for anything you want fully covered.
  • padding (default 0.05) - fractional padding relative to region size. A little positive padding keeps masks from clipping the subject's edges.
  • padding_px - absolute pixel padding on top of the fractional kind. Use whichever reads more naturally for your image size.
  • feather (default 0.05) - softens the mask edge so inpainting blends instead of cutting. 0 keeps a hard edge.
  • invert - flips the mask to everything outside the region. Handy for "change the background but keep the subject."

Outputs

mask / mask_list - the carved mask tensor(s), ready for inpainting or compositing nodes. It also returns region (the metadata of the region used), so you can chain downstream nodes that want to know which box produced the mask. The compare widget previews original vs. mask.

Installing it

lf-nodes is one pack:

cd ComfyUI/custom_nodes
git clone https://github.com/lucafoscili/lf-nodes

restart ComfyUI, or install via ComfyUI Manager ("LF Nodes"). No extra model downloads - this is pure mask math on CPU/GPU.

Common issues

  • Mask clipped at the subject's edge - bump padding and padding_px. Detection boxes are often a hair tight.
  • Hard seams after inpainting - you've left feather at 0, or very low. A little feathering is the difference between a patch and a blend.
  • Empty mask - region_index points at a region that doesn't exist, or the region_meta came from a graph run that produced nothing. Re-run detection first.
Category✨ LF Nodes/Regions

Inputs (9)

NameTypeDefaultDescription
imageIMAGEImage whose dimensions define the mask canvas.
region_metaREGION_METARegion metadata dict/list produced by detection nodes.
region_indexoptINT-1-1–256Index of the region to use; -1 selects the region flagged as selected.
shapeoptCOMBOrectangleMask shape to carve inside the bounding box.
paddingoptFLOAT0.050–1Padding factor relative to region size.
padding_pxoptFLOAT0.00–1024Additional padding in absolute pixels.
featheroptFLOAT0.050–1Feather amount; 0 keeps a hard edge.
invertoptBOOLEANfalseInvert the mask (highlight everything outside the region).
ui_widgetoptLF_COMPARE[object Object]Optional compare widget preview (original vs mask).

Outputs (3)

NameTypeDescription
maskMASKRegion mask output.
mask_listMASKRegion mask output as a list.
regionREGION_METAMetadata of the region used to build the mask.