Commit 8770d2c2 authored by oscar's avatar oscar

提交计算iou代码

parent f707dc68
......@@ -298,4 +298,189 @@ double CuboidIoU(const std::vector<float> &truth_poses, const std::vector<float>
// double iou_3d = CuboidIoU_once(truth_poses, landmark_poses);
// cout<<"the iou of two cuboid is: "<<iou_3d<<endl;
// return;
//}
\ No newline at end of file
//}
/*
#include <iostream>
#include <vector>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <opencv2/opencv.hpp>
#include "caffe/nms.h"
using namespace std;
using namespace cv;
//cv::Point2f center;
//c++ 多边形求交集
//const int maxn = 300;
const double eps = 1e-8;
int dcmp(double x)
{
if(x > eps) return 1;
return x < -eps ? -1 : 0;
}
struct MyPoint
{
double x, y;
};
double cross(MyPoint a,MyPoint b,MyPoint c) ///叉积
{
return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
MyPoint intersection(MyPoint a,MyPoint b,MyPoint c,MyPoint d)
{
MyPoint p = a;
double t =((a.x-c.x)*(c.y-d.y)-(a.y-c.y)*(c.x-d.x))/((a.x-b.x)*(c.y-d.y)-(a.y-b.y)*(c.x-d.x));
p.x +=(b.x-a.x)*t;
p.y +=(b.y-a.y)*t;
return p;
}
//计算多边形面积
double PolygonArea(MyPoint p[], int n)
{
if(n < 3) return 0.0;
double s = p[0].y * (p[n - 1].x - p[1].x);
p[n] = p[0];
for(int i = 1; i < n; ++ i)
s += p[i].y * (p[i - 1].x - p[i + 1].x);
return fabs(s * 0.5);
}
double CPIA(MyPoint a[], MyPoint b[], int na, int nb)//ConvexPolygonIntersectArea
{
MyPoint p[20], tmp[20];
int tn, sflag, eflag;
a[na] = a[0], b[nb] = b[0];
memcpy(p,b,sizeof(MyPoint)*(nb + 1));
for(int i = 0; i < na && nb > 2; i++)
{
sflag = dcmp(cross(a[i + 1], p[0],a[i]));
for(int j = tn = 0; j < nb; j++, sflag = eflag)
{
if(sflag>=0) tmp[tn++] = p[j];
eflag = dcmp(cross(a[i + 1], p[j + 1],a[i]));
if((sflag ^ eflag) == -2)
tmp[tn++] = intersection(a[i], a[i + 1], p[j], p[j + 1]); ///求交点
}
memcpy(p, tmp, sizeof(MyPoint) * tn);
nb = tn, p[nb] = p[0];
}
if(nb < 3) return 0.0;
return PolygonArea(p, nb);
}
double SPIA(MyPoint a[], MyPoint b[], int na, int nb)///SimplePolygonIntersectArea 调用此函数
{
int i, j;
MyPoint t1[4], t2[4];
double res = 0, num1, num2;
a[na] = t1[0] = a[0], b[nb] = t2[0] = b[0];
for(i = 2; i < na; i++)
{
t1[1] = a[i-1], t1[2] = a[i];
num1 = dcmp(cross(t1[1], t1[2],t1[0]));
if(num1 < 0) swap(t1[1], t1[2]);
for(j = 2; j < nb; j++)
{
t2[1] = b[j - 1], t2[2] = b[j];
num2 = dcmp(cross(t2[1], t2[2],t2[0]));
if(num2 < 0) swap(t2[1], t2[2]);
res += CPIA(t1, t2, 3, 3) * num1 * num2;
}
}
return res;
}
//c++ 多边形求交集
double IOU(const proposal_type & r1, const proposal_type & r2)
{
double inter = intersection_area(r1,r2);
double o = inter / (calcularea(r1) + calcularea(r2) - inter);
return (o >= 0) ? o : 0;
}
double intersection_area(const proposal_type & r1, const proposal_type & r2){
MyPoint p1[10],p2[10];
p1[0].x=r1.x2;
p1[0].y=r1.y2;
p1[1].x=r1.x3;
p1[1].y=r1.y3;
p1[2].x=r1.x4;
p1[2].y=r1.y4;
p1[3].x=r1.x1;
p1[3].y=r1.y1;
p2[0].x=r2.x2;
p2[0].y=r2.y2;
p2[1].x=r2.x3;
p2[1].y=r2.y3;
p2[2].x=r2.x4;
p2[2].y=r2.y4;
p2[3].x=r2.x1;
p2[3].y=r2.y1;
double area = SPIA(p1, p2, 4, 4);
return area;
}
double calcularea(const proposal_type & r){
float d12=sqrt(pow(r.x2-r.x1,2)+pow(r.y2-r.y1,2));
float d14=sqrt(pow(r.x4-r.x1,2)+pow(r.y4-r.y1,2));
float d24=sqrt(pow(r.x2-r.x4,2)+pow(r.y2-r.y4,2));
float d32=sqrt(pow(r.x2-r.x3,2)+pow(r.y2-r.y3,2));
float d34=sqrt(pow(r.x3-r.x4,2)+pow(r.y3-r.y4,2));
float p1=(d12+d14+d24)/2;
float p2=(d24+d32+d34)/2;
float s1=sqrt(p1*(p1-d12)*(p1-d14)*(p1-d24));
float s2=sqrt(p2*(p2-d32)*(p2-d34)*(p2-d24));
return s1+s2;
}
bool cmpr(const proposal_type &a,const proposal_type &b){
return a.score > b.score;
}
void nms(vector<proposal_type>& proposals, const double nms_threshold)
{
//按分数排序
sort(proposals.begin(),proposals.end(),cmpr);
//标志,false代表留下,true代表扔掉
vector<bool> del(proposals.size(), false);
for(size_t i = 0; i < proposals.size(); i++){
if(!del[i]){
// std::cout<<scores[i]<<std::endl;
for(size_t j = i+1; j < proposals.size()-1; j++){
if(!del[j] && IOU(proposals[i], proposals[j]) > nms_threshold){
del[j] = true;//IOU大于阈值,扔掉
}
}
}
}
/*for(int i=0;i<del.size();i++){
cout<<del[i]<<" ";
}*/
vector<proposal_type> new_proposals;
/*for(const auto i : index){
if(!del[i]) new_proposals.push_back(proposals[i]);
}*/
for (int i = 0; i < proposals.size(); i++) {
if (!del[i]) new_proposals.push_back(proposals[i]);
}
//cout<<new_proposals.size()<<endl;
proposals.clear();
vector<proposal_type>().swap(proposals);
proposals = new_proposals;
// cout<<proposals.size()<<endl;
// scores.clear();
//vector<float>().swap(scores);
}
*/
\ No newline at end of file
......@@ -4,6 +4,8 @@
#include "Iou.h"
#include "Component.h"
#include "LogBase.h"
#include <opencv2/opencv.hpp>
Track3D::Track3D():BaseTrack(10, 7)
{
......@@ -61,16 +63,62 @@ Track3D::Track3D():BaseTrack(10, 7)
kf_ = kf;
}
float calcIOU(cv::RotatedRect rect1, cv::RotatedRect rect2) {
float areaRect1 = rect1.size.width * rect1.size.height;
float areaRect2 = rect2.size.width * rect2.size.height;
vector<cv::Point2f> vertices;
int intersectionType = cv::rotatedRectangleIntersection(rect1, rect2, vertices);
if (vertices.size() == 0)
return 0.0;
else
{
vector<cv::Point2f> order_pts;
// 找到交集(交集的区域),对轮廓的各个点进行排序
cv::convexHull(cv::Mat(vertices), order_pts, true);
double area = cv::contourArea(order_pts);
float inner = (float)(area / (areaRect1 + areaRect2 - area + 0.0001));
return inner;
}
}
double Track3D::CalculateIou(const std::vector<float>& data)
{
std::vector<float> states;
GetStateData(states);
SDK_LOG(SDK_INFO, "calculate size1 = %d, size2 = %d",states.size(),data.size());
std::vector<float> truth_poses{ states[0], states[1], states[2],0, 0, states[6], states[3]/2, states[4]/2, states[5]/2 };
std::vector<float> landmark_poses{ data[0], data[1], data[2],0, 0, data[6], data[3]/2, data[4]/2, data[5]/2 };
//std::vector<float> truth_poses{ states[0], states[1], states[2],0, 0, states[6], states[3]/2, states[4]/2, states[5]/2 };
//std::vector<float> landmark_poses{ data[0], data[1], data[2],0, 0, data[6], data[3]/2, data[4]/2, data[5]/2 };
cv::RotatedRect rect1 = cv::RotatedRect(cv::Point2f(states[0], states[1]), cv::Size2f(states[3], states[4]), states[6] / 3.1415926 * 180);
cv::RotatedRect rect2 = cv::RotatedRect(cv::Point2f(data[0], data[1]), cv::Size2f(data[3], data[4]), data[6] / 3.1415926 * 180);
uint64_t begin = GetCurTime();
double iou_3d = CuboidIoU(truth_poses, landmark_poses);
//double iou_3d = CuboidIoU(truth_poses, landmark_poses);
double iou_2d = calcIOU(rect1,rect2);
SDK_LOG(SDK_INFO, " iou_2d = %f", iou_2d);
double iou_3d = 0;
if (iou_2d > 0) {
double tru_minz = states[2] - states[5];
double tru_maxz = states[2] + states[5];
double land_minz = data[2] - data[5];
double land_maxz = data[2] + data[5];
if (land_maxz <= tru_maxz && land_maxz >= tru_minz) {
double height_iou = (land_maxz - tru_minz) / (tru_maxz - land_minz);
iou_3d = iou_2d * height_iou;
}
else if (tru_maxz < land_maxz && tru_maxz > land_minz) {
double height_iou = (tru_maxz - land_minz) / (land_maxz - tru_minz);
iou_3d = iou_2d * height_iou;
}
}
SDK_LOG(SDK_INFO, "CuboidIoU time = %llu", GetCurTime() - begin);
return iou_3d;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment