当前位置:网站首页>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 :
边栏推荐
- Redis high availability and persistence
- A Fei's expectation
- Project -- high concurrency memory pool
- [200 opencv routines] 217 Mouse interaction to obtain polygon area (ROI)
- [combinatorics] combinatorial identities (sum of variable terms 3 combinatorial identities | sum of variable terms 4 combinatorial identities | binomial theorem + derivation to prove combinatorial ide
- [combinatorics] summary of combinatorial identities (eleven combinatorial identities | proof methods of combinatorial identities | summation methods)*
- [system safety] 43 PowerShell malicious code detection series (5) automatic extraction of ten thousand words from abstract syntax tree
- 一些事情的反思
- MongoDB 的安装和基本操作
- MB10M-ASEMI整流桥MB10M
猜你喜欢
![[list to map] collectors Tomap syntax sharing (case practice)](/img/ac/e02deb1cb237806d357a88fb812852.jpg)
[list to map] collectors Tomap syntax sharing (case practice)

半监督学习

The difference between calling by value and simulating calling by reference

Jmeter线程组功能介绍

Mongodb installation and basic operation

How to use AAB to APK and APK to AAB of Google play apps on the shelves

Three dimensional reconstruction of deep learning

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

About text selection in web pages and counting the length of selected text

Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (4)
随机推荐
【OpenCV 例程200篇】217. 鼠标交互获取多边形区域(ROI)
How can technology managers quickly improve leadership?
TCP拥塞控制详解 | 3. 设计空间
Page dynamics [2]keyframes
PHP CI(CodeIgniter)log级别设置
六月 致 -.-- -..- -
Detailed explanation of four modes of distributed transaction (Seata)
[combinatorics] summary of combinatorial identities (eleven combinatorial identities | proof methods of combinatorial identities | summation methods)*
嵌入式开发:避免开源软件的7个理由
Extraction of the same pointcut
About text selection in web pages and counting the length of selected text
How to use AAB to APK and APK to AAB of Google play apps on the shelves
Deep understanding of grouping sets statements in SQL
Multithread 02 thread join
Caching mechanism of Hibernate / session level caching mechanism
Pandora IOT development board learning (HAL Library) - Experiment 5 external interrupt experiment (learning notes)
疫情常态化大背景下,关于远程办公的思考|社区征文
Qt插件之自定义插件构建和使用
Unity项目优化案例一
Nifi from introduction to practice (nanny level tutorial) - flow