Nodes/comfyui_cv/CV HFS Segmentation (hierarchical feature selection)
ComfyUI Node

CV HFS Segmentation (hierarchical feature selection)

Segments an image into regions via hierarchical feature selection (cv2.hfs): SLIC superpixels are merged by texton + colour similarity over two gradient-threshold stages. Outputs a per-pixel region index map (uint16, one region label per pixel), a region-coloured version of the image (each region is repainted with its average colour), and the region count. Works without a GPU (CPU path). The merge thresholds trade region purity against region count: raise them to merge more aggressively, and lower 'spatial weight' to let colour/texture dominate over proximity.

By bmad4ever·Created 3 months ago·Updated 2 days ago· 0
CV HFS Segmentation (hierarchical feature selection)
  • image
  • segmented
  • labels
  • num_regions
egb_threshold_i0.08
min_region_size_i100
egb_threshold_ii0.28
min_region_size_ii200
spatial_weight0.60
superpixel_size8
slic_iterations5
Categoryimage/CV/contrib

Inputs (8)

NameTypeDefaultDescription
imageCOMFY_MATCHTYPE_V3Full-colour image to segment (3 channels). The region index map has one label per pixel and the same height/width. Accepts a ComfyUI IMAGE/MASK directly (frame 0 of a batch) or an NPARRAY. Arithmetic ops (add, multiply, etc.) process the full IMAGE batch when both inputs have the same batch size.
egb_threshold_ioptFLOAT0.080–1Stage-I merge threshold: gradient below this and two regions merge. Higher = fewer, larger regions (and less detail kept).
min_region_size_ioptINT1001–100000Stage-I minimum region size in pixels; regions smaller than this get absorbed into a neighbour.
egb_threshold_iioptFLOAT0.280–1Stage-II threshold, applied to the coarser regions. Usually comfortably larger than egb_threshold_i, hence the second coarser pass.
min_region_size_iioptINT2001–100000Stage-II minimum region size in pixels; the final merge removes regions smaller than this.
spatial_weightoptFLOAT0.600–1Weight of spatial distance inside the SLIC clustering (0 = colour/texture only, 1 = pure proximity). The default already leans on colour.
superpixel_sizeoptINT82–256SLIC superpixel size in pixels. Smaller = more seeds = finer detail captured; larger runs faster and over-merges small features.
slic_iterationsoptINT51–50SLIC clustering iterations. 5 is plenty for convergence; raise to 10 on very textured content.

Outputs (3)

NameTypeDescription
segmentedCOMFY_MATCHTYPE_V3The input with every region repainted by its average colour - a quick visual of the region map.
labelsNPARRAYuint16 per-pixel region index (0..num_regions-1), the same height/width as the input. Feed this into mask/crop/Draw Nodes.
num_regionsINTNumber of distinct regions in the label map.