MNS(非极大值抑制)python实现
发布日期:2022-03-11 10:18:57
浏览次数:28
分类:技术文章
本文共 3218 字,大约阅读时间需要 10 分钟。
理论参考:
源码:
import numpy as np def py_cpu_nms(dets, thresh): # 单独获取各个参数,以下参数shape = (5,) x1 = dets[:,0] y1 = dets[:,1] x2 = dets[:,2] y2 = dets[:,3] scores = dets[:,4] areas = (y2-y1+1) * (x2-x1+1) print("areas.shape: {}".format(areas.shape)) print("areas: {}".format(areas)) keep = [] # 得分按照由高到低排序的索引, index.shape = (6,) index = scores.argsort()[::-1] print("index.shape: {}".format(index.shape)) print("index: {}".format(index)) while index.size >0: # i为得分最高的索引 i = index[0] # 将得分最高的索引追加到列表中 keep.append(i) # 计算两个box左上角点坐标的最大值x11、y11和右下角坐标的最小值x22、y22 # x11、y11、x22、y22 shape = (5,) x11 = np.maximum(x1[i], x1[index[1:]]) y11 = np.maximum(y1[i], y1[index[1:]]) x22 = np.minimum(x2[i], x2[index[1:]]) y22 = np.minimum(y2[i], y2[index[1:]]) print("index[1:]: {}".format(index[1:])) print("x1[index[1:]]: {}".format(x1[index[1:]])) print("x11: {}".format(x11)) print("x11.shape: {}".format(x11.shape)) # 当两个方框相交时,22-11最后得到w,h是正值 # 当两个方框不相交的时候,22-11最后得到w,h是负值,则设置为0 # w、h shape = (5,) w = np.maximum(0, x22-x11+1) h = np.maximum(0, y22-y11+1) print("w: {}".format(w)) print("w.shape: {}".format(w.shape)) # 计算交集面积 # overlaps.shape = (5,) overlaps = w * h print("overlaps: {}".format(overlaps)) print("overlaps.shape: {}".format(overlaps.shape)) # 计算交并比 # ious.shape = (5,) ious = overlaps / (areas[i] + areas[index[1:]] - overlaps) print("ious.shape: {}".format(ious.shape)) print("ious: {}".format(ious)) # 得到满足阈值条件的ious中的索引(ious相比index缺少第一个最大值) ious_idx = np.where(ious<=thresh)[0] print("ious<=thres idx: {}".format(ious_idx)) # ious_idx+1得到在index中的索引 index = index[ious_idx + 1] # because index start from 1 print("index: {}".format(index)) return keep if __name__ == "__main__": boxes=np.array([[100,100,210,210,0.72], # 0 [250,250,420,420,0.8], # 1 [220,220,320,330,0.92], # 2 [100,100,210,210,0.72], # 3 [230,240,325,330,0.81], # 4 [220,230,315,340,0.9]]) # 5 keep = py_cpu_nms(boxes, thresh=0.7) print("keep: {}".format(keep))
输出:
areas.shape: (6,)areas: [ 12321. 29241. 11211. 12321. 8736. 10656.]index.shape: (6,)index: [2 5 4 1 3 0]index[1:]: [5 4 1 3 0]x1[index[1:]]: [ 220. 230. 250. 100. 100.]x11: [ 220. 230. 250. 220. 220.]x11.shape: (5,)w: [ 96. 91. 71. 0. 0.]w.shape: (5,)overlaps: [ 9696. 8281. 5751. 0. 0.]overlaps.shape: (5,)ious.shape: (5,)ious: [ 0.79664777 0.70984056 0.16573009 0. 0. ]ious<=thres idx: [2 3 4]index: [1 3 0]index[1:]: [3 0]x1[index[1:]]: [ 100. 100.]x11: [ 250. 250.]x11.shape: (2,)w: [ 0. 0.]w.shape: (2,)overlaps: [ 0. 0.]overlaps.shape: (2,)ious.shape: (2,)ious: [ 0. 0.]ious<=thres idx: [0 1]index: [3 0]index[1:]: [0]x1[index[1:]]: [ 100.]x11: [ 100.]x11.shape: (1,)w: [ 111.]w.shape: (1,)overlaps: [ 12321.]overlaps.shape: (1,)ious.shape: (1,)ious: [ 1.]ious<=thres idx: []index: []keep: [2, 1, 3]
转载地址:https://blog.csdn.net/weicao1990/article/details/110496636 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
网站不错 人气很旺了 加油
[***.192.178.218]2024年04月16日 22时58分14秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Power oj 2540 (FFT卷积)
2019-04-26
hdu 6165(dfs or bfs or tarjan+topsort)
2019-04-26
hdu 6168(stl)
2019-04-26
hdu 6170(正则表达式)
2019-04-26
排列组合 "n个球放入m个盒子m"问题 总结(转)
2019-04-26
codeforces845C(stl)
2019-04-26
图的几种存储方式(邻接矩阵+邻接表+vector)
2019-04-26
[LeetCode] 67. 二进制求和(简单模拟二进制求和)
2019-04-26
HDU1233(基础最小生成树 prim和 kruskal)
2019-04-26
终于找到可以一文多发的平台了!
2019-04-26
IntelliJ IDEA 2019 快捷键终极大全,速度收藏!
2019-04-26
第3章-5 字符转换 (15分)【python】
2019-04-26
L1-068 调和平均 (10 分)
2019-04-26
L1-069 胎压监测 (15 分)
2019-04-26
L1-071 前世档案 (20 分)
2019-04-26
L1-072 刮刮彩票 (20 分)
2019-04-26
L2-012 关于堆的判断 (25 分)
2019-04-26
L1-064 估值一亿的AI核心代码 (20 分)
2019-04-26
L2-033 简单计算器 (25 分)
2019-04-26
L2-034 口罩发放 (25 分)
2019-04-26