Nodes/ComfyUI_Gemini/🧩 Gemini Separate Mask Components
ComfyUI Node

🧩 Gemini Separate Mask Components

Split one mask into its individual blobs

By aarnoatchiΒ·Created 2 months agoΒ·Updated 2 months agoΒ· 0
🧩 Gemini Separate Mask Components
  • mask
  • masks
  • colored_preview
  • count
β—„threshold0.50β–Ί
β—„connectivity8β–Ί
β—„min_area0β–Ί
β—„max_components0β–Ί
β—„sort_byarea_descβ–Ί

Gemini Separate Mask Components takes a mask with several disconnected white blobs and splits it into a batch of masks, one per blob. A segmentation pass over a photo of a table gives you one mask with five separate objects in it; this node hands you five masks, each containing exactly one object. It's the missing step between "the model found everything" and "I want to work on just the mug."

It's local, no API, no key - and it's a drop-in replacement for Masquerade's node of the same name, written to be faster and more flexible. If you already know that node, the workflow migration is zero.

How it works

The core is connected-component labeling: it marks which white pixels are connected to which, and each connected group becomes its own mask. The interesting knobs:

  • connectivity - 8 (default) includes diagonals, so blobs touching at a corner merge into one; 4 only joins up/down/left/right, so corner-touching blobs stay separate. For most subject matter the 4-vs-8 distinction only bites on thin diagonal lines, but it's there when it matters.
  • threshold (default 0.5) - a pixel counts as "filled" when its value is above this. Your masks are probably already binary; this mostly matters for soft, feathered masks.
  • min_area - discards components smaller than N pixels. This is your speckle killer - a segmentation mask with noise dots becomes just the real objects.
  • max_components - cap on how many to keep after sorting (0 = keep all).
  • sort_by - area_desc (biggest first, the default), area_asc, or left_to_right/top_to_bottom by centroid position. Order matters more than it sounds: it determines which mask comes out at index 0, which is what you're picking from downstream.

Implementation-wise it uses scipy.ndimage.label for a single fast pass, with a pure-torch fallback if scipy is missing. Either way, every blob comes out as its own item in the masks output batch.

Outputs and wiring

Three outputs: masks (the MASK batch, one per blob), colored_preview (every blob tinted a different color as an IMAGE, so you can actually see the split), and count (how many blobs you got). The README's own suggested pairing is the tell: feed masks into Gemini Image Select (or Mask Select) and pick the blob you want by index - that's the "segment everything, then grab the one object" pattern.

Install

Standard pack install: ComfyUI Manager search "ComfyUI_Gemini", or git clone https://github.com/aarnoatchi/ComfyUI_Gemini into custom_nodes plus pip install -r requirements.txt. This node is one of the two that really wants scipy (the other is Garment Auto Crop); without it you get a slower pure-torch labeling path, so let the requirements install it.

The most common surprise is blobs merging when you expected them separate - that's usually connectivity set to 8 and two shapes touching at a corner, or a feathered mask where low-value fringe pixels connect regions. Set connectivity to 4 and check the threshold. And if count looks like it's missing tiny specks you wanted, drop min_area to 0 - the default keeps everything, but an upstream threshold can leave crumbs behind.

CategoryAI/Image Analysis/Google

Inputs (6)

NameTypeDefaultDescription
maskMASKA mask (black background, white blobs). Batched masks are all processed.
thresholdoptFLOAT0.500–1A pixel counts as 'filled' when its value is above this. White=1, black=0.
connectivityoptCOMBO8Which neighbours join a blob: 8 β€” includes diagonals (blobs touching at a corner merge) 4 β€” only up/down/left/right (corner-touching blobs stay separate)
min_areaoptINT00–1000000000Discard components smaller than this many pixels (0 = keep all). Kills speckle noise.
max_componentsoptINT00–4096Keep at most this many components after sorting (0 = keep all).
sort_byoptCOMBOarea_descOrder of the output masks: area_desc β€” biggest blob first (default) area_asc β€” smallest first left_to_right / top_to_bottom β€” by centroid position

Outputs (3)

NameTypeDescription
masksMASKβ€”
colored_previewIMAGEβ€”
countINTβ€”