ComfyUI Node

AD_sam_stitch

Paste refined video crops back into place without a single seam

By cardenluo·Created 2 years ago·Updated a day ago· 320
AD_sam_stitch
  • stitch_img
  • transform
  • masks
  • images
paste_regionfull_crop
mask_dilation16
feather6
colour_match1.00
blend1.00
undetected_framesfade_out

Every track-and-edit pipeline ends the same way: you generated a better version of one region, and now you have to put it back without leaving a visible edge. AD_sam_stitch is that final step - it takes the refined crops your sampler produced, and warps each one back into the exact position SAM3 tracked, blending it into the original frames so the edit doesn't look pasted.

It's the closing node of the pack's chain: AD_sam_Crop tracks and crops → AD_Inject_Latent re-generates the region → AD_sam_stitch puts it back. If you've ever done region re-generation with a naive paste, you know the two failure modes: the patch floats because it isn't warped to the tracked box, and the seam glows because there's no feathered blend. This node does both, and adds color matching on top.

Inputs that matter

  • stitch_img (IMAGE) - the refined crops, same canvas and frame count as AD_sam_Crop produced. Must match the canvas the transform expects (crop_width/crop_height).
  • transform (H3FACEXFORM) - the tracking metadata from AD_sam_Crop. It carries the per-frame boxes, the source frame size, and the per-frame confidence weights. If the source image dimensions don't match what the transform was built from, the node raises a clear error instead of silently misaligning.
  • paste_region - what gets pasted: full_crop (the whole refined crop canvas), obj_only (just the object rectangle), or obj_ellipse (the object as an ellipse - nice for faces/heads where a rectangle shows corners).
  • mask_dilation (INT, default 16) - expands the object mask before pasting, in case the refined region needs a little breathing room.
  • feather (INT, default 6) - the blend ramp at the paste edge. Too low and you see a hard line; too high and the edit bleeds into surrounding pixels.
  • colour_match (FLOAT, default 1) - 0–1 blend toward a mean/std color match between the patch and the underlying frame. This is the one that hides "the pasted face has different lighting" tell. 1 fully matches, 0 leaves the patch as-is.
  • blend (FLOAT, default 1) - global opacity of the paste.
  • undetected_frames - what to do on frames where SAM3 lost the track: fade_out (default, fade the paste out using the track weights), skip (leave those frames untouched), or composite_anyway (paste anyway, trusting the interpolation).
  • masks (optional MASK) - supply your own per-frame masks to define the paste region instead of the tracked boxes.

Output is images (IMAGE) - the composited frames.

How it works

For each frame it builds an affine transform from the tracked box, resamples the refined crop back to source resolution, and builds a paste mask (feathered, dilated, optionally elliptical). Then it does the color match - matching the patch's mean and standard deviation to the underlying frame inside the mask region - before the final blend. It also chunks the work to keep VRAM in check on long clips and bails out cleanly if you cancel mid-run. The per-frame confidence weights from AD_sam_Crop feed the fade_out mode, which is why undetected frames tend to dissolve instead of pop.

Where people get burned

The classic wiring bug: stitch_img must be the refined crops at the exact same canvas size the transform expects - mismatch errors here mean the crop canvas and the generation resolution diverged upstream. And colour_match is the dial people forget: at 1 it can flatten the patch's lighting so it matches the current frame; if you're editing a moving subject under changing light, drop it to ~0.5–0.8. It's also worth remembering the masks output from AD_sam_Crop can be passed into the optional masks input if the automatic ellipse/rect regions aren't right for your subject.

Installing it

From cardenluo/ComfyUI-Apt_Preset, the usual way:

cd ComfyUI/custom_nodes
git clone https://github.com/cardenluo/ComfyUI-Apt_Preset
pip install -r requirements.txt   # or install.bat on Windows

Restart ComfyUI. This node itself needs no model downloads - just the pack - but the pipeline it's part of needs the SAM3 checkpoint (in models/checkpoints) and native SAM3 support on the tracking side.

CategoryApt_Preset/AD

Inputs (9)

NameTypeDefaultDescription
stitch_imgIMAGE
transformH3FACEXFORM
paste_regionCOMBOfull_crop3 options: obj_only, obj_ellipse, full_crop
mask_dilationINT160–256
featherINT60–256
colour_matchFLOAT1.000–1
blendFLOAT1.000–1
undetected_framesCOMBOfade_out3 options: fade_out, skip, composite_anyway
masksoptMASK

Outputs (1)

NameTypeDescription
imagesIMAGE