I am attempting to template match a cropped template image from the image it was cropped from.
Here's my attempt:
import cv2
import numpy as np
def main()
img_rgb = cv2.imread('whole_image.jpg')
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
template = cv2.imread('cropped_image_from_whole_image.jpg', 0)
res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
threshold = 0.8
for i in res:
for x in i:
# Finally check if x >= threshold (means a match?).
if x >= threshold:
print('Match found!')
if __name__ == '__main__':
main()
Whole image:
Cropped image from whole image:
My overarching goal is to accurately check if a given template image is a image cropped from a larger whole image. If there's a match: print to standard output 'Match found!' (No GUI involved, only command line). Is the issue in how I'm handling the res
/results? What am I doing wrong?