-1

I am trying to match the following image

original

With the following templates, using cv2.matchTemplate

enter image description here enter image description here enter image description here enter image description here

#elementachercher = path to template png file
#screenascanner = path to global picture where to find template


import pyautogui
import time
import cv2
import numpy as np
from matplotlib import pyplot as plt
import os
import imutils

def tryfoundobject(elementachercher, screenascanner):
    img_rgb = cv2.imread(screenascanner)
    img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
    template = cv2.imread(elementachercher,0)
    w, h = template.shape[::-1]
    res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
    threshold = 0.8
    loc = np.where( res >= threshold)
    for pt in zip(*loc[::-1]):
        cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
        if pt != None:
            print(elementachercher+" matched")
            break
        else:
            continue
    cv2.imwrite(elementachercher,img_rgb)
    
  tryfoundobject("/home/noway/template.png", "/home/noway/mypicture.png")  
    

But no matches are found.

What am I doing wrong? 1 screen is what result i want, the second screen is what i got after script

What i want after succefully script

what result i got

Gulzar
  • 23,452
  • 27
  • 113
  • 201

1 Answers1

0

Following the chat discussion

res_normed = (res - np.min(res)) / (np.max(res) - np.min(res))
res_uint = np.array(res_normed * 255, dtype=np.uint8)
cv2.imshow("res_uint ", res_uint)

res looks like this

enter image description here

You have to look for local maxima on this image to get the coordinates of matches.

Refer to this for a very simple POC and to this for the full solution.

Gulzar
  • 23,452
  • 27
  • 113
  • 201