Nodes/opencv-comfyui/OpenCV matchTemplate_1
ComfyUI Node

OpenCV matchTemplate_1

OpenCV matchTemplate_1 — the duplicate pattern finder, and the five methods decoded

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV matchTemplate_1
  • image
  • templ
  • result
  • mask
  • nparray
method

First, the pack's ritual: matchTemplate_1 and matchTemplate_0 are identical - OpenCV's stubs declare cv2.matchTemplate for MatLike and for UMat, the generator numbered the overloads, and no difference survived. Same inputs, same behavior. Pick whichever's closer and move on.

Second, the function: slide a small template across a bigger image and produce a score map. This is how you answer "where does this logo/patch/UI element appear in this frame?" without loading a model. It's one of the few genuinely practical nodes in the pack - locate a cropped pattern inside a generated image and get a heatmap of where it matches.

The method field, decoded

The method int is the only input you'll actually think about, because it changes which way "good" points:

  • 0 TM_SQDIFF, 1 TM_SQDIFF_NORMED - sum of squared differences. Low = match. 0 is a perfect hit.
  • 2 TM_CCORR, 3 TM_CCORR_NORMED - cross-correlation. High = match, but TM_CCORR is notoriously brightness-sensitive; prefer the NORMED one.
  • 4 TM_CCOEFF, 5 TM_CCOEFF_NORMED - correlation coefficient, the one that actually subtracts the mean first. 5 is the recommended default: robust to brightness/contrast, and "close to 1 = match."

The rest of the inputs:

  • image - the search image (NPARRAY).
  • templ - the template, smaller than image on both axes.
  • result - optional out-parameter; skip it.
  • mask - optional; for masked template matching. Leave it alone unless you know.

Output is a float score map - normalize before previewing, and find the peak yourself; there's no built-in bbox step.

Install

cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui
pip install opencv-contrib-python

Restart, or ComfyUI Manager → "opencv". Requirements: opencv-contrib-python, numpy, torch.

Troubleshooting

  • -215 assertion - template is bigger than the image. Crop it down.
  • "The peak is in the wrong place" - you picked a low=good method and searched for a high value (or vice versa).
  • Score map looks black - it's float; normalize first.
  • Batch error - batch_size==1 only; slice with ImageFromBatch.

Honest limits: template matching is exact-match only - rotate or scale the template and it quietly stops finding anything. For rotation/scale-tolerant matching you'd want feature-point matching instead. But within its lane - finding a fixed, same-size pattern - it's instant, deterministic, and this wrapper gives it to you raw, which is exactly what this pack is for.

Categoryimage/OpenCV

Inputs (5)

NameTypeDefaultDescription
imageNPARRAY
templNPARRAY
methodINT
resultoptNPARRAY
maskoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY