def compute_iou(rec1, rec2):
"""
computing IoU
:param rec1: (y0, x0, y1, x1), which reflects
(top, left, bottom, right)
:param rec2: (y0, x0, y1, x1)
:return: scala value of IoU
"""
# computing area of each rectangles
S_rec1 = (rec1[2] - rec1[0]) * (rec1[3] - rec1[1])
S_rec2 = (rec2[2] - rec2[0]) * (rec2[3] - rec2[1]) # computing the sum_area
sum_area = S_rec1 + S_rec2 # find the each edge of intersect rectangle
left_line = max(rec1[1], rec2[1])
right_line = min(rec1[3], rec2[3])
top_line = max(rec1[0], rec2[0])
bottom_line = min(rec1[2], rec2[2]) # judge if there is an intersect
if left_line >= right_line or top_line >= bottom_line:
return 0
else:
intersect = (right_line - left_line) * (bottom_line - top_line)
return (intersect / (sum_area - intersect)) * 1.0 def compute_iou2(rec1, rec2):
areas1 = (rec1[3] - rec1[1]) * (rec1[2] - rec1[0])
areas2 = (rec2[3] - rec2[1]) * (rec2[2] - rec2[0])
left = max(rec1[1],rec2[1])
right = min(rec1[3],rec2[3])
top = max(rec1[0], rec2[0])
bottom = min(rec1[2], rec2[2])
w = max(0, right-left)
h = max(0, bottom-top)
return w*h/(areas2+areas1-w*h) if __name__ == '__main__':
rect1 = [661, 27, 679, 47]
# (top, left, bottom, right)
rect2 = [662, 27, 682, 47]
iou = compute_iou(rect1, rect2)
print(iou)
print(compute_iou2(rect1, rect2))

IOU计算python实现的更多相关文章

  1. 北京地铁月度消费总金额计算(Python版)

    最近业余时间在学习Python,这是那天坐地铁时突发奇想,想看看我这一个月的地铁费共多少钱,所以简单的构思了下思路,就直接开写了,没想到用Python来实现还挺简单的. 设计思路: 每次乘车正常消费7 ...

  2. 函数计算 Python 连接 SQL Server 小结

    python 连接数据库通常要安装第三方模块,连接 MS SQL Server 需要安装 pymssql .由于 pymsql 依赖于 FreeTDS,对于先于 2.1.3 版本的 pymssql,需 ...

  3. GIL计算python 2 和 python 3 计算密集型

    首先我画了一张图来表示GIL运行的方式: Python 3执行如下计算代码:#-*-conding:utf-8-*-import threading import timedef add(): n = ...

  4. 目标检测——IoU 计算

    Iou 的计算 我们先考虑一维的情况:令 \(A = [x_1,x_2], B = [y_1, y_2]\),若想要 \(A\) 与 \(B\) 有交集,需要满足如下情况: 简言之,要保证 \(A\) ...

  5. 计算Python运行时间

    可以调用datetime 或者 time库实现得到Python运行时间 方法1 import datetime start_t  = datetime.datetime.now() #运行大型代码 e ...

  6. 车位iou计算

    车位检测中,判断多帧图像检测出的车位是否是同一个车位.计算其IOU. 判断一个点是否在一个四边形内 Approach : Let the coordinates of four corners be ...

  7. DDBNet:Anchor-free新训练方法,边粒度IoU计算以及更准确的正负样本 | ECCV 2020

    论文针对当前anchor-free目标检测算法的问题提出了DDBNet,该算法对预测框进行更准确地评估,包括正负样本以及IoU的判断.DDBNet的创新点主要在于box分解和重组模块(D&R) ...

  8. 神经网络高维互信息计算Python实现(MINE)

    论文 Belghazi, Mohamed Ishmael, et al. " Mutual information neural estimation ."  Internatio ...

  9. 相似度与距离计算python代码实现

    #定义几种距离计算函数 #更高效的方式为把得分向量化之后使用scipy中定义的distance方法 from math import sqrt def euclidean_dis(rating1, r ...

随机推荐

  1. git设置忽略文件.gitignore

    在仓库目录下新建一个名为.gitignore的文件(因为是点开头,没有文件名,没办法直接在windows目录下直接创建,必须通过右键Git Bash,按照linux的方式来新建.gitignore文件 ...

  2. appium常见问题09_MAC打开uiautimatorviewer闪退怎么办?

    问题: 下载安装Android SDK后,并且已在.bash_profile文件中配置环境变量.但是在tools中打开定位工具uiautomatorviewer出现闪退. 解决: 首先检查环境变量配置 ...

  3. Java - 集合框架完全解析

    来自:http://www.jianshu.com/p/63e76826e852 数据结构是以某种形式将数据组织在一起的集合,它不仅存储数据,还支持访问和处理数据的操作.Java提供了几个能有效地组织 ...

  4. 解决:python安装mysqldb模块报 EnvironmentError: mysql_config not found

    最近学习python操作mysql需要安装mysqldb模块 出现EnvironmentError: mysql_config not found 经网上查看,需要安装mysql客户端开发库libmy ...

  5. UVA 11355 Cool Points( 极角计算 )

    We have a circle of radius R and several line segments situated within the circumference of this cir ...

  6. JQuery 获取表格table所有行第一列

    main_table 是表ID $("#main_table tr").find("td:eq(0)")

  7. 基于Windows 7(本地)和CentOS7.6(云端)的Minecraft服务器(无Forge/有Forge)搭建方法

    炎炎夏日中想和小伙伴们开黑的同学可以进来看一下了,本教程教你搭建基于两个平台的Minecraft服务器,这里我以Minecraft 1.11.2版本为例给大家讲解搭建流程.其中有Forge版本可以加入 ...

  8. MYSQL全文索引—CONTAINS语法

    我们通常在 WHERE 子句中使用 CONTAINS ,就象这样:SELECT * FROM table_name WHERE CONTAINS(fullText_column,'search con ...

  9. 【leetcode】946. Validate Stack Sequences

    题目如下: Given two sequences pushed and popped with distinct values, return true if and only if this co ...

  10. Ubuntu备份与恢复

    在使用Ubuntu之前,相信很多人都有过使用Windows系统的经历.如果你备份过Windows系统,那么你一定记忆犹新:首先需要找到一个备份工具(通常都是私有软件),然后重启电脑进入备份工具提供的软 ...