【多目标跟踪笔记十】xywh与xyxy数据的互相转换等函数

# --------------------------------------------------------
# Fast/er R-CNN
# Licensed under The MIT License [see LICENSE for details]
# Written by Ross Girshick
# --------------------------------------------------------

import numpy as np

def unique_boxes(boxes, scale=1.0):
    """Return indices of unique boxes."""

    #将boxes的坐标与缩放比例scale的乘积四舍五入,得到的结果与矩阵V点乘,最后将得到的结果转为整数(即取整数部分)
    v = np.array([1, 1e3, 1e6, 1e9])
    hashes = np.round(boxes * scale).dot(v)
    _, index = np.unique(hashes, return_index=True)
    return np.sort(index)

def xywh_to_xyxy(boxes):
    #将bbox的【x,y,w,h】转换成【x1,y1,x2,y2】
    #某些数据集例如 pascal_voc 的标注方式是采用【x,y,w,h】
    """Convert [x y w h] box format to [x1 y1 x2 y2] format."""
    return np.hstack((boxes[:, 0:2], boxes[:, 0:2] + boxes[:, 2:4] - 1))

def xyxy_to_xywh(boxes):
    #上面函数的逆函数
    """Convert [x1 y1 x2 y2] box format to [x y w h] format."""
    return np.hstack((boxes[:, 0:2], boxes[:, 2:4] - boxes[:, 0:2] + 1))

def validate_boxes(boxes, width=0, height=0):
    """Check that a set of boxes are valid."""
    #检查一个bbox是否合法
    x1 = boxes[:, 0]
    y1 = boxes[:, 1]
    x2 = boxes[:, 2]
    y2 = boxes[:, 3]
    #np.all()检查的是前面的是不是所有元素都满足条件
    #例如x1>=0,如果某一个元素小于0则返回false,反之为true
    assert (x1 >= 0).all()
    assert (y1 >= 0).all()
    assert (x2 >= x1).all()
    assert (y2 >= y1).all()
    assert (x2 < width).all()
    assert (y2 < height).all()

def filter_small_boxes(boxes, min_size):
    #过滤掉尺度过小的bbox
    w = boxes[:, 2] - boxes[:, 0]   #求宽
    h = boxes[:, 3] - boxes[:, 1]   #求高
    keep = np.where((w >= min_size) & (h > min_size))[0]   #保留高和宽都大于min_size的bbox
    return keep

 

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值