当前位置:网站首页>Basis of target detection (IOU)
Basis of target detection (IOU)
2022-07-03 16:17:00 【victor_ gx】
Fundamentals of target detection (IoU)

What is? IOU?
IOU( Occurring simultaneously than Intersection over Union) It's a term , Used to describe the degree of overlap between two boxes . The larger the overlap area ,IOU The bigger it is .
IOU It is mainly used in applications related to object detection , In this application , Our training model outputs an external rectangle that completely surrounds the target . for example , In the diagram above , We have a green box and a blue box . The green box represents the real box , The blue box represents the prediction box of our model . The purpose of the training model is to continuously improve the output of its prediction frame , Until the blue and green boxes completely overlap , Between two boxes IOU be equal to 1.
IOU It is often used in non maximum suppression , Multiple prediction frames of the same object are eliminated based on the confidence of the detection frame , To ensure the prediction box with the highest retention reliability .
IOU Calculation formula
In target detection , Let's assume that the box 1 from [x1,y1,x2,y2] Express , Same box 2 from [x3,y3,x4,y4] Express , Here's the picture 1 Shown .

here IOU The calculation formula of is as follows :
I o U = A r e a o f I n t e r s e c t i o n o f t w o b o x e s A r e a o f U n i o n o f t w o b o x e s IoU=\frac{Area\ of\ Intersection\ of\ two\ boxes}{Area\ of\ Union\ of\ two\ boxes} IoU=Area of Union of two boxesArea of Intersection of two boxes
Use pictures 2 Shown by the following :

Next, we calculate the area corresponding to the intersection and union of the corresponding rectangular box step by step .
Calculate the intersection area
Intuitive to see , The intersection of two rectangular boxes has many situations , Let's intuitively observe the following figure 3, Obviously, the lower right two rectangular boxes completely coincide, and the intersection area is the largest .

First, we express the coordinates of the intersecting rectangles as [x_inter1,y_inter1,x_inter2,y_inter2], Represent the coordinates of the upper left corner and the lower right corner of the intersecting rectangle respectively .

