OpenCV matchTemplate_0
Find a small image inside a big one, no model required
- image
- templ
- result
- mask
- nparray
"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 thanimagein both dimensions. - method - this is the one that matters, because it flips the score's meaning:
0(TM_SQDIFF) and1(TM_SQDIFF_NORMED) - lower = better.2–5(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
nparrayoutput. - 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. Keeptemplsmaller. - Wrong best-match interpretation - remember which methods are "low = good" (
0,1) vs "high = good" (2–5). Mixing them up is the #1 mistake. - Batch error -
batch_size==1only; slice withImageFromBatch.
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.
Inputs (5)
| Name | Type | Default | Description |
|---|---|---|---|
| image | NPARRAY | — | |
| templ | NPARRAY | — | |
| method | INT | — | |
| resultopt | NPARRAY | — | |
| maskopt | NPARRAY | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| nparray | NPARRAY | — |