深度学习(目标跟踪和目标检测)--边界框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 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.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】、任意维度【向量、一维矩阵、二维矩阵】)相关推荐
- 机器学习_深度学习毕设题目汇总——目标检测B
下面是该类的一些题目: 题目 典型恶劣天气条件下高铁周界入侵目标检测 图模型融合时空特征的视觉显著性目标检测算法研究 基于SAR图像的舰船目标检测方法研究 基于三维点云分析的智能汽车目标检测方法研究 ...
- 全网唯一一套labview深度学习教程:tensorflow+目标检测:龙哥教你学视觉—LabVIEW深度学习教程
全网唯一一套labview深度学习教程:tensorflow+目标检测:龙哥教你学视觉-LabVIEW深度学习教程 一.知识背景: 随着自动化技术的快速发展,在工业生产中很多需要人工操作的环节逐渐转由 ...
- 【深度学习】深入浅出YOLOv3目标检测算法和实现(图片和视频)
[深度学习]深入浅出YYOLOv3目标检测算法(图片和视频) 文章目录 1 概述 2 一个全卷积神经网络--Darknet-53 3 解释输出 4 代码实现4.1 导入项目4.2 执行脚本4.3 预测 ...
- 深度学习在遥感图像目标检测中的应用综述
深度学习在遥感图像目标检测中的应用综述 1 人工智能发展 1.1 发展历程 1.2 深度学习的应用 2 深度学习 2.1 机器学习概述 2.2 神经网络模型 2.3 深度学习 2.4 深度学习主要模型 ...
- 【深度学习前沿应用】目标检测
[深度学习前沿应用]目标检测 作者简介:在校大学生一枚,华为云享专家,阿里云星级博主,腾云先锋(TDP)成员,云曦智划项目总负责人,全国高等学校计算机教学与产业实践资源建设专家委员会(TIPCC)志愿 ...
- 《动手学深度学习》Task09:目标检测基础+图像风格迁移+图像分类案例1
1 目标检测基础 1.1 目标检测和边界框(9.3) %matplotlib inline from PIL import Imageimport sys sys.path.append('/home ...
- 目标检测YOLO实战应用案例100讲-基于深度学习的自动驾驶目标检测算法研究
目录 基于深度学习的自动驾驶目标检测算法研究 相关理论基础 2.1 卷积神经网络基本原理
- 动手学深度学习之Task09:目标检测基础;图像风格迁移;图像分类案例1
目标检测基础 9.4 锚框 目标检测算法通常会在输入图像中采样大量的区域,然后判断这些区域中是否包含我们感兴趣的目标,并调整区域边缘从而更准确地预测目标的真实边界框(ground-truth boun ...
- 吴恩达深度学习笔记12-Course4-Week3【目标检测】
目标检测(Object detection) 一.目标定位(Object Localization) 图像识别的三个层次: 图像分类:判断图像中是否包含某一类物体,并且假定每张图像只有一个目标. 目标 ...
最新文章
- 制作一个查询信息程序_三步学会制作一个小程序
- 显示乱七八糟图片问题之解决
- Android 去掉Activity的跳转动画
- 谷歌自锤Attention:纯注意力并没那么有用,Transformer组件很重要
- Liunx 重新mount
- Mockito教程--思维导图笔记
- python风控工具_python-风控模型分析01
- linux suse 安装redis,suse 安装redis(示例代码)
- Linux中变量$#,$@,$0,$1,$2,$*,$$,$?的含义
- LCD1602芯片的使用——简单易懂
- 阿里云oss 简单上传
- 游戏中MD5加密的一些作用
- 数据分析从零到精通第一课 数据分析技巧和OLAP工具简介
- 将python图表放入ppt_如何在ppt中嵌入python图表(或图像)并刷新
- python turtle工具绘制四叶草
- python语法简单吗_python基本语法练习实例 python好学吗? 语法简单吗? 举个例子?...
- xm-select使用
- 精品课 - Python 基础
- mysql只比较月日的情况
- mac文件共享连不上服务器,mac服务器文件夹共享权限设置