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

Posted 1994july

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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()
 来源:莆田SEO

以上是关于opencv模板匹配查找图像(python)的主要内容,如果未能解决你的问题,请参考以下文章

使用Python,OpenCV进行模板匹配单对象多对象及多尺度模板匹配

使用Python+OpenCV进行图像模板匹配(Match Template)

opencv python:模板匹配

Python+OpenCV图像处理—— 模板匹配

Python+OpenCV图像处理—— 模板匹配

Python+OpenCV图像处理之模板匹配