ComfyUI Node

TBG Difference Mask

Find what changed between two images — an SSIM-based diff mask

By Ltamann·Created about a year ago·Updated 7 months ago· 20
TBG Difference Mask
  • image1
  • image2
  • MASK
threshold0.30
min_area2500
invertfalse

Naive image-difference masks have a fatal flaw: abs(a − b) flags every pixel that moved, including noise, JPEG artifacts, and anti-aliasing shimmer, so you end up with a speckled mess instead of "the thing that changed." This node does it properly. It compares two images and produces a clean MASK of where they differ, using a hybrid of SSIM (structural similarity) and raw RGB difference, then filters the result through a GPU morphological area filter that throws away regions smaller than a minimum area. The output is a usable mask, not a noise map.

The source even ships a target use case in a comment: a t-shirt workflow where you pass image1 (the original) and image2 (the modified/designed version), and the node returns a clean mask of the design area - with the author's recommended settings of threshold=0.25, min_area=2500. That's the mental model for everything this node does.

Inputs

  • image1 / image2 - the two images to compare. They must have the same shape or the node raises an error (it checks and fails loudly rather than silently doing something dumb).
  • threshold - how different a region must be to count as "changed," 0–1 (default 0.3). Lower = more sensitive. The t-shirt recipe uses 0.25.
  • min_area - minimum pixel area for a detected region to survive filtering, default 2500. This is what kills the noise specks: isolated tiny differences get removed, real regions stay.
  • invert - flips the mask when you want "everything except what changed" (e.g. background-preservation masking).

Output: a single MASK, ready for a denoise-mask input, inpainting conditioning, or whatever you're driving.

Why the hybrid matters

SSIM alone is excellent at catching structural changes but can miss flat color shifts; raw RGB difference catches color but drowns in per-pixel noise. Blending them (roughly 70/30 in the source) gets you the best of both, and the min_area morphological opening - erosion then dilation - is what actually produces the clean, contiguous regions that make the mask usable for inpainting. For a small utility node that's a lot of engineering, and it's the difference between "works on one image pair" and "works on most."

Install

Manager → search TBG Takeaways → install, restart:

cd ComfyUI/custom_nodes
git clone https://github.com/Ltamann/ComfyUI-TBG-Takeaways

Under TBG/Takeaways after restart. No deps, no downloads.

Where it earns its keep: automated workflows where you need to isolate what a refiner/upscaler changed, or mask-generation for region inpainting - the "difference between original and edit" pattern. Where it won't help: if your two images are drastically different (different compositions), a diff mask is meaningless and you should be masking by hand. For its lane - "same scene, find the delta" - it's a genuinely better diff than the naive subtract nodes floating around.

CategoryTBG/Takeaways

Inputs (5)

NameTypeDefaultDescription
image1IMAGE
image2IMAGE
thresholdFLOAT0.300–1
min_areaINT25000–50000
invertBOOLEANfalse

Outputs (1)

NameTypeDescription
MASKMASK