# TemplateMatch - Detecting Valorant (game) kills from Player

**URL:** <https://forum.opencv.org/t/templatematch-detecting-valorant-game-kills-from-player/9821>\
**Category:** Python\
**Tags:** matchtemplate\
**Created:** [August 12, 2022, 1:18am UTC](https://forum.opencv.org/t/templatematch-detecting-valorant-game-kills-from-player/9821 "2022-08-12T01:18:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![caio1985](https://avatars.discourse-cdn.com/v4/letter/c/3be4f8/32.png) [@caio1985](https://forum.opencv.org/u/caio1985)\
**Post date:** [August 12, 2022, 1:18am UTC](https://forum.opencv.org/t/templatematch-detecting-valorant-game-kills-from-player/9821/1 "2022-08-12T01:18:39Z")

</div>

Hi,

I’m new to OpenCV and Python but I managed to put together a piece of code that is supposed to detect a template (PNG image with transparency) in video frames of a captured video stream.

I’m using this picture as the template - I captured it from a video frame.

As I know that the kill in the game shows up on the top right corner, I’m not processing the full dimensions of each frame to make processing faster:

For instance, in this frame is when the kill happens and the template should’ve matched:

 ![Capture4](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/5/50cf93d4ab871e34fc10558060ec085b8ad78ad2.png)

My current code is as following:

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

template = cv.imread('kill_frame5.png')

gray_template = cv.cvtColor(template, cv.COLOR_BGR2GRAY)

w, h = template.shape[:-1]

ret, mask = cv.threshold(gray_template, 200, 255, cv.THRESH_BINARY)

mask_inv = cv.bitwise_not(mask)
mask_inv = cv.cvtColor(mask_inv, cv.COLOR_GRAY2RGB)

cv.imshow('template', template)
cv.imshow('mask_inv', mask_inv)

# All the 6 methods for comparison in a list
'''
 methods = ['cv.TM_CCOEFF', 'cv.TM_CCOEFF_NORMED', 'cv.TM_CCORR',
            'cv.TM_CCORR_NORMED', 'cv.TM_SQDIFF', 'cv.TM_SQDIFF_NORMED']
'''

methods = ['cv.TM_CCORR_NORMED']

cap = cv.VideoCapture('video.mp4')

threshold = 0.8

while cap.isOpened():
    ret, frame = cap.read()

    cropped_frame = frame[:200, frame.shape[1] - 500:]

    # If frame is read correctly ret is True
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break

    for meth in methods:
        method = eval(meth)
        res = cv.matchTemplate(cropped_frame, template, cv.TM_CCOEFF_NORMED, None, mask=mask_inv)

        loc = np.where( res >= threshold)
        for pt in zip(*loc[::-1]):
            cv.rectangle(cropped_frame, pt, (pt[0] + w, pt[1] + h), (0,255,0), 2)

    cv.imshow('frame', cropped_frame)

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

cap.release()
cv.destroyAllWindows()

```

As I can’t attach all the assets as I’m new to the forum, I attached them [here](https://drive.google.com/drive/folders/1VYnj-28_Qil_z2QhDVyUWZOtnvHevVol?usp=sharing).

I’m sorry if this is a silly question, any help would be appreciated.

Thanks in advance!  
Caio

---

<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:** [August 12, 2022, 5:52am UTC](https://forum.opencv.org/t/templatematch-detecting-valorant-game-kills-from-player/9821/2 "2022-08-12T05:52:55Z")

</div>

it’s hard to tell from your screenshot of a screenshot, but do you use a full-resolution template and a downscaled “haystack”? then **that** is the error. `matchTemplate` **does not** do any _multiscale_ detection.

my latest writeup and some more:

- [node.js - deno template matching using OpenCV gives no results - Stack Overflow](https://stackoverflow.com/questions/73177065/deno-template-matching-using-opencv-gives-no-results)
- [Detecting zone with some color and text with openCV in python - Stack Overflow](https://stackoverflow.com/questions/72330691/detecting-zone-with-some-color-and-text-with-opencv-in-python/72337308#72337308)
- [How to find how much the arrow is rotated? - #4 by crackwitz](https://forum.opencv.org/t/how-to-find-how-much-the-arrow-is-rotated/3578/4)
- [matchTemplate and old games graphics - #11 by crackwitz](https://forum.opencv.org/t/matchtemplate-and-old-games-graphics/3486/11)

use `TM_SQDIFF`, never `TM_CCORR*`.

all these useless youtube tutorials show the same bad code and these youtubers never understand what it does, so they can’t _explain_ what it does, or why it sucks.

---

<div class="post-metadata">

**Author:** ![caio1985](https://avatars.discourse-cdn.com/v4/letter/c/3be4f8/32.png) [@caio1985](https://forum.opencv.org/u/caio1985)\
**Post date:** [August 12, 2022, 3:06pm UTC](https://forum.opencv.org/t/templatematch-detecting-valorant-game-kills-from-player/9821/3 "2022-08-12T15:06:55Z")

</div>

Thanks! I will make some tests later today after reading your links and see what can be improved. Probably it is a scale problem as you described.

---

<div class="post-metadata">

**Author:** ![caio1985](https://avatars.discourse-cdn.com/v4/letter/c/3be4f8/32.png) [@caio1985](https://forum.opencv.org/u/caio1985)\
**Post date:** [August 14, 2022, 4:56am UTC](https://forum.opencv.org/t/templatematch-detecting-valorant-game-kills-from-player/9821/4 "2022-08-14T04:56:02Z")

</div>

> [@crackwitz](#):
>
> matchTemplate and old games graphics - #11 by crackwitz

This link helped me solving the problem. Thank you!

- [matchTemplate and old games graphics - #11 by crackwitz](https://forum.opencv.org/t/matchtemplate-and-old-games-graphics/3486/11)
