Nodes/opencv-comfyui/OpenCV matchTemplate_0
ComfyUI Node

OpenCV matchTemplate_0

Find a small image inside a big one, no model required

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

"Where in this image is that thing?" That's template matching, and OpenCV matchTemplate_0 wraps it as a node. You give it a search image and a small template - a cropped logo, a UI element, a distinctive patch - and it slides the template across every position of the image, computing a match score at each spot. The output is a heatmap of scores: bright where the template lines up, dim elsewhere. It's the same idea behind "find the Waldo" algorithms, minus any machine learning.

It's one of the most genuinely useful nodes in the pack, because unlike the other wrapper functions it solves a real problem people have in workflows: locating a known pattern in a generated frame. Found a watermark, a repeated logo, or a specific object patch and want to know where it is? Crop it once, feed it as the template, and get a map of its positions.

The inputs

  • image - the big search image, NPARRAY.
  • templ - the small template, NPARRAY, strictly smaller than image in both dimensions.
  • method - this is the one that matters, because it flips the score's meaning:
    • 0 (TM_SQDIFF) and 1 (TM_SQDIFF_NORMED) - lower = better.
    • 25 (TM_CCORR, TM_CCORR_NORMED, TM_CCOEFF, TM_CCOEFF_NORMED) - higher = better.
    • 5 (TM_CCOEFF_NORMED) is the usual pick: normalized, robust to brightness, and "close to 1 = match."
  • result - optional out-parameter; ignore it, the map comes back on the nparray output.
  • mask - optional; a mask for template matching with masked templates. Leave it unless you know you need it.

The output nparray is a float score map, not a viewable image - you'll want to normalize it before previewing, and you still have to interpret it (find the peak) yourself. There's no built-in "give me the bounding box" step; that's on you or another node.

Install

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

Restart, or ComfyUI Manager → "opencv". Dependencies: opencv-contrib-python, numpy, torch. No models.

Troubleshooting

  • Dark float output - normalize the score map before previewing.
  • Template bigger than the image - OpenCV asserts (-215) on that. Keep templ smaller.
  • Wrong best-match interpretation - remember which methods are "low = good" (0, 1) vs "high = good" (25). Mixing them up is the #1 mistake.
  • Batch error - batch_size==1 only; slice with ImageFromBatch.

The _1 variant is identical (overload-numbering quirk). Honest take: matchTemplate is exact - it won't find a rotated or scaled instance of your template, only an upright same-size one. For that, you'd need feature matching. But for finding a fixed pattern in a frame, it's fast, free, and this node gives it to you raw.

Categoryimage/OpenCV

Inputs (5)

NameTypeDefaultDescription
imageNPARRAY
templNPARRAY
methodINT
resultoptNPARRAY
maskoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY