opencv模板匹配测试

opencv模板匹配

 

import cv2  
  
# 加载标准图像  
template = cv2.imread('template.jpg')  
  
# 预处理输入图像  
gray = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)  
gray = cv2.GaussianBlur(gray, (5, 5), 0)  
  
# 创建背景减除器  
background = cv2.subtract(template, gray)  
  
# 创建分类器  
classifier = cv2.HOGDescriptor()  
  
# 在待检测图像上进行预测  
result = cv2.matchTemplate(gray, template, classifier)  
  
# 对预测结果进行后处理  
  
# 根据分类器的输出结果,确定目标对象的位置和类别,并进行相应的处理  
  
# 显示检测结果  
for i in range(template.shape[0]):  
    for j in range(template.shape[1]):  
        for k in range(3):  
            if result[i, j, k] == 0:  
                center_x, center_y, w, h = cv2.boundingRect(result[i, j, k])  
                cv2.rectangle(img, (center_x, center_y), (center_x + w, center_y + h), (0, 255, 0), 2)  
  
cv2.imshow('Result', img)  
cv2.waitKey(0)  
cv2.destroyAllWindows()

 

 

 

###############

posted @ 2023-05-14 19:26  西北逍遥  阅读(29)  评论(0编辑  收藏  举报