我正在尝试创建一个计算两个边界框之间交叉比的函数。我有关于两个矩形的Rect信息。我创建了一个交集函数,它返回两个框之间交叉的双重分数:
double Detection::overlapBB(Rect a, Rect b){
int xa1 = a.x; int xa2 = a.x + a.width;
int ya1 = a.y; int ya2 = a.y + a.height;
int xb1 = b.x; int xb2 = b.x + b.width;
int yb1 = b.y; int yb2 = b.y + b.height;
cout << "The first rectangle:("<< xa1 << "," <<ya1 <<"),(" <<xa2<<"," <<ya2 <<")" <<endl;
cout << "The second rectangle:("<< xb1 << "," <<yb1 <<"),(" <<xb2<<"," <<yb2 <<")" <<endl;
int surfaceA = a.width*a.height;
int surfaceB = b.width*b.height;
int xn1 = Max(xa1, xb1); int yn1 = Max(ya1, yb1);
int xn2 = Min(xa2, xb2); int yn2 = Min(ya2, yb2);
cout << "The second rectangle:("<< xn1 << "," <<yn1 <<"),(" <<xn2<<"," <<yn2 <<")" <<endl;
//int SI = (xn2 - xn1)*(yn2-yn1);
int SI = Max(0, Max(xa2, xb2) - Min(xa1, xb1)) * Max(0, Max(ya2, yb2) - Min(ya1, yb1));
int SU = surfaceA +surfaceB - SI;
cout << SI << " " <<SU << " " <<surfaceA<< " " <<surfaceB << endl;
double intersection = ((double)SI/(double)SU);
return intersection;
}
我注意到,当一个矩形在另一个矩形内时,返回的变量不为真。我可以为完全重叠和部分重叠的情况创建语句吗?