1

I tried the method in the answer of this: How do I find an image on screen ignoring transparent pixels, it is exactly what I'm looking for, but it didn't work out for me, I keep getting a black image after the alpha processing.

I tried

    result = cv2.matchTemplate(Image, Template, cv2.TM_CCOEFF_NORMED)

and

    base = template[:,:,0:3]
    alpha = template[:,:,3]
    alpha = cv2.merge([alpha,alpha,alpha])

    # do masked template matching and save correlation image
    correlation = cv2.matchTemplate(img, base, cv2.TM_CCORR_NORMED, mask=alpha)

Template: 1

Image: 2

The template is bottom left for reference

Eery
  • 13
  • 3
  • Do you this method for matching with another template in the image? – BarzanHayati Mar 30 '22 at 17:15
  • Yes, I need to find multiple templates in the image, the one I posted was a random one of them, I tried the others with no success too – Eery Mar 30 '22 at 17:16
  • Please upload this part of your code and your image and template here to investigate bugs. – BarzanHayati Mar 30 '22 at 17:18
  • I already uploaded everything in my question, the code, the image and the template, I can't upload the image directly yet, but there is the imgur link – Eery Mar 30 '22 at 17:19

1 Answers1

1

Try this one :

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

original = cv.imread('Original_game.png')
img = cv.imread('Original_game.png',0)
img2 = img.copy()
template = cv.imread('template.png',0)

w, h = template.shape[::-1]
# All the 3 methods for comparison in a list
methods = ['cv.TM_CCOEFF', 'cv.TM_CCOEFF_NORMED', 'cv.TM_CCORR_NORMED']#,'cv.TM_CCORR','cv.TM_SQDIFF', 'cv.TM_SQDIFF_NORMED'

for meth in methods:
    img = img2.copy()
    method = eval(meth)

    # Apply template Matching
    res = cv.matchTemplate(img,template,method)
    min_val, max_val, min_loc, max_loc = cv.minMaxLoc(res)

    print(f"meth={meth} , min_val={min_val}, max_val={max_val}, min_loc={min_loc}, max_loc={max_loc}")
    
    # If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
    if method in [cv.TM_SQDIFF, cv.TM_SQDIFF_NORMED]:
        top_left = min_loc
    else:
        top_left = min_loc#max_loc
    bottom_right = (top_left[0] + w, top_left[1] + h)
    cv.rectangle(original,top_left, bottom_right, 255, 2)
    fig = plt.figure(figsize=(10, 7))
    plt.imshow(original)
    plt.show()

Sample Results:

Result

attention to the algorithm:

  1. Change threshold to find different location for matching
  2. Change matching algorithm

Check why sometimes you should use max and sometimes use min value found location matching.

helpful links:

Template Matching

OpenCV Template Matching ( cv2.matchTemplate )

Template matching using OpenCV in Python

Update #1

If you want to reach better results you should use feature descriptors like "HOG", "Surf", "SIFT" and ... . Or state of the art object detection models like YOLO are the best known to your problem.

BarzanHayati
  • 637
  • 2
  • 9
  • 22
  • Awesome that your code managed to get the template, I applied it to another template but it gave vague results every time, example: https://i.imgur.com/TvKsjCl.png, this template is bottom right, couldn't find it – Eery Mar 30 '22 at 19:09
  • Check other methods. You know these methods are not so strong to find any objects. – BarzanHayati Mar 30 '22 at 19:15
  • It's obvious. Because your template background is original image has different color and it's not white. These matching algorithms works with value and correlation. In order to find better results it's better to use "SIFT" "SURF" and other feature descriptors. then try to find nearest object. – BarzanHayati Mar 30 '22 at 19:45