直接看效果 大图: 小图:
程序运行效果图: 代码部分:
import cv2
def template_demo():
tpl = cv2.imread("./a1.png")
target = cv2.imread("./a4.png")
cv2.imshow("target image", target)
th, tw = target.shape[:2]
result = cv2.matchTemplate(target, tpl, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
tl = max_loc
br = (tl[0] + tw, tl[1] + th)
cv2.rectangle(tpl, tl, br, (0, 0, 255), 2)
print(tl)
print(br)
cv2.imshow("match-" + str(cv2.TM_CCOEFF_NORMED), tpl)
if __name__ == "__main__":
template_demo()
cv2.waitKey(0)
cv2.destroyAllWindows()
|