pythonopencvmatchtemplate

How to matchtemplate a transparent image opencv


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


Solution

  • 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.