# Python program to illustrate# template matchingimportcv2importnumpyasnp# Read the main imageimg_rgb=cv2.imread('mainimage.jpg').# Convert it to grayscaleimg_gray=cv2.cvtColor(img_rgb,cv2.COLOR_BGR2GRAY)# Read the templatetemplate=cv2.imread('template',0)# Store width and height of template in w and hw,h=template.shape[::-1]# Resize the image according to scale and# keeping track of ratio of resizingresize=imutils.resize(img_gray,width=int(shape[0]),height=int(img_gray.shape[1]*scale)# If resize image is smaller than that of template# break the loop# Detect edges in the resized, grayscale image and apply template# Matching to find the template in image edged# If we have found a new maximum correlation value, update# the found variable if# found = null/maxVal > found][0]ifresized.shape[0]<horresized.shape[1]<w:breakfound=(maxVal,maxLoc,r)# Unpack the found variables and compute(x,y) coordinates# of the bounding box(__,maxLoc,r)=found(startX,startY)=(int(maxLoc[0]*r),intmaxLoc[1]*r)(endX,endY)=(int((maxLoc[0]+tw)*r),int(maxLoc[1]+tH)*r)# Draw a bounding box around the detected result and display the imagecv2.rectangle(image,(startX,startY),(endX,endY),(0,0,255),2)cv2.imshow("Image",image)cv2.waitKey(0)