python不使用for计算两组、多个矩形两两间的iou方式


Posted in Python onJanuary 18, 2020

解决问题: 不使用for计算两组、多个矩形两两间的iou

使用numpy广播的方法,在python程序中并不建议使用for语句,python中的for语句耗时较多,如果使用numpy广播的思想将会提速不少。

代码:

def calc_iou(bbox1, bbox2):
 if not isinstance(bbox1, np.ndarray):
  bbox1 = np.array(bbox1)
 if not isinstance(bbox2, np.ndarray):
  bbox2 = np.array(bbox2)
 xmin1, ymin1, xmax1, ymax1, = np.split(bbox1, 4, axis=-1)
 xmin2, ymin2, xmax2, ymax2, = np.split(bbox2, 4, axis=-1)
 
 area1 = (xmax1 - xmin1) * (ymax1 - ymin1)
 area2 = (xmax2 - xmin2) * (ymax2 - ymin2)
 
 ymin = np.maximum(ymin1, np.squeeze(ymin2, axis=-1))
 xmin = np.maximum(xmin1, np.squeeze(xmin2, axis=-1))
 ymax = np.minimum(ymax1, np.squeeze(ymax2, axis=-1))
 xmax = np.minimum(xmax1, np.squeeze(xmax2, axis=-1))
 
 h = np.maximum(ymax - ymin, 0)
 w = np.maximum(xmax - xmin, 0)
 intersect = h * w
 
 union = area1 + np.squeeze(area2, axis=-1) - intersect
 return intersect / union

程序中输入为多个矩形[xmin, ymin, xmax,ymax]格式的数组或者list,输出为numpy格式,例:输入的shape为(3, 4)、(5,4)则输出为(3, 5)各个位置为boxes间相互的iou值。后面会卡一个iou的阈值,然后就可以将满足条件的索引取出。如:

def delete_bbox(bbox1, bbox2, roi_bbox1, roi_bbox2, class1, class2, idx1, idx2, iou_value):
 idx = np.where(iou_value > 0.4)
 left_idx = idx[0]
 right_idx = idx[1]
 left = roi_bbox1[left_idx]
 right = roi_bbox2[right_idx]
 xmin1, ymin1, xmax1, ymax1, = np.split(left, 4, axis=-1)
 xmin2, ymin2, xmax2, ymax2, = np.split(right, 4, axis=-1)
 left_area = (xmax1 - xmin1) * (ymax1 - ymin1)
 right_area = (xmax2 - xmin2) * (ymax2 - ymin2)
 left_idx = left_idx[np.squeeze(left_area < right_area, axis=-1)]#小的被删
 right_idx = right_idx[np.squeeze(left_area > right_area, axis=-1)]
 
 bbox1 = np.delete(bbox1, idx1[left_idx], 0)
 class1 = np.delete(class1, idx1[left_idx])
 bbox2 = np.delete(bbox2, idx2[right_idx], 0)
 class2 = np.delete(class2, idx2[right_idx])
 
 return bbox1, bbox2, class1, class2

IOU计算原理:

python不使用for计算两组、多个矩形两两间的iou方式

ymin = np.maximum(ymin1, np.squeeze(ymin2, axis=-1))

xmin = np.maximum(xmin1, np.squeeze(xmin2, axis=-1))

ymax = np.minimum(ymax1, np.squeeze(ymax2, axis=-1))

xmax = np.minimum(xmax1, np.squeeze(xmax2, axis=-1))

h = np.maximum(ymax - ymin, 0)

w = np.maximum(xmax - xmin, 0)

intersect = h * w

计算矩形间min的最大值,max的最小值,如果ymax-ymin值大于0则如左图所示,如果小于0则如右图所示

