ComfyUI Node

YOLOE-26 Refine Mask

Fix a rough mask without paying for a second detection pass

By Rinne414·Created 6 months ago·Updated 2 months ago· 11
YOLOE-26 Refine Mask
  • masks
  • refined_masks
  • refined_metadata_json
  • count
methodthreshold
kernel_size3
iterations1
min_area0
metadata_json

Detection is the expensive part of a YOLOE-26 workflow. Once you've paid for it, the last thing you want is to re-run inference just to tidy up the mask. This node is the tidy-up: it takes a mask you already have and applies one of seven cleanup operations, no model involved. The author himself describes the refinement here as "basic (morphological ops)" - that's honest, and it's fine, because basic is usually all you need to make a mask usable for inpainting.

Think of it as the node that turns "a bit noisy, speckled, holey mask" into "a clean mask that won't smear your inpaint." It's the standard post-processing step between segmentation and whatever eats the mask next.

How it works

Each input mask is a binary image, and the node runs a single OpenCV operation over it. No neural network, no re-detection - just fast, deterministic pixel math, so you can chain it in a loop and iterate freely. The operation is set by method, the dropdown with seven entries:

  • threshold - binarizes the mask (its "default" method; mostly a formality since recent Ultralytics masks arrive binary anyway).
  • dilate / erode - grow or shrink the mask boundary. Great for adding a little padding so your inpaint covers the true object edge, or shaving off fringe pixels.
  • open / close - morphological open (erode then dilate) kills small specks and noise; close (dilate then erode) fills small gaps and connects nearby blobs.
  • largest_component - keeps only the biggest connected region. If your mask grabbed a specular highlight or a background blob, this discards the strays.
  • fill_holes - fills enclosed holes. The classic fix for a "donut" mask where a part of the object ended up unmasked.

The other inputs are the tweak knobs: kernel_size (default 3, must be odd) and iterations (default 1) control the morphology strength; min_area (default 0) drops any refined mask smaller than that many pixels, which is your automatic speck-filter. metadata_json is optional and handy: feed it the metadata from an upstream node and it comes back annotated with a refinement record, so your JSON trail stays intact.

Outputs

  • refined_masks - the cleaned mask batch.
  • refined_metadata_json - the upstream metadata (if given) with the refinement parameters stamped on.
  • count - how many masks survived. With min_area set, this tells you how many masks you actually kept.

Installing it

Part of the Rinne414/ComfyUI-YOLOE26 pack - one install, seven nodes. Via ComfyUI Manager (search YOLOE-26) or:

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

Restart after installing. Only real dependency is ultralytics>=8.3.200,<9.0.0; unlike the segment nodes, this one doesn't need the model or the MobileCLIP download at all, because it never runs inference.

Where it fits

Typical chain: Prompt Segment (or Instance Masks) → Refine Mask → your inpaint node. For inpainting specifically, a dilate with a small kernel is the move - the mask hugging the object too tightly is a common cause of halos. fill_holes and largest_component are the two you'll reach for when a segmentation pass clearly grabbed the wrong thing. And because it's pure post-processing, you can drag it into a loop or a batch without worrying about burning GPU time. It won't make a bad mask great - for that you'd go back to detection or use SAM - but it will make a good mask work.

CategoryYOLOE26

Inputs (6)

NameTypeDefaultDescription
masksMASK
methodoptCOMBOthresholdRefinement operation applied to each input mask.
kernel_sizeoptINT31–99Kernel size used by morphology operations.
iterationsoptINT11–32Number of morphology iterations.
min_areaoptINT00–100000000Drop refined masks smaller than this area.
metadata_jsonoptSTRINGOptional upstream metadata JSON to preserve and annotate.

Outputs (3)

NameTypeDescription
refined_masksMASK
refined_metadata_jsonSTRING
countINT