Nodes/ComfyUI-YoloTrack/Mask Crop & Extract
ComfyUI Node

Mask Crop & Extract

Crop Any Mask to Its Bounding Box, With Coordinates — That's the Node

By tppp2806·Created 9 months ago·Updated 9 months ago· 0
Mask Crop & Extract
  • mask
  • image
  • cropped_mask
  • cropped_image
  • x
  • y
  • width
  • height
  • x2
  • y2

Mask Crop & Extract (class MaskCropExtractNode) is the quiet utility in the ComfyUI-YoloTrack pack, and honestly it might be the node you use most. It does almost nothing - which is why it's so useful. Give it a mask, get back the mask cropped to its bounding box plus the box coordinates. Give it an image too, and it crops that as well, so you can pull the exact pixels any mask covers.

Detection and SAM nodes hand you full-frame masks, and most downstream jobs want just the object. ComfyUI has other crop-by-mask nodes, but this one is batch-safe and explicitly hands back coordinates (x, y, width, height, x2, y2) - handy when you're wiring numbers into nodes that want explicit boxes. It's the natural finishing touch after this pack's own YOLO Detection & Selection node: grab its mask, crop it tight, and feed cropped_image into an upscaler, inpaint, or detail pass.

How it works

Under the hood it's exactly as simple as it sounds: find the rows and columns where the mask is nonzero, take the min and max, crop both mask and (optional) image to that box, and report the coordinates. Empty masks don't crash it - you get a 1×1 zero mask and all-zero coordinates. But that's also the trap: check width and height before wiring the crop downstream, because an all-zero mask in means a 1×1 blank out.

Inputs: mask (required), image (optional). Outputs: cropped_mask, cropped_image, and the six box coordinates. If you don't provide an image, cropped_image comes back as a 1×1 zeros placeholder - harmless, just don't feed it to anything expecting real pixels.

Installing it

Same pack as the YOLO nodes. ComfyUI Manager: search YoloTrack. Or:

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

Then restart ComfyUI. The good news: this node needs no YOLO model at all - it's pure torch/numpy slicing, so if you only want this utility you can install the pack and skip the model download entirely.

Gotchas

  • It crops to the mask's bounding box and nothing else - no padding, no expansion. If you need breathing room, add it with a pad/crop node downstream.
  • All-zero mask in → 1×1 zeros out. Guard on the coordinate outputs before trusting a crop.
  • The README's manual install snippet contains a your-username placeholder; use the URL above.

Thin node, thin article - that's appropriate. It's a ten-line utility done right, and it pairs beautifully with the rest of this pack.

CategoryYOLO

Inputs (2)

NameTypeDefaultDescription
maskMASK
imageoptIMAGE

Outputs (8)

NameTypeDescription
cropped_maskMASK
cropped_imageIMAGE
xINT
yINT
widthINT
heightINT
x2INT
y2INT