How to judge whether a point is in the middle of a box

In the process of fusion of radar structured data and targets detected by deep learning, how to quickly determine whether a radar target point is in the box (whether the fusion is successful) after the target acquired by the radar is converted from the world coordinate system to the pixel coordinate system.

You can refer to two methods:

1. Direct Judgment Method

Directly obtain the pixel coordinates of the radar target on the image, and then compare whether the x coordinate of the radar target is between [xmin, xmax] of the bbox box, and whether the y coordinate is between [ymin, ymax] of the bbox box, if both True, it means that it is in the bbox box.
insert image description here
The code is as follows:

def check_in_box(box, point):
    '''
    box : xyxy format
    point: x,y
    '''
    x, y = point
    if (x >= box[0] & x <= box[2]) & (y >= box[1] & y <= box[3]):
        return True
    else:
        return False

2. Coordinate vector method

The cross product of vectors is also called vector product, outer product, etc.
The result of the cross product of a and b is a vector, denoted as a×b. The angle between a and b is θ, then its mode and direction are:
mode: |a×b| = |a||b|sinθ
direction: perpendicular to the plane formed by a and b, and satisfy the right hand The rule
can be understood from the above formula that the result of the cross product of vectors a and b depends on sinθ. If the value of sinθ is greater than 0, according to the right-hand rule, it means that vector a can turn to the direction of vector b in the counterclockwise direction. In layman's terms, it is b The vector is on the left side of the a vector, and the other cases are similar, so I won't repeat them here. Therefore, it can be judged whether the fixed point is inside the rectangular frame according to this principle.
details as follows:

  • 1. The rectangle and point p are shown in the figure below
    insert image description here
  • 2. Determine whether point p is inside the rectangle, mainly to determine whether point p is inside the rectangle, which is equivalent to judging: 1. point p is between AB and CD, 2. point p is between AD and BC, so only calculation is required The following values ​​are enough:
    (1) BA×BP>0&DC×DP>0 means that point P is between AB and CD
    (2) AD×AP>0&CB×CP>0 means that point P is between AD and BC at the
    same time Satisfying the above two conditions can verify whether P is inside the rectangle.
    In the plane Cartesian coordinate system,
    vector a=(x1, y1)
    and vector b=(x2, y2)
    , then |a×b| = |x1y2-x2y1|

code show as below:

def check_in_box(box, point):
    '''
    box : xyxy format
    point: x,y
    '''
    x, y = point
    xmin, ymin, xmax, ymax = box
    BA = (xmin - xmax, 0)
    BP = (x - xmax, y - ymin)
    DC = (xmax - xmin, 0)
    DP = (x - xmin, y - ymax)
    AD = (0, ymax - ymin)
    AP = (x - xmin, y - ymin)
    CB = (0, ymin - ymax)
    CP = (x - xmax, y - ymax)
    
    a = BA[0]*BP[1]-BA[1]*BP[0]  # BA×BP
    b = DC[0]*DP[1]-DC[1]*DP[0]  # DC×DP
    c = AD[0]*AP[1]-AD[1]*AP[0]  # AD×AP
    d = CB[0]*CP[1]-CB[1]*CP[0]  # CB×CP

    if (a>0&b>0)&(c>0&d>0):
        return True
    else:
        False

3. Ray method

The main principle is that the points existing inside or outside the convex shape, by using it as the starting point, extend outward infinitely, and the number of intersection points with the sides of the convex shape, if it is an even number, the point is outside the convex shape, If odd, it is inside the convex shape.
The c++ code for its specific implementation is as follows:

#include"PtInPolygon.h"

bool PtInPolygon(Point2d p, vector<Point2d> &ptpolygon, int nCount)
{
    
    
	int nCross = 0;
	for (int i = 0; i < nCount; i++)
	{
    
    
		Point2d p1 = ptpolygon[i];
		Point2d p2 = ptpolygon[(i + 1) % nCount];
		if (p1.y == p2.y) {
    
    
			continue;
		}
		if (p.y < min(p1.y, p2.y)) {
    
    
			continue;
		}
		if (p.y >= max(p1.y, p2.y)) {
    
    
			continue;
		}

		double x = (double)(p.y - p1.y) * (double)(p2.x - p1.x) / (double)(p2.y - p1.y) + p1.x;
		if (x > p.x) {
    
    
			nCross++; //统计在点右侧射线的交点
		}
	}
	if ((nCross % 2) == 1) {
    
     // 在区域内部
		return true;
	}
	else
	{
    
    
		return false;
	}
}

The above is my summary, if you have any questions, please correct me.

Guess you like

Origin blog.csdn.net/caobin_cumt/article/details/129831820