Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the intersection and union ratio of two rectangles by Python3

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces Python3 how to achieve the intersection and merger of the two rectangles, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

The concept and application of crossover ratio

Suppose there is a rectangle in the plane coordinates, and the length and width of the rectangle are parallel to the x-axis and y-axis, respectively.

Then the unique position of the rectangle in the plane coordinates can be determined by the two vertex coordinates on the diagonal (not proved here).

As shown in the following figure: the unique position of the rectangle can be determined by the upper-left and lower-right vertex coordinates, namely: (xmin, ymax, xmax, ymin), or by the lower-left and upper-right vertex coordinates, namely (xmin, ymin, xmax, ymax).

Next, let's talk about the pit I stepped on: most blogs on the Internet are marked with top-left and lower-right vertex coordinates, but the code uniformly determines the rectangular position through the bottom-left and upper-right vertex coordinates. So it looked very halo at first.

Theoretically, both methods can be used, but relatively speaking, it is more in line with our habit to determine the location of the rectangle through the lower left and upper right vertex coordinates (xmin, ymin, xmax, ymax). I think that's why most of the code on the Internet is like this.

The area of the rectangle is easy to find, as long as X is wide:

The area of the rectangle = (xmax-xmin) X (ymax-ymin)

All right, after figuring out how to determine the position of the rectangle, let's solve the problem of calculating the intersection and merge ratio.

Cross-merge ratio (Intersection over Union, IoU) is a very important concept in target detection. It is the overlap rate of the generated prediction box (Predicted bounding box) and the original marker box (Ground-truth bounding box), that is, the ratio of their intersection (intersection area) to union (total area). Ideally, there is complete overlap, that is, the ratio is 1. Generally speaking, this score > 0. 5 can be considered a good result. This standard is used to measure the correlation between reality and prediction, the higher the correlation, the higher the value, it can evaluate the accuracy of the algorithm.

Suppose there are two rectangles in the plane coordinates: the original marker box (Ground-truth bounding box, G) and the prediction box (Predicted bounding box, P), where G is the manually marked box and P is the box predicted by the algorithm, and the length and width of the two rectangles are parallel to the x-axis and y-axis, respectively. As shown in the following figure:

IoU calculation formula:

So there are rectangle G (gxmin, gymin, gxmax, gymax) and rectangle P (pxmin, pymin, pxmax, pymax).

The key to finding the intersection ratio is to find out the area of the intersection rectangle G ∩ P.

To solve this problem, we only need to determine the vertex coordinates of the lower left (xmin, ymin) and the upper right (xmax, ymax) of the intersecting rectangle, and then determine (xmin, ymin, xmax, ymax).

By looking at the picture, we can clearly observe:

# the lower left vertex coordinates of the intersecting rectangles are the maximum values of the lower left coordinates of the two rectangles xmin = max (gxmin, pxmin) ymin = max (gymin, pymin) # the upper right vertex coordinates of the intersecting rectangles, that is, the minimum values of the upper right coordinates of the two rectangles xmax = min (gxmax, pxmax) ymax = min (gymax, pyxmax)

If you don't understand it at once, you can draw more pictures on the paper and understand.

Given the coordinates of the intersecting rectangles (xmin, ymin, xmax, ymax), then the area of the intersecting rectangles is very simple.

Area (G ∩ P) = length X width

W = xmax-xmin # calculate the length of the intersecting rectangle

H = ymax-ymin # calculate the width of the intersecting rectangle

Area (G ∩ P) = w X h # calculates the area of the intersecting rectangle

There is one last problem. When the calculated width or length is 0 or negative, it means that the two rectangles do not intersect and the intersecting area is 0, then the final IoU is 0. Here we have two ways to deal with it:

1. Use the if statement to classify the discussion:

If w

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report