# Want to speed up template matching in OpenCV python

**URL:** <https://forum.opencv.org/t/want-to-speed-up-template-matching-in-opencv-python/556>\
**Category:** Python\
**Created:** [December 27, 2020, 8:03pm UTC](https://forum.opencv.org/t/want-to-speed-up-template-matching-in-opencv-python/556 "2020-12-27T20:03:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sharp\_Shooter](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/sharp_shooter/32/365_2.png) [@Sharp\_Shooter](https://forum.opencv.org/u/Sharp_Shooter)\
**Post date:** [December 27, 2020, 8:03pm UTC](https://forum.opencv.org/t/want-to-speed-up-template-matching-in-opencv-python/556/1 "2020-12-27T20:03:56Z")

</div>

I am working with the code below to detect multiple images on the screen using multiple source images to detect different object on screen. found this [link](https://stackoverflow.com/questions/43339287/template-matching-with-multiple-source-images-in-opencv-and-python/43341663) with solution but when applied my fps drops down drastically

```auto
import cv2 as cv
import numpy as np
import os
from time import time
import mss

os.chdir(os.path.dirname(os.path.abspath( __file__ )))

imgs = ['car.png', 'bear.png', 'computer.png', 'log.png', 'utensils.png',
        'bench.png', 'river.png', 'keepdistance.png', 'waste.png', 'player.png']

def findClickPostion(needle_img_path, haystack_img):

    # haystack_img = cv.imread('/Users/amanrai/Desktop/conquer2020/main.png', cv.IMREAD_UNCHANGED)
    needle_img = cv.imread(needle_img_path, cv.IMREAD_UNCHANGED)

    result = cv.matchTemplate(haystack_img, needle_img, cv.TM_CCOEFF_NORMED)

    min_val, max_val, min_loc, max_loc = cv.minMaxLoc(result)

    threshold = 0.8
    if max_val >= threshold:
        needle_w = needle_img.shape[1]
        needle_h = needle_img.shape[0]
        top_left = max_loc
        bottom_left = (top_left[0] + needle_w, top_left[1] + needle_h)
        print('Found')
        cv.rectangle(haystack_img, top_left, bottom_left, color=(
            0, 255, 0), thickness=2, lineType=cv.LINE_4)
        cv.imshow('result', haystack_img)
        # cv.waitKey()
    else:
        print('Not Found')

loop_time = time()
with mss.mss() as sct:
    monitor = {"top": 450, "left": 0, "width": 1438,
               "height": 450} # "width": 1500, "height": 860
    while (True):

        screenshot = np.array(sct.grab(monitor))
        #screenshot = cv.cvtColor(screenshot, cv.COLOR_RGB2BGR)

        #cv.imshow('Computer Vision', screenshot)
        for ik in imgs:
            findClickPostion(ik, screenshot)

            print('FPS {}'.format(1 / (time() - loop_time)))
            loop_time = time()

        if cv.waitKey(1) == ord('q'):
            cv.destroyAllWindows()
            break

print('done')

```

Is there a faster way to achieve it as speed up the process so that it detects multiple objects on screen without losing any fps.

---

<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:** [December 27, 2020, 10:29pm UTC](https://forum.opencv.org/t/want-to-speed-up-template-matching-in-opencv-python/556/2 "2020-12-27T22:29:55Z")

</div>

related [Want to speed up template matching in OpenCV python - Stack Overflow](https://stackoverflow.com/questions/65464727/want-to-speed-up-template-matching-in-opencv-python)

as noted over there, your mistake is calling imread in every iteration. move it outside of the loop.