Then start by defining the reference coordinate system . We use +X The axis is moving to the right ,+Y Computer graphics Convention for axial downward movement .
In order to calculate the coordinates of the upper left corner of the intersecting rectangle , We compare the upper left corner of each box . We can see from the example above ,x_inter1 You can find which box is closer to the right in the upper left corner . Similarly , You can find... By looking at which box's upper left corner is lower than the other box y_inter1. Mathematically speaking , They can be summarized as : x _ i n t e r 1 = m a x ( x 1 , x 3 ) x\_inter1=max(x1,x3) x_inter1=max(x1,x3), y y y_ i n t e r 1 = m a x ( y 1 , y 3 ) inter1=max(y1,y3) inter1=max(y1,y3)
To calculate the coordinates of the lower right corner of the intersecting rectangle , We compare the lower right corner of each box .
x_inter2 You can find by looking at which box's lower right corner is more left .
Similarly , You can find... By looking at which box's lower right corner is higher than the other box y_inter2.
Similarly , They can be summarized as : x _ i n t e r 2 = m a x ( x 2 , x 4 ) x\_inter2=max(x2,x4) x_inter2=max(x2,x4), y y y_ i n t e r 2 = m a x ( y 2 , y 4 ) inter2=max(y2,y4) inter2=max(y2,y4)
Now we have the coordinates of the intersecting rectangles , Then the area of the intersection is the area of the rectangle .( in application , We will use the absolute values of width and height , To ensure that even if the order of boxes changes , The width and height are still positive ; namely , If x_inter1>x_inter2, We will still get a positive value for the width )
w i d t h _ i n t e r = ( x _ i n t e r 2 − x _ i n t e r 1 ) h e i g h t _ i n t e r = ( y _ i n t e r 2 − y _ i n t e r 1 ) a r e a _ i n t e r = w i d t h _ i n t e r ∗ h e i g h t _ i n t e r width\_inter=(x\_inter2-x\_inter1) \\height\_inter=(y\_inter2-y\_inter1) \\area\_inter=width\_inter*height\_inter width_inter=(x_inter2−x_inter1)height_inter=(y_inter2−y_inter1)area_inter=width_inter∗height_inter
Calculate Union area
The union of boxes is the total area covered by two boxes , Pictured 2 Shown .
To find the total area , We first calculate the area of each rectangular box
w i d t h _ b o x 1 = ( x _ 2 − x _ 1 ) h e i g h t _ b o x 1 = ( y _ 2 − y _ 1 ) w i d t h _ b o x 2 = ( x _ 4 − x _ 3 ) h e i g h t _ b o x 2 = ( y _ 4 − y _ 3 ) a r e a _ b o x 1 = w i d t h _ b o x 1 ∗ h e i g h t _ b o x 1 a r e a _ b o x 2 = w i d t h _ b o x 2 ∗ h e i g h t _ b o x 2 width\_box1=(x\_2-x\_1) \\height\_box1=(y\_2-y\_1) \\width\_box2=(x\_4-x\_3) \\height\_box2=(y\_4-y\_3) \\area\_box1=width\_box1*height\_box1 \\area\_box2=width\_box2*height\_box2 width_box1=(x_2−x_1)height_box1=(y_2−y_1)width_box2=(x_4−x_3)height_box2=(y_4−y_3)area_box1=width_box1∗height_box1area_box2=width_box2∗height_box2
If we look at the total area covered by the two boxes , We will see that both boxes cover the part of the intersecting rectangle , That is, the area of the intersecting rectangle is included in box1 Areas and box2 In the region .
Because we only want to calculate the common intersection area once , We can subtract the intersection area we calculated from the total area of the two boxes . namely :
a r e a _ u n i o n = a r e a _ b o x 1 + a r e a _ b o x 2 − a r e a _ i n t e r s e c t i o n area\_union=area\_box1+area\_box2-area\_intersection area_union=area_box1+area_box2−area_intersection
Calculate the ratio of intersection and union
Through the above calculation formula , We calculate the area of the corresponding intersection and union of two rectangular boxes , At this time, the rectangle is IOU The calculation formula is as follows :
I o U = a r e a _ i n t e r a r e a _ u n i o n IoU=\frac{area\_inter}{area\_union} IoU=area_unionarea_inter
Observe the above formula , It can be concluded as follows :
- Two box IOU Can take 0 To 1 Any value between .
- If the two boxes do not intersect , Then their intersection area will be 0, therefore IOU Will also be 0.
- If two completely overlapping rectangular boxes , Then the area of the intersection will be equal to the area of its Union , therefore IOU Will be for 1
Code implementation
def IOU(box1, box2):
x1, y1, x2, y2 = box1
x3, y3, x4, y4 = box2
x_inter1 = max(x1, x3)
y_inter1 = max(y1, y3)
x_inter2 = min(x2, x4)
y_inter2 = min(y2, y4)
width_inter = abs(x_inter2 - x_inter1)
height_inter = abs(y_inter2 - y_inter1)
area_inter = width_inter * height_inter
width_box1 = abs(x2 - x1)
height_box1 = abs(y2 - y1)
width_box2 = abs(x4 - x3)
height_box2 = abs(y4 - y3)
area_box1 = width_box1 * height_box1
area_box2 = width_box2 * height_box2
area_union = area_box1 + area_box2 - area_inter
iou = area_inter / area_union
return iou
- function IOU Accept two boxes , namely box1 and box2 As input . The data in each box is a containing [x1,y1,x2,y2] A list of , It represents the coordinates of the upper left corner and the lower right corner .
- As mentioned earlier , Let's first calculate the area of intersecting rectangles , Then the area of the Union .
- abs The function is python The built-in function used to calculate the absolute value in . This ensures that we will never calculate the result with negative width or negative height .
- Back to IOU yes float Type value , Be situated between 0 and 1 Between .
The operation results are as follows :
边栏推荐
- LeetCode1491. Average value of wages after removing the minimum wage and the maximum wage
- 架构实战营 - 第 6 期 毕业总结
- Mongodb installation and basic operation
- [redis foundation] understand redis master-slave architecture, sentinel mode and cluster together (Demo detailed explanation)
- Advanced Mathematics (Seventh Edition) Tongji University exercises 2-1 personal solutions
- App mobile terminal test [4] APK operation
- PHP CI(CodeIgniter)log级别设置
- NSQ源码安装运行过程
- Qt插件之自定义插件构建和使用
- Embedded development: seven reasons to avoid open source software
猜你喜欢

Brush questions -- sword finger offer

拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。

Low level version of drawing interface (explain each step in detail)

TCP拥塞控制详解 | 3. 设计空间

Uploads labs range (with source code analysis) (under update)

深度学习之三维重建

嵌入式开发:避免开源软件的7个理由

面试官:JVM如何分配和回收堆外内存

TCP擁塞控制詳解 | 3. 設計空間
![[statement] about searching sogk1997 and finding many web crawler results](/img/1a/8ed3ca0030ea227adcd95e8b306aca.png)
[statement] about searching sogk1997 and finding many web crawler results
随机推荐
Myopia: take off or match glasses? These problems must be understood clearly first
远程文件包含实操
“用Android复刻Apple产品UI”(2)——丝滑的AppStore卡片转场动画
First knowledge of database
程序猿如何快速成长
记一次jar包冲突解决过程
From "zero sum game" to "positive sum game", PAAS triggered the third wave of cloud computing
[200 opencv routines] 217 Mouse interaction to obtain polygon area (ROI)
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
Caching mechanism of Hibernate / session level caching mechanism
Unity项目优化案例一
Redis high availability and persistence
0214-27100 a day with little fluctuation
架构实战营 - 第 6 期 毕业总结
Q2 encryption market investment and financing report in 2022: gamefi becomes an investment keyword
"Remake Apple product UI with Android" (2) -- silky Appstore card transition animation
NSQ源码安装运行过程
用通达信炒股开户安全吗?
App mobile terminal test [3] ADB command
[proteus simulation] 8 × 8LED dot matrix screen imitates elevator digital scrolling display