Nodes/ComfyUI-RefineNode/RefineNode Slice And Match Masks
ComfyUI Node

RefineNode Slice And Match Masks

Slice one product mask into a grid, or line up two views of the same product

By 1Kynx·Created 4 months ago·Updated 25 days ago· 30
RefineNode Slice And Match Masks
  • mask1
  • mask2
  • mask1
  • mask2
output_modebbox
min_area_ratio0.01
rows4
columns1
auto_match_orientationtrue
match_modeunion

RefineNodeSliceAndMatchMasks is the advanced mask prep node in ComfyUI-RefineNode, and it does two jobs: it slices a single product mask into a tidy rows-by-columns grid of regions, and it aligns two mask groups that describe the same product across different views, sizes, or proportions. Think "one mask becomes a 4×1 stack of refinement jobs," or "make the mask of the front view and the mask of the side view correspond region-for-region."

The reason either is worth doing: local refinement gets its power from full-resolution crops. Slice a product into a grid and each cell becomes its own refinement job with its own generation budget - four 500px regions beat one 2000px region for fine detail. And when you have two views of the same product (say, the photo you're fixing and the clean reference shot), matching their grids means the region you refine on the source lines up with the equivalent region on the reference, automatically.

How it slices

The node splits disconnected painted islands internally, drops components smaller than min_area_ratio (default 0.01 - relative to the largest component's foreground area, so 0.02 removes anything under 2% of the biggest blob), unions what's left into one product mask, and cuts that product's bounding box into a row-major grid: top to bottom, left to right. rows (default 4) and columns (default 1) set the grid. Want two-dimensional regions? Multiple rows and columns.

auto_match_orientation (default on) is the neat trick: it makes the longer split direction follow the longer side of the product bbox. So rows=4, columns=1 stays 4×1 for a vertical bottle but flips to 1×4 for a horizontal one - the grid always runs along the product, not the canvas.

output_mode decides what each cell contains: mask keeps the original product-mask shape inside each bbox slice (empty cells are skipped, and mask2 keeps the same remaining positions), while bbox outputs full rectangular slices for the most stable spatial correspondence. Default is bbox, and it's the right default for matching views.

How it matches two mask groups

Connect mask1 and mask2 (both optional, at least one required). mask1 anchors the output order; the node applies the same normalized row/column bbox grid to mask2, and each group keeps its own original image size. Then match_mode decides the pairing:

  • union (default) - both sides are unioned into one product each.
  • repeat_mask1 - one shared mask1 reference gets repeated for every mask2 product; mask2 is not unioned, so each mask2 product gets its own matched set against the same reference slices.
  • pair_by_index - mask1[i] with mask2[i], last mask on the shorter side reused when counts differ.
  • pair_by_position - products are paired by normalized position inside each group's union bbox. This is the recommended mode when SAM or detection nodes hand you masks in inconsistent order, because position doesn't care about ordering.

Outputs are mask1 and mask2, both batches. With only one input connected, the second output is an empty placeholder batch - harmless, but know it's there.

Install

ComfyUI Manager, search "ComfyUI-RefineNode", or:

cd ComfyUI/custom_nodes
git clone https://github.com/1Kynx/ComfyUI-RefineNode.git

Restart ComfyUI. No model files, no extra requirements - pure mask math. Its outputs feed straight into RefineNode Preprocess Mask as the refinement job list.

Gotchas

  • mask output mode + empty cells = skipped cells, which changes positions downstream; bbox mode preserves the full grid. If positions must line up across two views, stay on bbox.
  • min_area_ratio is your dust-speck filter. Left at 0.01 it quietly removes tiny specks; set to 0.0 to disable filtering entirely if you have a mask with deliberate small detail.
  • Pairing surprises are almost always an ordering problem - if pair_by_index is misaligned and you can't reorder the inputs, switch to pair_by_position.
CategoryRefineNode/Mask

Inputs (8)

NameTypeDefaultDescription
output_modeCOMBObbox2 options: mask, bbox
min_area_ratioFLOAT0.010–1
rowsINT41–16
columnsINT11–16
auto_match_orientationBOOLEANtrue
match_modeCOMBOunion4 options: union, repeat_mask1, pair_by_index, pair_by_position
mask1optMASK
mask2optMASK

Outputs (2)

NameTypeDescription
mask1MASK
mask2MASK