模拟量差分和单端(iou计算方法)

一.计算IOU人工修正后的每一个框与算法输出的所有框去计算IOU,取出IOU大于0.9的算法输出框defcompute_IOU(algrim_bboxs,fix_bboxs):#print(‘algrim_bboxs:’,algrim_bboxs)#print(‘fix_bboxs:’,fix_bboxs)fori,fix_bboxinen…

大家好,又见面了,我是你们的朋友全栈君。

一.计算IOU

def intersect(box_a, box_b):
    max_xy = np.minimum(box_a[:, 2:], box_b[2:])
    min_xy = np.maximum(box_a[:, :2], box_b[:2])
    inter = np.clip((max_xy - min_xy), a_min=0, a_max=np.inf)
    return inter[:, 0] * inter[:, 1]


def jaccard_numpy(box_a, box_b):
    """Compute the jaccard overlap of two sets of boxes.  The jaccard overlap
    is simply the intersection over union of two boxes.
    E.g.:
    Args:
        box_a: Multiple bounding boxes, Shape: [num_boxes,4]
        box_b: Single bounding box, Shape: [4]
    Return:
        jaccard overlap: Shape: [box_a.shape[0], box_a.shape[1]]
    """
    inter = intersect(box_a, box_b)
    area_a = ((box_a[:, 2]-box_a[:, 0]) *
              (box_a[:, 3]-box_a[:, 1]))  # [A,B]
    area_b = ((box_b[2]-box_b[0]) *
              (box_b[3]-box_b[1]))  # [A,B]
    union = area_a + area_b - inter
    return inter / union  # [A,B]

人工修正后的每一个框与算法输出的所有框去计算IOU,取出IOU大于0.9的算法输出框

def compute_IOU(algrim_bboxs,fix_bboxs):
    # print('algrim_bboxs:', algrim_bboxs)
    # print('fix_bboxs:', fix_bboxs)
    for i, fix_bbox in enumerate(fix_bboxs):
        print('fix_bbox:', fix_bbox)
        fix_x1, fix_y1, fix_x2, fix_y2 = fix_bbox
        x1, y1, x2, y2 = algrim_bboxs[:,0], algrim_bboxs[:,1], algrim_bboxs[:,-2], algrim_bboxs[:,-1]
        area = (y2-y1)*(x2-x1)
        inter = np.maximum((np.minimum(x2,fix_x2) - np.maximum(x1,fix_x1)),0)*np.maximum((np.minimum(y2,fix_y2) - np.maximum(y1, fix_y1)),0)
        union = area+(fix_y2-fix_y1)*(fix_x2-fix_x1)-inter
        IOU = inter/union
        # print('IOU:', IOU)
        index = np.where(IOU > 0.9)[0]
        print('algrim_bboxs[index]:', algrim_bboxs[index])

 

注释:下面的代码假设了第0行是IOU最大的,那么其余做NMS计算

模拟量差分和单端(iou计算方法)

boxes=np.array([[1,2,3,4],
              [5,6,7,8],
              [9,10,11,12]])
y1 = boxes[:, 0]
x1 = boxes[:, 1]
y2 = boxes[:, 2]
x2 = boxes[:, 3]
area = (y2 - y1) * (x2 - x1)
print('area=',area)
box_area=area[0]
print('box_area=',box_area)
boxes_area=area[1:]
print('boxes_area=',boxes_area)
box=boxes[0]
y1 = np.maximum(box[0], boxes[1:, 0])
y2 = np.minimum(box[2], boxes[1:, 2])
x1 = np.maximum(box[1], boxes[1:, 1])
x2 = np.minimum(box[3], boxes[1:, 3])
#注意与0判断 若不想交返回0值,即intersection为0
intersection = np.maximum(x2 - x1, 0) * np.maximum(y2 - y1, 0)
print('intersection=',intersection)
union = box_area + boxes_area - intersection[:]
print('union=',union)
iou = intersection / union
print('iou=',iou)

模拟量差分和单端(iou计算方法)

二,一般加上score,这样用

scores = np.array([0.5, 0.7, 0.3, 0.2])
ixs=scores.argsort()[::-1]
print('ixs=',ixs)
boxes=np.array([[1,2,3,4],
                [1.2, 0.4,2.1, 0.8],
                [5,6,7,8],
                [9,10,11,12]])
