车位iou计算

博客围绕车位检测展开,涉及判断多帧图像检测出的车位是否为同一车位并计算IOU,重点介绍判断一个点是否在四边形内的方法,通过计算四边形面积及该点与四边形各边构成三角形的面积,若各三角形面积之和等于四边形面积,则点在四边形内。

车位检测中,判断多帧图像检测出的车位是否是同一个车位.计算其IOU.

判断一个点是否在一个四边形内

Approach : Let the coordinates of four corners be A(x1, y1), B(x2, y2), C(x3, y3) and D(x4, y4). And coordinates of the given point P be (x, y)

  1. Calculate area of the given rectangle, i.e., area of the rectangle ABCD as area of triangle ABC + area of triangle ACD.
    Area A = [ x1(y2 – y3) + x2(y3 – y1) + x3(y1-y2)]/2 + [ x1(y4 – y3) + x4(y3 – y1) + x3(y1-y4)]/2
  2. Calculate area of the triangle PAB as A1.
  3. Calculate area of the triangle PBC as A2.
  4. Calculate area of the triangle PCD as A3.
  5. Calculate area of the triangle PAD as A4.
  6. If P lies inside the triangle, then A1 + A2 + A3 + A4 must be equal to A.
#include <bits/stdc++.h> 
using namespace std; 
  
/* A utility function to calculate area of  
   triangle formed by (x1, y1), (x2, y2) and 
  (x3, y3) */
float area(int x1, int y1, int x2, int y2, 
                            int x3, int y3) 
{ 
    return abs((x1 * (y2 - y3) + x2 * (y3 - y1) +  
                x3 * (y1 - y2)) / 2.0); 
} 
  
/* A function to check whether point P(x, y)  
   lies inside the rectangle formed by A(x1, y1),  
   B(x2, y2), C(x3, y3) and D(x4, y4) */
bool check(int x1, int y1, int x2, int y2, int x3,  
             int y3, int x4, int y4, int x, int y) 
{ 
    /* Calculate area of rectangle ABCD */
    float A = area(x1, y1, x2, y2, x3, y3) +  
              area(x1, y1, x4, y4, x3, y3); 
  
    /* Calculate area of triangle PAB */
    float A1 = area(x, y, x1, y1, x2, y2); 
  
    /* Calculate area of triangle PBC */
    float A2 = area(x, y, x2, y2, x3, y3); 
  
    /* Calculate area of triangle PCD */
    float A3 = area(x, y, x3, y3, x4, y4); 
  
    /* Calculate area of triangle PAD */
    float A4 = area(x, y, x1, y1, x4, y4); 
  
    /* Check if sum of A1, A2, A3 and A4  
       is same as A */
    return (A == A1 + A2 + A3 + A4); 
} 
  
/* Driver program to test above function */
int main() 
{ 
    /* Let us check whether the point P(10, 15) 
      lies inside the rectangle formed by A(0, 10), 
      B(10, 0) C(0, -10) D(-10, 0) */
    if (check(0, 10, 10, 0, 0, -10, -10, 0, 10, 15)) 
        cout << "yes"; 
    else
        cout << "no"; 
    return 0; 
} 

转载于:https://www.cnblogs.com/sdu20112013/p/10919352.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值