文章目录
多对象模板匹配
import cv2
import numpy as np
from matplotlib import pyplot as plt
src = cv2.imread(r'F:\OPENCV\Opencv\mario.jfif', cv2.IMREAD_COLOR)
img = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
template = cv2.imread(r'F:\OPENCV\Opencv\mario1.jfif', cv2.IMREAD_GRAYSCALE)
h, w = template.shape[:]
res = cv2.matchTemplate(img, template, cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where(res >= threshold)
for pt in zip(*loc[::-1]):
cv2.rectangle(src, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), 2)
cv2.namedWindow('dst', cv2.WINDOW_NORMAL)
cv2.imshow('dst', src)
cv2.waitKey()
cv2.destroyAllWindows()
结果图片
原始图像