import numpy as np def NMS(nums, threshold): x1 = nums[:, 0] y1 = nums[:, 1] x2 = nums[:, 2] y2 = nums[:, 3] area = (x2 - x1) * (y2 - y1) score = nums[:, 4] score = np.argsort(score)[::-1] # 函数是将x中的元素从小到大排列,提取其对应的index(索引),然后输出到argsort() keep = [] while score.size > 0: #print("score=",score) keep.append(score[0]) # 计算次大于score的所有框子与最大的score的IOU xx1 = np.maximum(x1[score[0]], x1[score[1:]]) # 注意:这里是一个迭代器 for yy1 = np.maximum(y1[score[0]], y1[score[1:]]) xx2 = np.minimum(x2[score[0]], x2[score[1:]]) yy2 = np.minimum(y2[score[0]], y2[score[1:]]) w = np.maximum(0.0, xx2 - xx1) h = np.maximum(0.0, yy2 - yy1) # IOU overlap = w * h iou = overlap/(area[score[0]] + area[score[1:]] - overlap) # print("iou=",iou) iou_low = np.where(iou <= threshold)[0] # 返回的是所有与最大的score相比的<threshold 的下标 # print("找到重叠度不高于阈值的矩形框索引:",iou_low) score = score[iou_low + 1] return keep
NMS减少框
最新推荐文章于 2024-08-07 18:20:02 发布