Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

match_template didn't match the correct region #6039

Closed
felixgao opened this issue Nov 15, 2021 · 1 comment
Closed

match_template didn't match the correct region #6039

felixgao opened this issue Nov 15, 2021 · 1 comment
Labels
🫂 Support User help and QA

Comments

@felixgao
Copy link

Description

Way to reproduce

import cv2
import numpy as np
from PIL import Image, ImageFile, ImageOps
from skimage.feature import match_template
from skimage.color import rgb2gray

from pathlib import Path


def skimage_template_matching(template, img_gray, draw_match=True):
    w, h = template.shape[::-1]
    result = match_template(img_gray, template, constant_values=img_gray.mean())
    ij = np.unravel_index(np.argmax(result), result.shape)
    x, y = ij[::-1]
    top_left = (x, y)
    bottom_right = (x+w, y+h)
    if draw_match:
        cv2.rectangle(img_gray, top_left, bottom_right, (0,0,255), 2)
    return top_left, bottom_right, img_gray


if __name__ == '__main__':
    im = Image.open(Path(W-2.png"))
    im = ImageOps.grayscale(im)
    template = np.array(im)
    
    img_file = Image.open(Path("w2 part.jpeg"))
    img_file = ImageOps.grayscale(img_file)
    gray_image = np.array(img_file)
    
    top_left, bottom_right, overlaid = skimage_template_matching(template, gray_image)
    
    print(top_left)
    print(bottom_right)
    
    cv2.imwrite("overlaid.jpeg", overlaid)

the images for the template, image, and overlaid is

Template
W2
overlaid

Version information

# Paste the output of the following python commands
from __future__ import print_function
import sys; print(sys.version)
import platform; print(platform.platform())
import skimage; print(f'scikit-image version: {skimage.__version__}')
import numpy; print(f'numpy version: {numpy.__version__}')
3.8.7 (default, Apr 28 2021, 16:07:46)
[Clang 12.0.0 (clang-1200.0.32.29)]
macOS-10.16-x86_64-i386-64bit
scikit-image version: 0.18.3
numpy version: 1.21.1

I am expecting the region to be at least closer to the button left corner of the original image rather than the middle of the image.

@rfezzani
Copy link
Member

@felixgao, Thank you for your interest in skimage. match_template is not scale invariant! I mean that the template and target image must be at same scale.
It seams in your example that your template resolution is twice your target image image resolution. After rescaling the template I am able to match the correct location:

import numpy as np
import imageio
from skimage import color
from skimage.feature import match_template
from skimage.transform import rescale
from matplotlib import pyplot as plt
from matplotlib.patches import Rectangle

img = color.rgb2gray(imageio.imread("/tmp/target.jpeg"))
template = rescale(
    color.rgb2gray(
        color.rgba2rgb(imageio.imread("/tmp/template.png"))), 0.5)

nl, nc = template.shape

result = match_template(img, template)

i, j = np.unravel_index(np.argmax(result), result.shape)

print(i, j)

_, (ax0, ax1) = plt.subplots(1, 2)

ax0.imshow(result)
ax1.imshow(img, cmap="gray")
ax1.add_artist(Rectangle((j, i), nc, nl, ec="red", fc="None", lw=2))

plt.show()

And obtained
Figure_1

If you don't mind, I will convert this issue to Discussion 😉.

@rfezzani rfezzani added the 🫂 Support User help and QA label Nov 16, 2021
@scikit-image scikit-image locked and limited conversation to collaborators Nov 16, 2021
@scikit-image scikit-image unlocked this conversation Apr 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🫂 Support User help and QA
Projects
None yet
Development

No branches or pull requests

2 participants