# Can't make cv.matchTemplate efficient

**URL:** <https://forum.opencv.org/t/cant-make-cv-matchtemplate-efficient/7048>\
**Category:** Python\
**Created:** [January 10, 2022, 12:20am UTC](https://forum.opencv.org/t/cant-make-cv-matchtemplate-efficient/7048 "2022-01-10T00:20:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Newb](https://avatars.discourse-cdn.com/v4/letter/n/2acd7d/32.png) [@Newb](https://forum.opencv.org/u/Newb)\
**Post date:** [January 10, 2022, 12:20am UTC](https://forum.opencv.org/t/cant-make-cv-matchtemplate-efficient/7048/1 "2022-01-10T00:20:17Z")

</div>

I followed a tutorial on how to use cv.matchTemplate and the guy had a 98% match while i got a 51% which was in a completely different part of the picture. When he showed his result picture(comparison of his two pictures) he had a very bright spot while i didn’t (even though i took a screenshot of my screenshot).  
First, Is there a way to get a higher match value?  
And second, instead of selecting the brightest white pixel as a result of cv.matchTemplate, is it possible to get a match by selecting the most amount of white pixels within a certain surface area?

Keep in mind i’m not an computer engineer, i just started coding

```auto
import cv2 as cv
import numpy as np

haystack = cv.imread('gamepictures/haystack.png', cv.IMREAD_REDUCED_COLOR_2)
grayhaystack = cv.cvtColor(haystack, cv.COLOR_BGR2GRAY)

needle = cv.imread('gamepictures/needle.png', cv.IMREAD_REDUCED_COLOR_2)
grayneedle =cv.cvtColor(needle, cv.COLOR_BGR2GRAY)
#cv.imshow('Haystack', haystack)
result = cv.matchTemplate(haystack, grayneedle, cv.TM_CCOEFF_NORMED)

#1to10 min white , max white, location
min_val, max_val, min_loc, max_loc = cv.minMaxLoc(result)

print('Best match top left position: %s' % str(max_loc))
print("Best match confidence: %s" % max_val)

needle_w = needle.shape[1]
needle_h = needle.shape[0]
top_left = max_loc

bottom_right = (top_left[0] + needle_w, top_left[1] + needle_h)

cv.rectangle(haystack, top_left, bottom_right, (0, 255,0), 2, cv.LINE_4)

cv.imshow('Haystack', haystack)

cv.imshow('Result', result)

cv.waitKey()
```

---

<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:** [January 10, 2022, 12:45am UTC](https://forum.opencv.org/t/cant-make-cv-matchtemplate-efficient/7048/2 "2022-01-10T00:45:50Z")

</div>

same rules as everywhere else: post data and code, a “minimal reproducible example”. can’t work with abstractions and hypotheticals.
