# Maximum template matching speed

**URL:** <https://forum.opencv.org/t/maximum-template-matching-speed/7725>\
**Category:** Python\
**Created:** [February 26, 2022, 1:56pm UTC](https://forum.opencv.org/t/maximum-template-matching-speed/7725 "2022-02-26T13:56:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![J\_B](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/j_b/32/4625_2.png) [@J\_B](https://forum.opencv.org/u/J_B)\
**Post date:** [February 26, 2022, 1:56pm UTC](https://forum.opencv.org/t/maximum-template-matching-speed/7725/1 "2022-02-26T13:56:01Z")

</div>

What hardware upgrades would be ideal for maximizing speed when doing large numbers of template matching. Talking hundreds of thousands of images. Can gpus be used?

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [February 26, 2022, 3:06pm UTC](https://forum.opencv.org/t/maximum-template-matching-speed/7725/2 "2022-02-26T15:06:47Z")

</div>

does it _have to be_ template matching? would convolution/correlation work too?

the general `matchTemplate` has OpenCL kernels, so that might work on any (CPU and) GPU.

---

<div class="post-metadata">

**Author:** ![J\_B](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/j_b/32/4625_2.png) [@J\_B](https://forum.opencv.org/u/J_B)\
**Post date:** [February 26, 2022, 4:43pm UTC](https://forum.opencv.org/t/maximum-template-matching-speed/7725/4 "2022-02-26T16:43:01Z")

</div>

I know exactly what im looking for (part of a document)… other than noise caused by the quality of the document.

My accuracy is great, but just wanted to see how fast i could get it going. I assumed it was limited by the CPU. You think convolution/correlation might perform faster?

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [February 26, 2022, 6:32pm UTC](https://forum.opencv.org/t/maximum-template-matching-speed/7725/5 "2022-02-26T18:32:30Z")

</div>

correlation and convolution are nearly the same operation, just mirrored.

convolution can be done in O(n log n) because it can be implemented as fourier transform and elementwise multiplication of the spectra.

opencv’s matchTemplate _is_ calculating variants of correlation. the equations are detailed in the docs. it does a little more than regular correlation because some of the equations involve subtraction. because it can take a mask argument, it _has to_ perform the naive algorithm which is O(n²) at least, if not worse.

again, `matchTemplate` can execute on GPUs (and so can almost everything else in OpenCV, including convolution).

in any case, more compute power (also memory _bandwidth_) would help of course.
