赞
踩
- //求任意多边形的面积
- /*语法:result = polygonarea(vector<Point>&polygon, int N);
- 参数:
- polygon:多变形顶点数组
- N:多边形顶点数目
- 返回值:多边形面积
- 注意:
- 支持任意多边形,凹、凸皆可
- 多边形顶点输入时按顺时针顺序排列
- */
- #include <iostream>
- #include <vector>
- using namespace std;
- typedef struct Point{
- double x, y;
- Point(){}
- } Point;
- double polygonarea(vector<Point>&polygon, int N)
- {
- int i, j;
- double area = 0;
- for (i = 0; i<N; i++) {
- j = (i + 1) % N;
- area += polygon[i].x * polygon[j].y;
- area -= polygon[i].y * polygon[j].x;
- }
- area /= 2;
- return(area < 0 ? -area : area);
- }
-
- //测试函数
- int main(){
-
- vector<Point> polygon;
- int n;
- cin >> n;
- polygon.resize(n);
- int i = 0;
- while (n--){
- double x, y;
- cin >> x >> y;
- polygon[i].x = x;
- polygon[i].y = y;
- ++i;
- }
- cout << "此多边形的面积为" << polygonarea(polygon, polygon.size()) << endl;
-
-
- return 0;
- }
-
-
计算IOU代码稍后补充。
计算IOU最初只计算正矩形的IOU,当矩形有角度或者任意四边形的IOU计算就比较困难。一般见到的计算IOU都是python直接调用封装好的库。如下,下面代码从shapely中导出Polygon库,其实可以直接导入Polygon库。
看到一篇clipper和Polygon的文章:python多边形裁剪库效果对比
一般来说,这个IoU > 0.5 就可以被认为是一个不错的结果
有些目标检测中,预测的边界框为规则的矩形,则只需要知道矩形的左上角和右下角的坐标信息,就可以得到矩形框所有想要的信息。对于这种情况,IoU的python实现如下(python3.5)
- def IoU(box1, box2):
- '''
- 计算两个矩形框的交并比
- :param box1: list,第一个矩形框的左上角和右下角坐标
- :param box2: list,第二个矩形框的左上角和右下角坐标
- :return: 两个矩形框的交并比iou
- '''
- x1 = max(box1[0], box2[0]) # 交集左上角x
- x2 = min(box1[2], box2[2]) # 交集右下角x
- y1 = max(box1[1], box2[1]) # 交集左上角y
- y2 = min(box1[3], box2[3]) # 交集右下角y
-
- overlap = max(0., x2-x1) * max(0., y2-y1)
- union = (box1[2]-box1[0]) * (box1[3]-box1[1]) \
- + (box2[2]-box2[0]) * (box2[3]-box2[1]) \
- - overlap
-
- return overlap/union
- if __name__ == '__main__':
- # box = [左上角x1,左上角y1,右下角x2,右下角y2]
- box1 = [10, 0, 15, 10]
- box2 = [12, 5, 20, 15]
- iou = IoU(box1, box2)
在有些目标检测中,检测框并不是规则的矩形框,例如自然场景下的文本检测,有些呈现平行四边形,梯形等情况,这时计算IoU时,就比较复杂一些。这时可以借助于python的一些库实现多边形的面积计算
- import shapely
- import numpy as np
- from shapely.geometry import Polygon, MultiPoint # 多边形
-
- def bbox_iou_eval(box1, box2):
- '''
- 利用python的库函数实现非矩形的IoU计算
- :param box1: list,检测框的四个坐标[x1,y1,x2,y2,x3,y3,x4,y4]
- :param box2: lsit,检测框的四个坐标[x1,y1,x2,y2,x3,y3,x4,y4]
- :return: IoU
- '''
- box1 = np.array(box1).reshape(4, 2) # 四边形二维坐标表示
- # python四边形对象,会自动计算四个点,并将四个点重新排列成
- # 左上,左下,右下,右上,左上(没错左上排了两遍)
- poly1 = Polygon(box1).convex_hull
- box2 = np.array(box2).reshape(4, 2)
- poly2 = Polygon(box2).convex_hull
-
- if not poly1.intersects(poly2): # 如果两四边形不相交
- iou = 0
- else:
- try:
- inter_area = poly1.intersection(poly2).area # 相交面积
- iou = float(inter_area) / (poly1.area + poly2.area - inter_area)
- except shapely.geos.TopologicalError:
- print('shapely.geos.TopologicalError occured, iou set to 0')
- iou = 0
-
- return iou
-
- if __name__ == '__main__':
- # box = [四个点的坐标,顺序无所谓]
- box3 = [10, 0, 15, 0, 15, 10, 10, 10] # 左上,右上,右下,左下
- box4 = [12, 5, 20, 2, 20, 15, 12, 15]
- iou = bbox_iou_eval(box3, box4)
- print(iou)
参考:IOU交并比的计算
关于计算交集面积的代码看到一篇,还没有验证:Gym 100952J&&2015 HIAST Collegiate Programming Contest J. Polygons Intersection【计算几何求解两个凸多边形的相交面积板子题】
- #include "iostream"
- #include "string.h"
- #include "stack"
- #include "queue"
- #include "string"
- #include "vector"
- #include "set"
- #include "map"
- #include "algorithm"
- #include "stdio.h"
- #include "math.h"
- #define ll long long
- #define bug(x) cout<<x<<" "<<"UUUUU"<<endl;
- #define mem(a) memset(a,0,sizeof(a))
- #define mp(x,y) make_pair(x,y)
- using namespace std;
- const long long INF = 1e18+1LL;
- const int inf = 1e9+1e8;
- const int N=1e5+100;
- #define maxn 510
- const double eps=1E-8;
- int sig(double d){
- return(d>eps)-(d<-eps);
- }
- struct Point{
- double x,y; Point(){}
- Point(double x,double y):x(x),y(y){}
- bool operator==(const Point&p)const{
- return sig(x-p.x)==0&&sig(y-p.y)==0;
- }
- };
- double cross(Point o,Point a,Point b){
- return(a.x-o.x)*(b.y-o.y)-(b.x-o.x)*(a.y-o.y);
- }
- double area(Point* ps,int n){
- ps[n]=ps[0];
- double res=0;
- for(int i=0;i<n;i++){
- res+=ps[i].x*ps[i+1].y-ps[i].y*ps[i+1].x;
- }
- return res/2.0;
- }
- int lineCross(Point a,Point b,Point c,Point d,Point&p){
- double s1,s2;
- s1=cross(a,b,c);
- s2=cross(a,b,d);
- if(sig(s1)==0&&sig(s2)==0) return 2;
- if(sig(s2-s1)==0) return 0;
- p.x=(c.x*s2-d.x*s1)/(s2-s1);
- p.y=(c.y*s2-d.y*s1)/(s2-s1);
- return 1;
- }
- //多边形切割
- //用直线ab切割多边形p,切割后的在向量(a,b)的左侧,并原地保存切割结果
- //如果退化为一个点,也会返回去,此时n为1
- void polygon_cut(Point*p,int&n,Point a,Point b){
- static Point pp[maxn];
- int m=0;p[n]=p[0];
- for(int i=0;i<n;i++){
- if(sig(cross(a,b,p[i]))>0) pp[m++]=p[i];
- if(sig(cross(a,b,p[i]))!=sig(cross(a,b,p[i+1])))
- lineCross(a,b,p[i],p[i+1],pp[m++]);
- }
- n=0;
- for(int i=0;i<m;i++)
- if(!i||!(pp[i]==pp[i-1]))
- p[n++]=pp[i];
- while(n>1&&p[n-1]==p[0])n--;
- }
- //---------------华丽的分隔线-----------------//
- //返回三角形oab和三角形ocd的有向交面积,o是原点//
- double intersectArea(Point a,Point b,Point c,Point d){
- Point o(0,0);
- int s1=sig(cross(o,a,b));
- int s2=sig(cross(o,c,d));
- if(s1==0||s2==0)return 0.0;//退化,面积为0
- if(s1==-1) swap(a,b);
- if(s2==-1) swap(c,d);
- Point p[10]={o,a,b};
- int n=3;
- polygon_cut(p,n,o,c);
- polygon_cut(p,n,c,d);
- polygon_cut(p,n,d,o);
- double res=fabs(area(p,n));
- if(s1*s2==-1) res=-res;return res;
- }
- //求两多边形的交面积
- double intersectArea(Point*ps1,int n1,Point*ps2,int n2){
- if(area(ps1,n1)<0) reverse(ps1,ps1+n1);
- if(area(ps2,n2)<0) reverse(ps2,ps2+n2);
- ps1[n1]=ps1[0];
- ps2[n2]=ps2[0];
- double res=0;
- for(int i=0;i<n1;i++){
- for(int j=0;j<n2;j++){
- res+=intersectArea(ps1[i],ps1[i+1],ps2[j],ps2[j+1]);
- }
- }
- return res;//assumeresispositive!
- }
- //hdu-3060求两个任意简单多边形的并面积
- Point ps1[maxn],ps2[maxn];
- int n1,n2;
- int main(){
- int t;
- cin>>t;
- while(t--){
- scanf("%d%d",&n1,&n2);
- for(int i=0;i<n1;i++)
- scanf("%lf%lf",&ps1[i].x,&ps1[i].y);
- for(int i=0;i<n2;i++)
- scanf("%lf%lf",&ps2[i].x,&ps2[i].y);
- double ans=intersectArea(ps1,n1,ps2,n2);
- //ans=fabs(area(ps1,n1))+fabs(area(ps2,n2))-ans;//容斥
- printf("%.4f\n",ans);
- }
- return 0;
- }
运行输入和结果如下(其中前四行是输入,最后一行是输出):
另外还看到一篇计算IOU的代码:https://github.com/CAPTAIN-WHU/DOTA_devkit/blob/master/polyiou.cpp
- #include<cstdio>
- #include<iostream>
- #include<algorithm>
- #include<cmath>
- #include <vector>
- using namespace std;
- #define maxn 51
- const double eps=1E-8;
- int sig(double d){
- return(d>eps)-(d<-eps);
- }
- struct Point{
- double x,y; Point(){}
- Point(double x,double y):x(x),y(y){}
- bool operator==(const Point&p)const{
- return sig(x-p.x)==0&&sig(y-p.y)==0;
- }
- };
- double cross(Point o,Point a,Point b){ //叉积
- return(a.x-o.x)*(b.y-o.y)-(b.x-o.x)*(a.y-o.y);
- }
- double area(Point* ps,int n){
- ps[n]=ps[0];
- double res=0;
- for(int i=0;i<n;i++){
- res+=ps[i].x*ps[i+1].y-ps[i].y*ps[i+1].x;
- }
- return res/2.0;
- }
- int lineCross(Point a,Point b,Point c,Point d,Point&p){
- double s1,s2;
- s1=cross(a,b,c);
- s2=cross(a,b,d);
- if(sig(s1)==0&&sig(s2)==0) return 2;
- if(sig(s2-s1)==0) return 0;
- p.x=(c.x*s2-d.x*s1)/(s2-s1);
- p.y=(c.y*s2-d.y*s1)/(s2-s1);
- return 1;
- }
- //多边形切割
- //用直线ab切割多边形p,切割后的在向量(a,b)的左侧,并原地保存切割结果
- //如果退化为一个点,也会返回去,此时n为1
- //void polygon_cut(Point*p,int&n,Point a,Point b){
- // static Point pp[maxn];
- // int m=0;p[n]=p[0];
- // for(int i=0;i<n;i++){
- // if(sig(cross(a,b,p[i]))>0) pp[m++]=p[i];
- // if(sig(cross(a,b,p[i]))!=sig(cross(a,b,p[i+1])))
- // lineCross(a,b,p[i],p[i+1],pp[m++]);
- // }
- // n=0;
- // for(int i=0;i<m;i++)
- // if(!i||!(pp[i]==pp[i-1]))
- // p[n++]=pp[i];
- // while(n>1&&p[n-1]==p[0])n--;
- //}
- void polygon_cut(Point*p,int&n,Point a,Point b, Point* pp){
- // static Point pp[maxn];
- int m=0;p[n]=p[0];
- for(int i=0;i<n;i++){
- if(sig(cross(a,b,p[i]))>0) pp[m++]=p[i];
- if(sig(cross(a,b,p[i]))!=sig(cross(a,b,p[i+1])))
- lineCross(a,b,p[i],p[i+1],pp[m++]);
- }
- n=0;
- for(int i=0;i<m;i++)
- if(!i||!(pp[i]==pp[i-1]))
- p[n++]=pp[i];
- while(n>1&&p[n-1]==p[0])n--;
- }
- //---------------华丽的分隔线-----------------//
- //返回三角形oab和三角形ocd的有向交面积,o是原点//
- double intersectArea(Point a,Point b,Point c,Point d){
- Point o(0,0);
- int s1=sig(cross(o,a,b));
- int s2=sig(cross(o,c,d));
- if(s1==0||s2==0)return 0.0;//退化,面积为0
- if(s1==-1) swap(a,b);
- if(s2==-1) swap(c,d);
- Point p[10]={o,a,b};
- int n=3;
- Point pp[maxn];
- polygon_cut(p,n,o,c, pp);
- polygon_cut(p,n,c,d, pp);
- polygon_cut(p,n,d,o, pp);
- double res=fabs(area(p,n));
- if(s1*s2==-1) res=-res;return res;
- }
- //求两多边形的交面积
- double intersectArea(Point*ps1,int n1,Point*ps2,int n2){
- if(area(ps1,n1)<0) reverse(ps1,ps1+n1);
- if(area(ps2,n2)<0) reverse(ps2,ps2+n2);
- ps1[n1]=ps1[0];
- ps2[n2]=ps2[0];
- double res=0;
- for(int i=0;i<n1;i++){
- for(int j=0;j<n2;j++){
- res+=intersectArea(ps1[i],ps1[i+1],ps2[j],ps2[j+1]);
- }
- }
- return res;//assumeresispositive!
- }
-
-
-
-
- double iou_poly(vector<double> p, vector<double> q) {
- Point ps1[maxn],ps2[maxn];
- int n1 = 4;
- int n2 = 4;
- for (int i = 0; i < 4; i++) {
- ps1[i].x = p[i * 2];
- ps1[i].y = p[i * 2 + 1];
-
- ps2[i].x = q[i * 2];
- ps2[i].y = q[i * 2 + 1];
- }
- double inter_area = intersectArea(ps1, n1, ps2, n2);
- double union_area = fabs(area(ps1, n1)) + fabs(area(ps2, n2)) - inter_area;
- double iou = inter_area / union_area;
-
- // cout << "inter_area:" << inter_area << endl;
- // cout << "union_area:" << union_area << endl;
- // cout << "iou:" << iou << endl;
-
- return iou;
- }
- //
- //int main(){
- // double p[8] = {0, 0, 1, 0, 1, 1, 0, 1};
- // double q[8] = {0.5, 0.5, 1.5, 0.5, 1.5, 1.5, 0.5, 1.5};
- // vector<double> P(p, p + 8);
- // vector<double> Q(q, q + 8);
- // iou_poly(P, Q);
- // return 0;
- //}
-
- //int main(){
- // double p[8] = {0, 0, 1, 0, 1, 1, 0, 1};
- // double q[8] = {0.5, 0.5, 1.5, 0.5, 1.5, 1.5, 0.5, 1.5};
- // iou_poly(p, q);
- // return 0;
- //}
其头文件:https://github.com/CAPTAIN-WHU/DOTA_devkit/blob/master/polyiou.h
- //
- // Created by dingjian on 18-2-3.
- //
-
- #ifndef POLYIOU_POLYIOU_H
- #define POLYIOU_POLYIOU_H
-
- #include <vector>
- double iou_poly(std::vector<double> p, std::vector<double> q);
- #endif //POLYIOU_POLYIOU_H
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。