cv2.matchTemplate()进行模板匹配
import os
import cv2
import numpy as np
import matplotlib.pyplot as plt
def read_img(img_7_path,img_8_path):
img=cv2.imread(img_7_path,0)
template=cv2.imread(img_8_path,0)
th,tw=template.shape[::]
rv=cv2.matchTemplate(img,template,cv2.TM_SQDIFF)
minVal,maxVal,minLoc,maxLoc=cv2.minMaxLoc(rv)
topleft=minLoc
bottomright=(topleft[0]+tw,topleft[1]+th)
cv2.rectangle(img,topleft,bottomright,255,2)
plt.subplot(121), plt.imshow(rv, cmap='gray')
plt.title('Matching Result'), plt.axis('off')
plt.subplot(122), plt.imshow(img, cmap='gray')
plt.title('Detected Point'), plt.axis('off')
plt.show()
if __name__ == '__main__':
work_path = os.getcwd()
data_path = os.path.join(work_path, 'data')
img_7_path = os.path.join(data_path, '7.jpg')
img_8_path = os.path.join(data_path, '8.jpg')
read_img(img_7_path,img_8_path)