以上这篇python不使用for计算两组、多个矩形两两间的iou方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python文件写入实例分析
Apr 08 Python
Python中线程编程之threading模块的使用详解
Jun 23 Python
用python实现简单EXCEL数据统计的实例
Jan 24 Python
python 解决动态的定义变量名,并给其赋值的方法(大数据处理)
Nov 10 Python
利用python实现简易版的贪吃蛇游戏(面向python小白)
Dec 30 Python
Python设计模式之抽象工厂模式原理与用法详解
Jan 15 Python
利用python numpy+matplotlib绘制股票k线图的方法
Jun 26 Python
python中的列表与元组的使用
Aug 08 Python
python图片剪裁代码(图片按四个点坐标剪裁)
Mar 10 Python
python同时遍历两个list用法说明
May 02 Python
如何实现一个python函数装饰器(Decorator)
Oct 12 Python
Python 实现进度条的六种方式
Jan 06 Python
浅谈Python3实现两个矩形的交并比(IoU)
Jan 18 #Python
利用setuptools打包python程序的方法步骤
Jan 18 #Python
python计算二维矩形IOU实例
Jan 18 #Python
解决python replace函数替换无效问题
Jan 18 #Python
使用Python来做一个屏幕录制工具的操作代码
Jan 18 #Python
pytorch 状态字典:state_dict使用详解
Jan 17 #Python
Python标准库itertools的使用方法
Jan 17 #Python
You might like
实用函数5
2007/11/08 PHP
php 使用file_get_contents读取大文件的方法
2014/11/13 PHP
PHP简单判断iPhone、iPad、Android及PC设备的方法
2016/10/11 PHP
PHP实现获取第一个中文首字母并进行排序的方法
2017/05/09 PHP
Laravel统计一段时间间隔的数据方法
2019/10/09 PHP
TP5框架实现上传多张图片的方法分析
2020/03/29 PHP
用Greasemonkey 脚本收藏网站会员信息到本地
2009/10/26 Javascript
JavaScript 语言的递归编程
2010/05/18 Javascript
jQuery EasyUI API 中文文档 - TimeSpinner时间微调器
2011/10/23 Javascript
js 链式延迟执行DOME
2012/01/04 Javascript
js将控件隐藏及display属性的使用介绍
2013/12/30 Javascript
javascript动态修改Li节点值的方法
2015/01/20 Javascript
在linux中使用包管理器安装node.js
2015/03/13 Javascript
jQuery实现带分组数据的Table表头排序实例分析
2015/11/24 Javascript
element-ui 表格实现单元格可编辑的示例
2018/02/26 Javascript
详解用JS添加和删除class类名
2019/03/25 Javascript
Postman参数化实现过程及原理解析
2020/08/13 Javascript
Vue单页面应用中实现Markdown渲染
2021/02/14 Vue.js
Python 拷贝对象(深拷贝deepcopy与浅拷贝copy)
2008/09/06 Python
Python导入oracle数据的方法
2015/07/10 Python
python搭建虚拟环境的步骤详解
2016/09/27 Python
Python内置函数OCT详解
2016/11/09 Python
linecache模块加载和缓存文件内容详解
2018/01/11 Python
python微信跳一跳系列之棋子定位像素遍历
2018/02/26 Python
Python中循环后使用list.append()数据被覆盖问题的解决
2018/07/01 Python
python3获取当前目录的实现方法
2019/07/29 Python
Python 实现数组相减示例
2019/12/27 Python
scrapy利用selenium爬取豆瓣阅读的全步骤
2020/09/20 Python
Python return语句如何实现结果返回调用
2020/10/15 Python
C++和python实现阿姆斯特朗数字查找实例代码
2020/12/07 Python
金山毒霸系列的笔试题
2013/04/13 面试题
岗位廉政承诺书
2014/03/27 职场文书
环保专项行动方案
2014/05/12 职场文书
停车位租赁协议书
2014/09/24 职场文书
致三级跳运动员加油稿
2015/07/21 职场文书
教您:房贷工资收入证明应该怎么写?
2019/08/19 职场文书