Nodes/opencv-comfyui/OpenCV subtract_0
ComfyUI Node

OpenCV subtract_0

Saturated subtraction — the difference operator that won't wrap

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV subtract_0
  • src1
  • src2
  • dst
  • mask
  • nparray
dtype

subtract_0 is the difference operator you actually want for images, and the detail that makes it better than plain numpy subtraction is one word: saturating. cv2.subtract clamps the result at zero - pixel values can never go negative and wrap around to garbage. If you've ever subtracted two images in numpy and watched bright speckles appear where values underflowed, you know exactly why this node exists.

The practical uses are the classic image-difference jobs. Difference between two video frames → motion/change detection mask. Difference between an image and its blurred copy → the high-frequency detail plane, which is literally the "subtract" half of unsharp masking (blur, subtract, add the difference back scaled). Difference between a source and a processed version → a "what changed" map you can feed into a mask or a compositor. It's one of those primitives you don't think about until you need it, and then it's everywhere.

Inputs that matter:

  • src1 - the image you subtract from.
  • src2 - the image you subtract (can also be a scalar-ish single value in the cv2 sense, but here it's NPARRAY).
  • dtype - INT; -1 keeps the source depth, which is what you want 99% of the time. Pick something else only if you deliberately need to down/upcast the output.
  • mask - optional NPARRAY; if given, only pixels where the mask is nonzero are computed (result elsewhere stays whatever dst held).
  • dst - optional out-parameter; the README says to avoid these.

Output is a single nparray. It's saturating at the bottom, so there's no way to get a signed difference here - if you need negative deltas, that's cv2.absdiff territory (this pack has absdiff_0 too), which returns the magnitude and ignores sign.

The type contract. These are NPARRAY nodes: BGR, uint8, 0–255 numpy arrays, not ComfyUI IMAGE tensors. Convert in with Image2Nparray (batch_size 1 only - ImageFromBatch(length=1) for batches), convert out with Nparrays2Image. Both inputs need matching shapes and depth or cv2 throws an assertion error; mismatch them and you'll know immediately.

Install. ComfyUI Manager → search "opencv-comfyui", or:

cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui

restart. Needs opencv-contrib-python, numpy, torch. The Cannot import name 'guidedFilter' from 'cv2.ximgproc' startup error means conflicting OpenCV packages - resolve that first. And yes, subtract_1 is the identical twin; pick either.

Categoryimage/OpenCV

Inputs (5)

NameTypeDefaultDescription
src1NPARRAY
src2NPARRAY
dtypeINT
dstoptNPARRAY
maskoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY