Nodes/GraftingRayman/GR Image/Depth Mask
ComfyUI Node

GR Image/Depth Mask

Turn a depth map into a usable mask with one threshold slider

By GraftingRayman·Created 2 years ago·Updated about a month ago· 76
GR Image/Depth Mask
  • image
  • MASK
threshold0.50
invertfalse
blur_radius0.0
blur_radius_expand0.0
brightness1.0
contrast1.0
channelall
expand0
contract0

A depth map already encodes exactly what you need to isolate a subject from its background - white is close, black is far - and this node's whole idea is to skip segmentation models entirely and just threshold that brightness directly into a mask. If your subject is clearly nearer to the camera than everything else, this is a much cheaper way to get a usable mask than running SAM or a background-removal network, because the geometry information is already sitting right there in the depth map.

How it works

Feed it a depth map (the image input - this expects a depth image, not a regular photo) and a threshold. Everything on one side of the threshold becomes mask, everything on the other side doesn't - invert flips which side is which. From there it's cleanup: blur_radius and blur_radius_expand soften the mask edge instead of leaving a hard cutoff at the threshold line, brightness and contrast let you adjust the depth map before thresholding (useful if your depth map is low-contrast and the threshold isn't landing where you expect), channel picks which color channel to read the depth value from if it isn't a plain grayscale image, and expand/contract grow or shrink the resulting mask - the same "grow mask" operation you'd reach for after any segmentation step, just built in here.

This only works as well as your depth map does. A clean, well-separated depth map (Depth Anything V2 is the current default preprocessor most people reach for) gives you a clean mask; a noisy or low-contrast one gives you a noisy mask, no matter how you tune the threshold.

The inputs and outputs that matter

  • image - your depth map, not a regular photo.
  • threshold (default 0.5, 0-1 range) - the one control that decides what counts as foreground. Push it toward 0 to keep more of the scene as mask, toward 1 to keep less; where exactly it should sit depends entirely on the depth map's own value range.
  • invert - flip foreground/background if the mask comes out backwards.
  • expand / contract - grow or shrink the mask after thresholding, useful for cleaning up a ragged edge or making sure you're not cutting right at the subject's boundary.
  • channel - which channel to read depth from (alpha, red, green, blue, all); leave at all unless your depth source packs the value into a specific channel.

One output: MASK, ready to feed into an inpainting node, a compositing step, or anywhere else a mask is expected.

How to install it

Search GraftingRayman in ComfyUI Manager, or clone manually:

cd ComfyUI/custom_nodes
git clone https://github.com/GraftingRayman/ComfyUI_GraftingRayman

Restart afterward. This pack needs OpenAI's CLIP installed as a separate step or none of it imports - portable ComfyUI: .\python_embeded\python.exe -m pip install git+https://github.com/openai/CLIP.git; system Python: pip install git+https://github.com/openai/CLIP.git.

Common issues & troubleshooting

Pack won't load. Check your console at startup for a missing-CLIP import error and run the pip install above - it blocks every node in the pack, this one included.

Mask comes out solid or empty. Your depth map's value range probably doesn't sit where threshold expects. Check the depth map's actual brightness range first - a depth map that's mostly mid-gray with a narrow range will need a threshold much closer to that midpoint than the default 0.5.

Mask is inverted from what you want. Toggle invert - depth-based masks flip foreground/background depending on the source model's convention, and there's no universal standard for which way a given depth preprocessor encodes near versus far.

Edges are ragged or noisy. Raise blur_radius a bit before reaching for anything more elaborate - a soft edge on the mask often looks better in the final composite than a hard-edged mask straight from a threshold operation.

CategoryGraftingRayman/Mask

Inputs (10)

NameTypeDefaultDescription
imageIMAGE
thresholdFLOAT0.500–1
invertBOOLEANfalse
blur_radiusFLOAT0.00–10
blur_radius_expandFLOAT0.00–10
brightnessFLOAT1.00–2
contrastFLOAT1.00–2
channelCOMBOall5 options: alpha, red, green, blue, all
expandINT00–100
contractINT00–100

Outputs (1)

NameTypeDescription
MASKMASK