I'm trying to use the match_template method from the scikit-image library to check if a template exists inside an image and get its X and Y positions. I'm using the scikit-image template matching example.
My code looks like this:
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import numpy as np
from skimage import io
from skimage.color import rgb2gray
from skimage.feature import match_template
def exists(image, template):
"""Perform a template match and returns the X and Y positions.
Args:
image (str): path to the full image.
template (str): path to the template image.
Returns:
If there is a match, return the X and Y positions.
If there is not match, return None.
"""
image = io.imread(image, as_gray=True)
template = io.imread(template, as_gray=True)
result = match_template(image, template, pad_input=True)
return np.unravel_index(np.argmax(result), result.shape)[::-1]
# unreachable
return None
It is performing the template match correctly when it exists in the image, but when the template doesn't exist it gives me wrong X and Y positions.
How can I check if the template doesn't exist and return None
in this case?