Python implementation of IOU calculation case

Calculating the intersection ratio of two rectangles is usually used as a detection index in the detection task. The difference between your predicted bbox and groundtruth can be reflected through IOU. Very simple algorithm implementation, I also wrote one casually, well, very simple.

  1. When using it, please pay attention to the order of the four numbers in the bbox (y0, x0, y1, x1), the order is not the same.
#! /usr/bin/env python
# encoding: utf-8
 
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:return0else:
 intersect =(right_line - left_line)*(bottom_line - top_line)return(intersect /(sum_area - intersect))*1.0if __name__=='__main__':
 rect1 =(661,27,679,47)
 # ( top, left, bottom, right)
 rect2 =(662,27,682,47)
 iou =compute_iou(rect1, rect2)print(iou)

Supplementary knowledge: IOU algorithm based on Python-the simplest and easy to understand code implementation

Concept introduction:

Intersection over Union: (Intersection over Union)

As shown in the figure above, the IOU value is positioned as the ratio of the intersection and union of the areas of two rectangular boxes. which is:

The implementation of cross-to-comparison is also very simple, the execution process is as follows:

  1. The width of the intersection shape is calculated as:

IOU_W = min(x1,x2,x3,x4)+w1+w2-max(x1,x2,x3,x4)

  1. The height of the intersection shape is calculated as:

IOU_H = min(y1,y2,y3,y4)+h1+h2-max(y1,y2,y3,y4)

In fact, it is a very simple geometric relationship transformation. The above diagram can help you understand this meaning well.

Code implementation: 001-IOU calculation

The above python implementation of IOU calculation case is all the content shared by the editor, I hope to give you a reference.

Recommended Posts

Python implementation of IOU calculation case
Python implementation of intersection and IOU tutorial
Python implementation of gomoku program
Implementation of reverse traversal of python list
Python preliminary implementation of word2vec operation
Python calculation of information entropy example
Implementation of python selenium operation cookie
Implementation of python3 registration global hotkey
Implementation of python student management system
Implementation of python gradient descent algorithm
Basic analysis of Python turtle library implementation
Implementation of JWT user authentication in python
Implementation principle of dynamic binding of Python classes
7 features of Python3.9
Implementation of Python headless crawler to download files
Python implementation of AI automatic matting example analysis
Python implementation of hand drawing effect example sharing
Implementation of business card management system with python
Python writes the game implementation of fishing master
Implementation of business card management system based on python
Detailed explanation of the implementation steps of Python interface development
Basics of Python syntax
Basic syntax of Python
Basic knowledge of Python (1)
Prettytable module of python
09. Common modules of Python3
Consolidate the foundation of Python (4)
Consolidate the foundation of Python(7)
In-depth understanding of python list (LIST)
Subscripts of tuples in Python
Python analysis of wav files
How Python converts string case
Consolidate the foundation of Python(6)
Python drawing rose implementation code
Analysis of JS of Python crawler
Python numpy implements rolling case
python king of glory wallpaper
Consolidate the foundation of Python(5)
Analysis of Python Sandbox Escape
Some new features of Python 3.10
Deep understanding of Python multithreading
Analysis of Python object-oriented programming
Implementation of CentOS8.0 Network Configuration
Python version of OpenCV installation
Is python code case sensitive
Python GUI simulation implementation calculator
9 feature engineering techniques of Python
matplotlib of python drawing module
Python method of parameter passing
Consolidate the foundation of Python (3)
Collection of Python Common Modules
Python arithmetic sequence calculation method