2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > 深度学习(目标跟踪和目标检测)--边界框bbox坐标转换(任意格式【list numpy tensor】

深度学习(目标跟踪和目标检测)--边界框bbox坐标转换(任意格式【list numpy tensor】

时间:2024-07-09 17:42:18

相关推荐

深度学习(目标跟踪和目标检测)--边界框bbox坐标转换(任意格式【list numpy tensor】

作者提示:可能存在错误,在我的电脑上可以运行;

写程序过程中发现不同的人写的边界框转换程序不一样,

有的只能转换numpy矩阵,

有的只能是转换tensor矩阵,

我就尝试着写了一个可以转换任何维度任意格式的bbox函数。

水平不够,写的时候用的时间长了,脑袋就有些晕乎乎的,就发出来希望大家一起发现其中的错误,也方便大家使用;

如果朋友们发现程序有问题,希望可以及时指出,我会立马做出修改,共同进步。

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

import numpy as npimport 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.stack([left + w / 2, top + h / 2, w, h], axis=-1)return _bboxif 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 _bboxdef 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 _bboxif 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 _bboxdef 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 _bboxif 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 _bboxdef 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 _bboxif 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 _bboxdef 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 _bboxif 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 _bboxdef 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,hreturn _bboxif 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 _bboxif __name__ == '__main__':print('Start...')box1 = [50, 50, 100, 200] # listbox2 = 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_)

深度学习(目标跟踪和目标检测)--边界框bbox坐标转换(任意格式【list numpy tensor】 任意维度【向量 一维矩阵 二维矩阵】)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。