import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
def template_demo():
tpl = cv.imread('./9.png')
target = cv.imread('./8.png')
cv.imshow('templete image', tpl)
cv.imshow('target image', target)
methods = [cv.TM_SQDIFF_NORMED, cv.TM_CCORR_NORMED, cv.TM_CCOEFF_NORMED]
th, tw = tpl.shape[:2]
for md in methods:
print(md)
result = cv.matchTemplate(target, tpl, md)
min_val, max_val, min_col, max_col = cv.minMaxLoc(result)
if md == cv.TM_SQDIFF_NORMED:
tl = min_col
else:
tl = max_col
br = (tl[0] + tw, tl[1] + th)
cv.rectangle(target, tl, br, (0, 0, 255), 2)
# cv.imshow('match-' + np.str(md), target)
cv.imshow('result-' + np.str(md), result)
src = cv.imread('./0.png') # blue, green, red
# cv.namedWindow('input image', cv.WINDOW_AUTOSIZE)
# cv.imshow('input image', src)
template_demo()
cv.waitKey(0)
cv.destroyAllWindows()
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 2621041184@qq.com