用IOC过滤重叠框

通过tensorflow的物体检测detection object识别图片里的object会检测到不同类型及评分的box,不同类别的box之间可能会出现重叠,为了使结果更加的准确需要对重叠box进行过滤

过滤前过滤后

方案:

一个object里不会包含其它的object,这个具体因需求而异。

1、过滤到评分低的框

2、判断两个框是否存在交集

3、存在包含关系则淘汰掉评分低的

4、交并比大于50%则淘汰掉评分低的

5、交并比小于50%,但是大于某一相交面积的50%则移除那个相交面积

代码:

#执行去重
    def doRemoveOverlap(self,box1,box2,box1_score,box2_score):
        #boxesItem.append(item['ymin'],item['xmin'],item['ymax'],item['xmax'])
        box1_xmin = box1[1]
        box1_ymin = box1[0]
        box1_xmax = box1[3]
        box1_ymax = box1[2]
        box2_xmin = box2[1]
        box2_ymin = box2[0]
        box2_xmax = box2[3]
        box2_ymax = box2[2]
        # 1、判断两个框是否存在交集,不存在则返回
        if (box2_xmin >= box1_xmax or box2_xmax <= box1_xmin):
            return 0;
        if (box2_ymin >= box1_ymax or box2_ymax <= box1_ymin):
            return 0;
        #2、存在包含关系则淘汰掉评分低的
        if((box2_xmax <= box1_xmax and box2_xmin >= box1_xmin) and (box2_ymax <= box1_ymax and box2_ymin >= box1_ymin)):
            if(box1_score>=box2_score):
                return 2
            else:
                return 1
        if ((box1_xmax <= box2_xmax and box1_xmin >= box2_xmin) and (
                box1_ymax <= box2_ymax and box1_ymin >= box2_ymin)):
            if (box2_score >= box1_score):
                return 1
            else:
                return 2
        #3、交并比大于50%则淘汰掉评分低的
        area_bagbox = (box1_xmax - box1_xmin) * (box1_ymax - box1_ymin)
        area_bottle = (box2_xmax - box2_xmin) * (box2_ymax - box2_ymin)
        xx1 = max(box1_xmin, box2_xmin)
        yy1 = max(box1_ymin, box2_ymin)
        xx2 = min(box1_xmax, box2_xmax)
        yy2 = min(box1_ymax, box2_ymax)
        area_intersection = max(0, xx2 - xx1) * max(0, yy2 - yy1)
        iou=area_intersection/(area_bagbox+area_bottle-area_intersection)
        if(iou>0.5):
            if (box1_score >= box2_score):
                return 2
            else:
                return 1
        elif(area_intersection>area_bagbox*0.5):#交并比小于50%,但是大于某一相交面积的50%则移除那个相交面积
            return  1
        elif(area_intersection>area_bottle*0.5):
            return 2
        return 0;

    #对照片去重叠,返回保留下来的
    def doRemoveOverlapPhoto(self,boxes,scores,min_score):
        for index1,box1 in enumerate(boxes):
            score1 = scores[index1]
            if(score1<min_score):
                boxes[index1] = None
                continue
            if(box1!=None and score1>=min_score):
                for index2,box2 in enumerate(boxes):
                    if(box2!=None):
                        if(index1!=index2):
                            #评分小于最小评分的直接移除
                            score2=scores[index2]
                            if(score2<min_score):
                                boxes[index2] = None
                                continue
                            result=self.doRemoveOverlap(box1,box2,scores[index1],scores[index2])
                            #将要移除的项设置为None,0不移除,1移除index1,2移除index2
                            if(result==1):
                                boxes[index1]=None
                            elif(result==2):
                                boxes[index2] = None
        # filter(None,boxes)#过滤掉None
        remainOverlapList=[];#剩下的box
        for item in boxes:
            if(item!=None):
                remainOverlapList.append(item)
        return remainOverlapList;

IOU:

交并比,两个框相交的面积/并积

参考:https://blog.csdn.net/lanchunhui/article/details/71190055

 

posted on 2018-04-20 07:57  村_长  阅读(328)  评论(0编辑  收藏  举报

导航