码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-12-22 00:42:56      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:imshow   span   word   bottom   图像   pen   number   class   模板   

#!/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)

标签:imshow   span   word   bottom   图像   pen   number   class   模板   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!