匹配多个对象

简介: 匹配多个对象
import cv2
import numpy as np

# 原图
origin = cv2.imread('./image/many-cat.png')
# 原图灰度化
origin_gray= cv2.cvtColor(origin,cv2.COLOR_BGR2GRAY)
# 直接读取模版灰度图
template = cv2.imread('./image/min-cat.png',0)
# 202,167
print(template.shape)
# 1是宽0是高
# w = template.shape[1]
# h = template.shape[0]
h,w = template.shape

# 模版匹配
res = cv2.matchTemplate(origin_gray,template,cv2.TM_CCOEFF_NORMED)
# 设定阈值,认为相关系数大于0.8,就认为是匹配上了
threshold = 0.98
# print(res)
# print(res>=threshold)
# where函数找到位置坐标,是一个元组,一个x轴,一个y轴
# print(np.where(res>=threshold))
# 找到复核条件的坐标,返回x,y的列表,在一起
# print(np.argwhere(res>=threshold))
loc = np.argwhere(res>=threshold)
# print(loc)
for pt in loc:
    bottom_right = (pt[1] + w,pt[0] + h)
    cv2.rectangle(origin,pt[::-1],bottom_right,(0,0,255),2)

# loc = np.where(res >= threshold)
# for pt in zip(*loc[::-1]):
#     bottom_right=(pt[0] + w,pt[1] + h)
#     cv2.rectangle(origin,pt,bottom_right,(0,0,255),2)

cv2.imshow('origin',origin)
cv2.waitKey(0)
cv2.destroyAllWindows()
目录
相关文章
|
4月前
|
JavaScript 前端开发 Java
正则表达式深度解析:匹配任意字符串
【4月更文挑战第1天】
2422 0
|
4月前
2020-10-10 数组和对象的区分方法
2020-10-10 数组和对象的区分方法
|
4月前
|
C++
HRBUST - 1170(判断括号是否匹配)
HRBUST - 1170(判断括号是否匹配)
|
算法 前端开发 JavaScript
【前端算法】判断一个字符串的括号是否成对匹配
使用typescript完成判断一个字符串的括号是否成对匹配的过程
122 0
|
Scala 开发者
对象匹配的应用案例 | 学习笔记
快速学习对象匹配的应用案例
正则匹配指定字符之间的内容,并替换(多个匹配替换)
var str="是吗@Test1:我觉得你说的很对@Test:学无止境"; var r=str.match(/@.*?:/ig); for (var index = 0; index < r.
2411 0
|
数据采集 Python
使用正则表达式替换构造字典
  写爬虫时获取网页的请求头时,拿到的数据往往不是字典类型,我们可以使用 Pycharm 的正则表达式替换功能,替换为字典类型。  
1014 0