ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

深度学习--bbox坐标转换(任意格式【list,numpy,tensor】、任意维度【向量、一维矩阵、二维矩阵】)

2022-01-28 23:03:39  阅读:310  来源: 互联网

标签:... tensor top 矩阵 cx bbox np 任意 left


作者提示:可能存在错误,在我的电脑上可以运行;
写程序过程中发现不同的人写的边界框转换程序不一样,
有的只能转换numpy矩阵,
有的只能是转换tensor矩阵,
我就尝试着写了一个可以转换任何维度任意格式的bbox函数。
水平不够,写的时候用的时间长了,脑袋就有些晕乎乎的,就发出来希望大家一起发现其中的错误,也方便大家使用;
如果朋友们发现程序有问题,希望可以及时指出,我会立马做出修改,共同进步。

本程序目的是:可以转换以下三种格式的输入数据 list,numpy,tensor,维度可以从0维到2维, 也就是shape为:(4,) (3, 4) torch.Size([4]) torch.Size([3, 4])的边界框数据

import numpy as np
import torch


#  ===============================================================================#
#  坐标转换系列函数
#  输入:可能是 列表、np矩阵、tensor矩阵 以下六个函数可以保证输入输出的维度一致
#  输入的维度可能是一个向量shape=(4,)(.T转置之后的到的是原变量)
#  ===============================================================================#
def ltwh2center(bbox):
    """

    :param bbox:[left, top, w, h]
    :return:[cx, cy, w, h]
    """
    if isinstance(bbox, list):
        bbox = np.array(bbox)

    if bbox.shape[-1] != 4:
        raise ValueError('bbox.shape[-1] should equal 4')
    else:
        if isinstance(bbox, np.ndarray):
            left, top, w, h = bbox[..., 0], bbox[..., 1], bbox[..., 2], bbox[..., 3]
            # cx=left+w/2; cy=top+h/2;w;h
            _bbox = np.array([left + w / 2, top + h / 2, w, h])
            _bbox = _bbox.T
            return _bbox

        if isinstance(bbox, torch.Tensor):
            left, top, w, h = bbox[..., 0], bbox[..., 1], bbox[..., 2], bbox[..., 3]
            # cx=left+w/2; cy=top+h/2;w;h
            _bbox = torch.stack((left + w / 2, top + h / 2, w, h), dim=-1)
            return _bbox


def ltwh2corner(bbox):
    """

    :param bbox:[left, top, w, h]
    :return:[left, top, right, bottom]
    """
    if isinstance(bbox, list):
        bbox = np.array(bbox)

    if bbox.shape[-1] != 4:
        raise ValueError('bbox.shape[-1] should equal 4')
    else:
        if isinstance(bbox, np.ndarray):
            left, top, w, h = bbox[..., 0], bbox[..., 1], bbox[..., 2], bbox[..., 3]
            # left; top; right=left+w; bottom=top+h
            _bbox = np.stack([left, top, left + w, top + h], axis=-1)
            return _bbox

        if isinstance(bbox, torch.Tensor):
            left, top, w, h = bbox[..., 0], bbox[..., 1], bbox[..., 2], bbox[..., 3]
            _bbox = torch.stack((left, top, left + w, top + h), dim=-1)
            return _bbox


def corner2ltwh(bbox):
    """

    :param bbox:[left, top, right, bottom]
    :return:[left, top, w, h]
    """
    if isinstance(bbox, list):
        bbox = np.array(bbox)

    if bbox.shape[-1] != 4:
        raise ValueError('bbox.shape[-1] should equal 4')
    else:
        if isinstance(bbox, np.ndarray):
            left, top, right, bottom = bbox[..., 0], bbox[..., 1], bbox[..., 2], bbox[..., 3]
            # left; top; w=right-left; h=bottom-top
            _bbox = np.stack([left, top, right - left, bottom - top], axis=-1)
            return _bbox

        if isinstance(bbox, torch.Tensor):
            left, top, right, bottom = bbox[..., 0], bbox[..., 1], bbox[..., 2], bbox[..., 3]
            _bbox = torch.stack((left, top, right - left, bottom - top), dim=-1)
            return _bbox


