opencv模板匹配查找图像(python)

---

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import cv2
import numpy as np
from cv2 import COLOR_BGR2GRAY
   
def main():
    # 读取原图
    img_rgb = cv2.imread("d:/img-src.png")
    # 转为灰度图
    img_gray = cv2.cvtColor(img_rgb, COLOR_BGR2GRAY)
    # 读取模版图
    template = cv2.imread("d:/img-tmp.png", 0)
    # 获取模版图宽高
    w, h = template.shape[::-1]
    # 模板匹配
    res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
    # threshold 和 res_ts 用于阈值设定匹配
    #threshold = 0.9
    #res_ts = np.where(res >= threshold)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
    top_left = max_loc
    bottom_right = (top_left[0] + w, top_left[1] + h)
    # 画方框,[0,0,255] 颜色,2 线宽
    cv2.rectangle(img_rgb, top_left, bottom_right, [0,0,255], 2)
    cv2.imwrite("d:/res.png",img_rgb)
    img_out = cv2.imread("d:/res.png")
    cv2.imshow("123", img_out)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
if __name__ == "__main__":
    main()

原文:https://www.cnblogs.com/1994july/p/12078762.html

--

原文地址:https://www.cnblogs.com/Ph-one/p/13280803.html