The image of the code is in the link given with this question. (https://i.stack.imgur.com/zq8V8.png) Even when I pass the wrong image as the 'main' image , the template still matches. And even when I give the main image and the template(that is actually a part of main image ) then also the 'for' loop fails to run because the rectangle never gets drawn on the main image. As for the code , it is available in almost all the links when you google 'template matching in openCV python'
# Python program to illustrate
# template matching
import cv2
import numpy as np
# Read the main image
img_rgb = cv2.imread('test.jpg')
# Convert it to grayscale
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
# Read the template
template = cv2.imread('cropped1.jpg',0)
# Store width and heigth of template in w and h
w, h = template.shape[::-1]
# Perform match operations.
res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
# Specify a threshold
threshold = 0.8
# Store the coordinates of matched area in a numpy array
loc = np.where( res >= threshold)
# Draw a rectangle around the matched region.
for pt in zip(*loc[::-1]):
cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,255,255), 2)
# Show the final image with the matched area.
cv2.imshow('Detected',img_rgb)
cv2.waitKey(0)