def corner2center(bbox):
    """

    :param bbox:[left, top, right, bottom]
    :return:[cx,cy, w, h]
    """
    if isinstance(bbox, list):
        bbox = np.array(bbox)

    if bbox.shape[-1] != 4:
        raise ValueError('bbox.shape[-1] should equal 4')
    else:
        if isinstance(bbox, np.ndarray):
            left, top, right, bottom = bbox[..., 0], bbox[..., 1], bbox[..., 2], bbox[..., 3]
            # cx=(left+right)/2; cy=(top+bottom)/2; w=right-left; h=bottom-top
            _bbox = np.stack([(left + right) / 2, (top + bottom) / 2, right - left, bottom - top], axis=-1)
            return _bbox

        if isinstance(bbox, torch.Tensor):
            left, top, right, bottom = bbox[..., 0], bbox[..., 1], bbox[..., 2], bbox[..., 3]
            _bbox = torch.stack(((left + right) / 2, (top + bottom) / 2, right - left, bottom - top), dim=-1)
            return _bbox


def center2corner(bbox):
    """

    :param bbox: [cx,cy,w,h]
    :return: [left, top, right, bottom]
    """
    if isinstance(bbox, list):
        bbox = np.array(bbox)

    if bbox.shape[-1] != 4:
        raise ValueError('bbox.shape[-1] should equal 4')
    else:
        if isinstance(bbox, np.ndarray):
            cx, cy, w, h = bbox[..., 0], bbox[..., 1], bbox[..., 2], bbox[..., 3]
            # left=cx-w/2; top=cy-h/2; right=cx+w/2; bottom=cy+h/2
            _bbox = np.stack([cx - w / 2, cy - h / 2, cx + w / 2, cy + h / 2], axis=-1)
            return _bbox

        if isinstance(bbox, torch.Tensor):
            cx, cy, w, h = bbox[..., 0], bbox[..., 1], bbox[..., 2], bbox[..., 3]
            _bbox = torch.stack((cx - w / 2, cy - h / 2, cx + w / 2, cy + h / 2), dim=-1)
            return _bbox


def center2ltwh(bbox):
    """

    :param bbox: [cx, cy, w, h]
    :return: [left, top, w, h]
    """
    if isinstance(bbox, list):
        bbox = np.array(bbox)

    if bbox.shape[-1] != 4:
        raise ValueError('bbox.shape[-1] should equal 4')
    else:
        if isinstance(bbox, np.ndarray):
            cx, cy, w, h = bbox[..., 0], bbox[..., 1], bbox[..., 2], bbox[..., 3]
            # left=cx-w/2; top=cy-h/2; w; h
            _bbox = np.stack([cx - w / 2, cy - h / 2, w, h], axis=-1)  # cx,cy,w,h
            return _bbox

        if isinstance(bbox, torch.Tensor):
            cx, cy, w, h = bbox[..., 0], bbox[..., 1], bbox[..., 2], bbox[..., 3]
            _bbox = torch.stack((cx - w / 2, cy - h / 2, w, h), dim=-1)  # 将数据坐标拼接起来
            return _bbox


if __name__ == '__main__':
    print('Start...')
    box1 = [50, 50, 100, 200]  # list
    box2 = np.array([50, 50, 120, 220])  # 一个坐标
    box3 = np.array([[50, 50, 100, 200], [50, 50, 120, 220], [50, 50, 120, 220]])  # 多个坐标
    box4 = torch.FloatTensor([50, 50, 100, 200])  # 一个tensor坐标数据
    box5 = torch.FloatTensor([[50, 50, 100, 200], [50, 50, 120, 220], [50, 50, 120, 220]])  # 多个tensor坐标数据

    for box in [box1, box2, box3, box4, box5]:
        box_ = ltwh2center(box)
        print('\n', 'input (%s):\n' % type(box), box, '\n', 'output(%s):\n' % type(box_), box_)

标签:...,tensor,top,矩阵,cx,bbox,np,任意,left
来源: https://blog.csdn.net/weixin_50727642/article/details/122737638

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有