Nodes/comfyui_cv/CV Crop by Masks
ComfyUI Node

CV Crop by Masks

Crops around each mask of a MASK batch (e.g. from 'Connected Components', 'Labels to Masks' or 'K-Means Mask Clusters') - the mask-driven shorthand for 'OpenCV Masks to BBoxes' -> 'CV Crop by BBoxes', and it takes the SAME policy, so one 'Snap BBoxes' can drive both in lockstep. The policy MODE picks the use case: '-> batch' stacks every crop at one common size so they paste back losslessly (downstream runs once), '-> list' emits one item per region at its own tight size (downstream runs once per region). The source can be an IMAGE, MASK, NPARRAY or LATENT and the crops come back in the same type; masks are fitted to its size, so a LATENT source works with pixel-space masks. Also returns the matching cropped masks (for inpainting) and the crop regions as bounding boxes.

By bmad4ever·Created 3 months ago·Updated 2 days ago· 0
CV Crop by Masks
  • image
  • masks
  • crops
  • crop_masks
  • bboxes
  • region_index
padding16
squarefalse
policyone source, regions -> batch
Categoryimage/CV

Inputs (5)

NameTypeDefaultDescription
imageCOMFY_MATCHTYPE_V3What to crop. IMAGE / MASK / NPARRAY / LATENT; the crops come back in the same type.
masksMASKOne mask per region to crop (MASK batch). Several batches wired in are concatenated.
paddingINT160–4096Extra context around each region in pixels - inpainting works better with surroundings included.
squareBOOLEANfalseForce square crops (useful for diffusion inpainting).
policyoptSTRINGone source, regions -> batchWhich use case this crop serves. 'one source, regions -> batch' crops every region out of frame 0 at one common size and stacks them (the inpainting case - downstream runs once on the batch). '-> list' instead emits one item per region at its own tight size, so downstream runs once per region. The 'per-frame' modes pair region group g with frame g of a batch instead of always reading frame 0. Crop policy. In the UI this is a mode dropdown plus one widget per option; wire an 'CV Snap BBoxes' node in to drive several crop nodes from one place (the widgets then hide). A linked policy overrides only the fields it actually sets. The padding/square widgets above are the baseline; a policy overrides only the fields it sets.

Outputs (4)

NameTypeDescription
cropsCOMFY_MATCHTYPE_V3One stacked batch (batch modes) or one item per region (list modes), in the source's own type.
crop_masksMASKEach region's mask cropped to its crop, fanned out in lockstep with 'crops'.
bboxesBOUNDING_BOXCore BOUNDING_BOX data: per-frame lists of {x, y, width, height} dicts - compatible with Draw BBoxes, Crop By Bounding Boxes, Image Crop, etc. Fanned out in lockstep with 'crops'.
region_indexNPARRAY(B,) int32: which row of the input MASK batch each crop came from. Empty masks produce no crop, so this is what realigns the crops with the input batch.