nms 区域融合

def nms(dets,thresh):

    x1 = dets[:, 0]
    y1 = dets[:, 1]
    x2 = dets[:, 2]
    y2 = dets[:, 3]
    org_scores = dets[:, 4]

    areas = (x2 - x1 + 1) * (y2 - y1 + 1)
    scores = org_scores * areas
    order = scores.argsort()[::-1]

    keep = []  
    while order.size > 0:  
        i = order[0]

        xx1 = np.maximum(x1[i], x1[order[1:]])
        yy1 = np.maximum(y1[i], y1[order[1:]])  
        xx2 = np.minimum(x2[i], x2[order[1:]])  
        yy2 = np.minimum(y2[i], y2[order[1:]])

        w = np.maximum(0.0, xx2 - xx1 + 1)  
        h = np.maximum(0.0, yy2 - yy1 + 1)  
        inter = w * h

        ovr = inter / (areas[i] + areas[order[1:]] - inter)
        ovr_inds = np.where(ovr > thresh)[0]
        if ovr_inds.shape[0] > 0:
            ovr_order = order[ovr_inds + 1]
            ovr_boxes = boxes[ovr_order]
            boxes[i,0] = min(np.hstack((boxes[i,0],ovr_boxes[:,0])))
            boxes[i,2] = max(np.hstack((boxes[i,2],ovr_boxes[:,2])))

            miny = np.hstack((boxes[i,1],ovr_boxes[:,1]))
            maxy = np.hstack((boxes[i,3],ovr_boxes[:,3]))

            ovr_scores = np.hstack((boxes[i,4],ovr_boxes[:,4]))
            ovr_scores = ovr_scores / sum(ovr_scores)

            boxes[i,1] = sum(ovr_scores * miny)
            boxes[i,3] = sum(ovr_scores * maxy)
        inds = np.where(ovr <= thresh)[0]

        order = order[inds + 1]
        keep.append(i)
    return keep

猜你喜欢

转载自blog.csdn.net/he_wen_jie/article/details/81206838
NMS
今日推荐