OpenCV matchTemplate_1
OpenCV matchTemplate_1 — the duplicate pattern finder, and the five methods decoded
- image
- templ
- result
- mask
- nparray
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:
0TM_SQDIFF,1TM_SQDIFF_NORMED- sum of squared differences. Low = match.0is a perfect hit.2TM_CCORR,3TM_CCORR_NORMED- cross-correlation. High = match, butTM_CCORRis notoriously brightness-sensitive; prefer the NORMED one.4TM_CCOEFF,5TM_CCOEFF_NORMED- correlation coefficient, the one that actually subtracts the mean first.5is 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
imageon 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
-215assertion - template is bigger than the image. Crop it down.- "The peak is in the wrong place" - you picked a
low=goodmethod and searched for a high value (or vice versa). - Score map looks black - it's float; normalize first.
- Batch error -
batch_size==1only; slice withImageFromBatch.
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.
Inputs (5)
| Name | Type | Default | Description |
|---|---|---|---|
| image | NPARRAY | — | |
| templ | NPARRAY | — | |
| method | INT | — | |
| resultopt | NPARRAY | — | |
| maskopt | NPARRAY | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| nparray | NPARRAY | — |