y1 = boxes[:, 0]
x1 = boxes[:, 1]
y2 = boxes[:, 2]
x2 = boxes[:, 3]
area = (y2 - y1) * (x2 - x1)
print('area=',area)
box_area=area[ixs[0]]
print('box_area=',box_area)
boxes_area=area[ixs[1:]]
print('boxes_area=',boxes_area)
box=boxes[ixs[0]]
boxes=boxes[ixs[1:]]
y1 = np.maximum(box[0], boxes[:, 0])
y2 = np.minimum(box[2], boxes[:, 2])
x1 = np.maximum(box[1], boxes[:, 1])
x2 = np.minimum(box[3], boxes[:, 3])
#注意与0判断 若不想交返回0值,即intersection为0
intersection = np.maximum(x2 - x1, 0) * np.maximum(y2 - y1, 0)
print('intersection=',intersection)
union = box_area + boxes_area - intersection[:]
print('union=',union)
iou = intersection / union
print('iou=',iou)

模拟量差分和单端(iou计算方法)

三,求其NMS后的框的小trick

scores=np.array([0.8,0.4,0.7,0.2,0.5])
ixs = scores.argsort()[::-1]
print('ixs=',ixs)
#iou要比score少一个数值,因为iou的计算是由score的第一个值确定的,故下面的np.where要加1
iou=np.array([0.3,0.6,0.1,0.4])
threshold=0.3
remove_ixs = np.where(iou > threshold)[0]+1
print('remove_ixs=',remove_ixs)
# Remove indices of the picked and overlapped boxes.
ixs = np.delete(ixs, remove_ixs)
print('ixs=',ixs)
ixs = np.delete(ixs, 0)
print('ixs=',ixs)

模拟量差分和单端(iou计算方法)

四,加入while,就把不要的框删掉

scores = np.array([0.8, 0.4, 0.7, 0.2, 0.5])
ixs = scores.argsort()[::-1]
while len(ixs) > 0:
    print('================================')
    print('ixs=', ixs)
    # iou要比score少一个数值,因为iou的计算是由score的第一个值确定的,故下面的np.where要加1
    iou = np.array([0.3, 0.6, 0.1, 0.4])
    threshold = 0.3
    remove_ixs = np.where(iou > threshold)[0] + 1
    print('remove_ixs=', remove_ixs)
    # Remove indices of the picked and overlapped boxes.
    ixs = np.delete(ixs, remove_ixs)
    print('ixs=', ixs)
    ixs = np.delete(ixs, 0)
    print('ixs=', ixs)

模拟量差分和单端(iou计算方法)

五,faster rcnn NMS

def py_cpu_nms(dets, thresh):
    """Pure Python NMS baseline."""
    x1 = dets[:, 0]
    y1 = dets[:, 1]
    x2 = dets[:, 2]
    y2 = dets[:, 3]
    scores = dets[:, 4]

    areas = (x2 - x1 + 1) * (y2 - y1 + 1)
    order = scores.argsort()[::-1]
    print('order',order)
    keep = []
    while order.size > 0:
        i = order[0]
        keep.append(i)
        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)

        print('ovr=',ovr)
        print('np.where(ovr <= thresh)',np.where(ovr <= thresh))
        inds = np.where(ovr <= thresh)[0]
        print('inds=',inds)
        print('inds + 1',inds + 1)
        print('order[inds + 1]',order[inds + 1])
        order = order[inds + 1]
        print('order=',order)

    return keep
def nms():
    # ###self nms
    result = [np.array([[7.9301813e+02, 6.3052429e+02, 8.9795898e+02,             
                 7.2513965e+02,9.9307442e-01],
                     [8.0682990e+02, 6.4606281e+02, 8.7198071e+02, 7.1328979e+02,
                      9.5732883e-02]], dtype=np.float32)]
    ##faster rcnn  nms
    keep=py_cpu_nms(result[0],0.7)
    print('keep=',keep)
    for i in keep:
        print('i=',i)
        print(result[0][i])

模拟量差分和单端(iou计算方法)

此处大于0.7IOU的框就抑制, 小于0.7的就留下.

六.多边形利用polygon计算IOU

