Translate

The code for IOU / NMS

Python Code for IOU (numpy):

def iou_nms(bbox,gt):
    #lt是两个框中间重叠框的最左边和最上边的坐标,
    #rb是两个框中间重叠框的最右边和最下边的坐标
    lt = np.maximum(bbox[:,None,:2],gt[:,:2]) # [N,M,2]
    rb = np.minimum(bbox[:,None,2:4],gt[:,2:4]) # [N,M,2]
    #wh是重叠框的宽和高,+1是因为求边长,边长就等于前后两端的坐标点相减且+1
    wh = np.maximum(rb - lt  + 1 , 0) # [N,M,2]
    #求重叠框的面积
    inter_area = wh[:,:,0] * wh[:,:,1] #[N,M]
    #分别求两个框各自的面积
    bbox_area = (bbox[:,2] - bbox[:,0] + 1) * (bbox[:,3] - bbox[:,1] + 1)  #[N,]
    gt_area = (gt[:,2] - gt[:,0] + 1) * (gt[:,3] - gt[:,1] + 1)  #[M,]
    #iou的公式,重叠框面积 / 两个框面积之和减去重叠框面积
    iou = inter_area / (bbox_area[:,None] + gt_area - inter_area)  #[N,M]
    return iou

input: bbox and gt should be numpy


Python Code for IOU (list):

def iou(boxA, boxB): # determine the (x, y)-coordinates of the intersection rectangle xA = max(boxA[0], boxB[0]) yA = max(boxA[1], boxB[1]) xB = min(boxA[2], boxB[2]) yB = min(boxA[3], boxB[3]) # compute the area of intersection rectangle interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1) # compute the area of both the prediction and ground-truth # rectangles boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1) boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1) # compute the intersection over union by taking the intersection # area and dividing it by the sum of prediction + ground-truth # areas - the interesection area iou = interArea / float(boxAArea + boxBArea - interArea) # return the intersection over union value return iou

input: boxA and boxB should be list (format xyxy)


Python Code for NMS:

def nms(bbox,thresh):
    #得分bbox第五列是得分,前四列是x0,y0,x1,y1
    score = bbox[:,4]
    #对得分进行排序
    order = np.argsort(score)
    #记录结果值,每次保存得分最高的那个框的索引,最后再用bbox[keep]取出相应框
    keep = []
    #一直筛选到没有可用的框
    while order.size > 0:
        #取得分最高的框的索引,因为order是升序,所以最后一位是得分最高的
        index = order[-1]
        #保存得分最高的那个框的索引
        keep.append(index)
        #取出这个框
        x = bbox[index]
        #计算iou,x[None,:]是为了保持shape一致,squeeze(0)是去掉第一个维度,
        #不去掉的话结果的shape是[1,5],再np.where就不对了,必须让其等于[5,]
        sub_bbox_iou = iou_nms(x[None,:],bbox[order[:-1]]).squeeze(0)
        #筛选小于阈值的框,大于阈值的话,就和得分最高的那个框重叠了,所以保留不重叠的框
        index_after = np.where(sub_bbox_iou < thresh)
        #筛选剩下的框
        order = order[index_after]
    return keep

* Input bbox should be numpy

* should be used with function iou_nms

* output is a list of index of the keep bbox

        keep = nms(input_nms, 0.45)
        boxes_after_nms = np.asarray(input_box)[keep]



No comments:

Post a Comment