非极大值抑制(non-maximum suppression)的理解与实现

RCNN 和微软提出的 SPP_net 等著名的目标检测模型,在算法具体的实施过程中,一般都会用到 non-maximum suppress(非最大值抑制,抑制即忽略, 也即忽略那些(IoU)高于提供的阈值的) 的机制。

引入 non-maximum suppression 的目的在于:根据事先提供的 score 向量,以及 regions(由不同的 bounding boxes,矩形窗口左上和右下点的坐标构成) 的坐标信息,从中筛选出置信度较高的 bounding boxes。

其基本操作流程如下:

  • 首先,计算每一个 bounding box 的面积:
    • (x1, y1) ⇒ 左上点的坐标,(x2, y2) ⇒ 右下点的坐标;
    • (x2-x1+1)x(y2-y1+1)
  • 根据 scores 进行排序(一般从小到大),将 score 最大的bounding box置于队列,接下来计算其余 bounding box 与当前 score 最大的 bounding box 的 IoU,抑制(忽略也即去除)IoU大于设定阈值的 bounding box;
  • 重复以上过程,直至候选 bounding boxes 为空;
function picked = nms(boxes, overlap_thresh)
% boxes[:, 1:4] 存储着 regions 信息,boxes[:, 5] 存储的则是 scores 向量

if isempty(boxes)
    picked = [];
    return;
end

x1 = boxes[:, 1];
y1 = boxes[:, 2];           % (x1, y1) ⇒ bounding boxes 左上点坐标的集合
x2 = boxes[:, 3];
y2 = boxes[:, 4];           % (x2, y2) ⇒ bounding boxes 右下点坐标的集合
s = boxes[:, 5];            % scores 向量

areas = (x2-x1+1).*(y2-y1+1);   % 各个 bounding boxes 的面积
[vals, idx] = sort(s);      % 默认从小到大排序

while ~isempty(idx)
    last = length(idx);
    i = idx(last);
    picked = [picked, i];

    xx1 = max(x1(i), x1(1:last-1));
    yy1 = max(y1(i), y1(1:last-1));
    xx2 = min(x2(i), x2(1:last-1));
    yy2 = min(y2(i), y2(1:last-1));

    h = max(0, yy2-yy1+1);
    w = max(0, xx2-xx1+1);

    inter = w .* h;
    iou = inter ./ (areas(i) + areas(1:last-1)-inter);

    I = I(iou <= overlap_thresh);
end

end
  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五道口纳什

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值