def compute_IOU_():
    import numpy as np
    import shapely
    from shapely.geometry import Polygon, MultiPoint  # 多边形

    path = './134.jpg'
    img = cv2.imread(path)
    print('img.shape:', img.shape)
    line1 = [728, 252, 908, 215, 934, 312, 752, 355]  # 四边形四个点坐标的一维数组表示,[x,y,x,y....]
    line2 = [741, 262, 907, 228, 923, 308, 758, 342]

    # #debug to show
    # line1 = np.array(line1).reshape(4, 2)
    # line2 = np.array(line2).reshape(4, 2)
    # cv2.polylines(img, [np.array(line1).reshape(-1, 1, 2)], True, (0, 255, 0), thickness=2)
    # cv2.polylines(img, [np.array(line2).reshape(-1, 1, 2)], True, (0, 0, 255), thickness=2)
    # cv2.imwrite('134_with_rect.jpg',img)

    line1_box = np.array(line1).reshape(4, 2)  # 四边形二维坐标表示
    # 凸多边形
    # poly1 = Polygon(line1_box).convex_hull
    #凸多边形与凹多边形
    poly1 = Polygon(line1_box)#.convex_hull  # python四边形对象,会自动计算四个点,最后四个点顺序为:左上 左下  右下 右上 左上
    print(Polygon(line1_box).convex_hull)
    line2_box = np.array(line2).reshape(4, 2)
    # 凸多边形
    # poly2 = Polygon(line2_box).convex_hull
    # 凸多边形与凹多边形
    poly2 = Polygon(line2_box)
    print(Polygon(line2_box).convex_hull)

    union_poly = np.concatenate((line1_box, line2_box))  # 合并两个box坐标,变为8*2
    # print(union_poly)
    print(MultiPoint(union_poly).convex_hull)  # 包含两四边形最小的多边形点
    if not poly1.intersects(poly2):  # 如果两四边形不相交
        iou = 0
    else:
        try:
            inter_area = poly1.intersection(poly2).area  # 相交面积
            print(inter_area)
            union_area = MultiPoint(union_poly).convex_hull.area
            print(union_area)
            if union_area == 0:
                iou = 0
            # iou = float(inter_area) / (union_area-inter_area)  #错了
            iou = float(inter_area) / union_area
        except shapely.geos.TopologicalError:
            print('shapely.geos.TopologicalError occured, iou set to 0')
            iou = 0
    print('line1_box:', line1_box)
    print('iou:', iou)

模拟量差分和单端(iou计算方法)

模拟量差分和单端(iou计算方法)

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/128636.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 【打一局王者荣耀掉星的时间,我制作了一款支持 重力感应 的 3D动态壁纸】

    【打一局王者荣耀掉星的时间,我制作了一款支持 重力感应 的 3D动态壁纸】正在兢兢业业的当一个打工仔,有一个小伙伴问我能不能用Unity制作3D动态壁纸。我一寻思应该问题不大,因为之前用Unity简单制作过一个PC端的桌面宠物,开启Unity背景穿透模式能有一个壁纸的效果。但是仔细一想在手机端也这样做的话好像不能直接套用…所以在网上搜索了一下有没有什么简单可行的方法。然后我发现Unity有一款专门用来做动态壁纸的插件:uLiveWallpaper所以本篇文章就来使用这款插件制作一款最基础的3D重力感应动态壁纸,下面一起看看如何制作吧~

  • 使用mxnet实现卷积神经网络LeNet

    1.LeNet模型LeNet是一个早期用来识别手写数字的卷积神经网络,这个名字来源于LeNet论文的第一作者YannLeCun。LeNet展示了通过梯度下降训练卷积神经网络可以达到手写数字识别在当

    2021年12月30日
  • Mount NTFS Partitions

    Mount NTFS PartitionsMountNTFSPartitions18January2007Windowsusesadifferentfilesystem(NTFS)tostorefiles.InorderforFedoratoreadthatfilesystem,yourequireNTFSsupportinyourkernel.Youcaneither

  • placeholder的样式设置

    placeholder的样式设置在input框中有时想将输入的字和placeholder设为不同的颜色或其它效果,这时就可以用以下代码来对placeholder进行样式设置了。::-webkit-input-placeholder

  • [CNN] 卷积、反卷积、池化、反池化「建议收藏」

    [CNN] 卷积、反卷积、池化、反池化「建议收藏」之前一直太忙,没时间整理,这两天抽出点时间整理一下卷积、反卷积、池化、反池化的内容。一、卷积1、卷积的简单定义卷积神经网络中的卷积操作可以看做是输入和卷积核的内积运算。其运算过程非常容易理解,下面会有详细解释。2、举例解释(1)为了方便直接解释,我们首先以一个通道为例进行讲解,首先明确概念:1)输入是一个5*5的图片,其像素值如下:[11100011100011100110011…

  • 【Web攻防】红队外围信息收集【总结】

    【Web攻防】红队外围信息收集【总结】​外围打点前言由于红队不同于一般的渗透测试,强调更多的是如何搞进去拿到相应机器权限或者实现某特定目的,而不局限于你一定要在什么时间,用什么技术或者必须通过什么途径去搞,相比传统渗透测试,红队则更趋于真实的入侵活动,这种场景其实对防御者的实战对抗经验和技术深度都是比较大的挑战信息收集方式一般采取以下几种方式在搜索引擎(如:baidu、google)进行搜索: 主站相关联的链接,主站链接下可能会放置跳转,如邮件、OA等相关系统。 主站子域名进行搜索,通过二级或三级域名进行目标搜索相.

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号