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. 记录java

    1.从今天起,我会将自己在java学习道路上的一些心得体会记录下来.

  2. 使用UI Automation实现自动化测试--5-7

    使用UI Automation实现自动化测试--5 (Winfrom和WPF中弹出和关闭对话框的不同处理方式) 在使用UI Automation对Winform和WPF的程序测试中发现有一些不同的地方 ...

  3. 第6章 RPC之道

    6.1 认识RPC 分布式.微服务的架构思维中都不能缺少 RPC 的影子 RPC(Remote Procedure Call)远程过程调用.通过网络在跨进程的两台服务器之间传输信息,我们使用的时候不用 ...

  4. for语句基础求和练习

    结构 for(初始化表达式;条件表达式;循环后的操作表达式) { 循环体; } 1.求出1-10之间数据之和: class Hello2 { public static void main(Strin ...

  5. 全局唯一iD的生成 雪花算法详解及其他用法

    一.介绍 雪花算法的原始版本是scala版,用于生成分布式ID(纯数字,时间顺序),订单编号等. 自增ID:对于数据敏感场景不宜使用,且不适合于分布式场景.GUID:采用无意义字符串,数据量增大时造成 ...

  6. WPF自适应问题

    引用水哥同事的文章 点击跳转

  7. C# 下载模板

    /// <summary> /// 模板下载 /// </summary> /// <returns></returns> public ActionR ...

  8. ThreadLocal的使用和理解

    ThreadLocal是个threadlocalvariable(线程局部变量),其实就是为每一个使用该变量的线程都提供一个变量值的副本,从线程的角度看,每个线程都保持一个对其线程局部变量副本的隐式引 ...

  9. 前端学习(二十二)css3(笔记)

    html5        普通:        header section footer nav article aside figure 特殊:        canvas video audio ...

  10. WinForm解决UI假死

    运行WinForm程序时,如果后台执行比较费时的操作,前天UI就会假死卡住,很影响使用感受,这里我们简单的解决一下这个问题 using System; using System.Collections ...