import cv2
import numpy as np
def find_template(template_path, image_path):
# 加载模板和图像,并将它们转换成灰度图像
template = cv2.imread(template_path, 0)
image = cv2.imread(image_path, 0)
# 使用模板匹配算法找到小图在大图中的位置
result = cv2.matchTemplate(image, template, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
print(min_val)
print(max_val)
print(min_loc)
print(max_loc)
# 将识别结果可视化
top_left = max_loc
bottom_right = (top_left[0] + template.shape[1], top_left[1] + template.shape[0])
cv2.rectangle(image, top_left, bottom_right, 255, 2)
# 展示识别结果
cv2.imshow('Result', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 调用函数进行图像识别
find_template('D://a/22.png', 'D://a/11.png')
转载至:python大图中识别小图位置_mob64ca12d32849的技术博客_51CTO博客