NMS算法(NonMaximumSuppression)

  • NMS算法(NonMaximumSuppression
def nms(boxes,nms_threshold=0.6):
    l = len(boxes)
    if l==0:
        return []
    else:
        b_x = boxes[:,0]
        b_y = boxes[:,1]
        b_w = boxes[:,2]
        b_h = boxes[:,3]
        scores = boxes[:,4]
        areas = (b_w+1)*(b_h+1)
        order = scores.argsort()[::-1]
        keep = list()
        while order.size>0:
            i = order[0]
            keep.append(i)
            xx1 = np.maximum(b_x[i], b_x[order[1:]])
            yy1 = np.maximum(b_y[i], b_y[order[1:]])
            xx2 = np.minimum(b_x[i] + b_w[i], b_x[order[1:]] + b_w[order[1:]])
            yy2 = np.minimum(b_y[i] + b_h[i], b_y[order[1:]] + b_h[order[1:]])

            #相交面积,不重叠面积为0
            w = np.maximum(0.0,xx2-xx1+1)
            h = np.maximum(0.0,yy2-yy1+1)
            inter = w*h
            #相交面积,面积1+年纪2-相交
            union = areas[i]+areas[order[1:]]-inter
            #计算iou
            Iou = inter/union
            #保留IOu小于阈值的box
            inds = np.where(Iou<=nms_threshold)[0]
            order = order[inds+1] #

        final_boxes = [boxes[i] for i in keep]
        return final_boxes

你可能感兴趣的:(目标检测,每日一个脚本强壮自己)