python实现模板匹配

p y t h o n 实 现 模 板 匹 配 python实现模板匹配 python

import cv2
import numpy as np
from matplotlib import pyplot as plt
# 1. 读入原图和模板
img_rgb = cv2.imread('mario.jpg')
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
template = cv2.imread('mario_coin.jpg', 0)
h, w = template.shape[:2]

# 归一化平方差匹配
res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
threshold = 0.8

# 这段代码后面会有解释
loc = np.where(res >= threshold)  # 匹配程度大于80%的坐标y,x
for pt in zip(*loc[::-1]): # *号表示可选参数
    right_bottom = (pt[0] + w, pt[1] + h)
    cv2.rectangle(img_rgb, pt, right_bottom, (0, 0, 255), 2)
    
cv2.imwrite('res.png', img_rgb)

参考文献

https://www.jianshu.com/p/5f4bb5c78184

你可能感兴趣的:(python)