Nodes/KJNodes for ComfyUI/FilterZeroMasksAndCorrespondingImages
ComfyUI Node Runs on cloud

FilterZeroMasksAndCorrespondingImages

Drop the frames where detection found nothing

By kijai·Created 3 years ago·Updated about 21 hours ago· 2,930
FilterZeroMasksAndCorrespondingImages
  • masks
  • original_images
  • non_zero_masks_out
  • non_zero_mask_images_out
  • zero_mask_images_out
  • zero_mask_images_out_indexes

Run a detector or segmenter over a batch - faces across a video, a subject across a shoot, anything that produces one mask per image - and you will get empty masks back sometimes. No face in that frame, nothing matching your prompt in that shot, whatever. An all-zero mask isn't wrong, it's honest: nothing was found. But it's dead weight for whatever comes next, whether that's an inpainting pass, a crop-and-stitch, or a batch you're about to feed a model that chokes on an empty region. FilterZeroMasksAndCorrespondingImages is the cleanup step: it strips the empty masks out of the batch, and - if you give it the matching images - strips those out too, by index, so the two stay aligned.

How it works

It walks the mask batch, checks each mask for being entirely zero (nothing is masked), and splits the batch into two groups: the ones with actual content and the ones without. If you also pass in original_images, it applies the same split to that batch, so you get the images that go with your real detections separated from the ones that go with your misses - without you having to track indices by hand.

The inputs and outputs that matter

  • masks (required) - your batch of masks straight out of whatever detector or segmenter produced them.
  • original_images (optional) - the images the masks came from, if you want them filtered in lockstep. It has to be the same length as masks, or the indexing won't line up.

Four outputs come back:

  • non_zero_masks_out - just the masks that actually found something. This is what you wire into your next masking step.
  • non_zero_mask_images_out - the corresponding images, if you supplied original_images.
  • zero_mask_images_out - the leftovers: images whose mask came back empty.
  • zero_mask_images_out_indexes - where those misses sat in the original batch, useful if you need to go back and figure out why the detector whiffed on frame 47.

How to install it

Through ComfyUI Manager: search "KJNodes for ComfyUI," install, restart. Or by hand:

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

then restart ComfyUI. Nothing this node does needs anything beyond the base install - it's pure tensor filtering, no models, no downloads.

Common issues & troubleshooting

Everything gets filtered out. If non_zero_masks_out comes back empty, your upstream detector isn't finding anything at all - check its confidence threshold or input, not this node. This is a filter, not a detector; it can only report what it was handed.

Batch length mismatch errors. If you're feeding original_images, it has to have exactly as many images as masks has masks. A common cause is upstream nodes that silently drop or duplicate frames (a resize step, a different batch source) before this one - verify both batches are the same length before this node, not after.

You expected zero_mask_images_out to also filter, not just report. That output genuinely is the miss-pile, by design - it's there so you can inspect or re-run detection on exactly the frames that failed, using the paired indexes to find them again in your original source.

CategoryKJNodes/masking

Inputs (2)

NameTypeDefaultDescription
masksMASK
original_imagesoptIMAGE

Outputs (4)

NameTypeDescription
non_zero_masks_outMASK
non_zero_mask_images_outIMAGE
zero_mask_images_outIMAGE
zero_mask_images_out_indexesINDEXES