拿张纸画个图
#include <iostream>
using namespace std;
// double iou(int rect1[4], int rect2[4] ){
// if (rect1[0] == 0 || rect1[1] == 0 || rect1[2] == 0 || rect1[3] == 0 || rect2[0] == 0 || rect2[1] == 0 || rect2[2] == 0 || rect2[3] == 0)
// return 0 ;
// }
double iou(int rect1[4], int rect2[4]) {
if (rect1[0] == 0 || rect1[1] == 0 || rect1[2] == 0 || rect1[3] == 0 ||
rect2[0] == 0 || rect2[1] == 0 || rect2[2] == 0 || rect2[3] == 0)
return 0;
int x1 = rect1[0];
int y1 = rect1[1];
int x2 = rect1[2];
int y2 = rect1[3];
int a1 = rect2[0];
int b1 = rect2[1];
int a2 = rect2[2];
int b2 = rect2[3];
double s1 = (x2 - x1)*(y2 - y1);
double s2 = (a2 - a1)*(b2 - b1);
int AX = max(x1, a1);
int AY = max(y1, b1);
int BX = min(x2, a2);
int BY = min(y2, b2);
int w = BX - AX;
int h = BY - AY;
if (w <= 0 || h <= 0) return 0;
double S = w * h;
double B = S / (s1 + s2 - S);
cout << "交集面积是:" << S << endl;
cout << "交集面积:" << S << endl;
return B;
}
void test() {
int rect1[4] = {2, 1, 4, 3};
int rect2[4] = {1, 2, 3, 4};
cout << iou(rect1, rect2) << endl;
}
int main() {
test();
return 0;
}
// int main(){
// test();
// return 0;
// }
def compute_iou(rect1,rect2):
x1 = rect1[0]
y1 = rect1[1]
x2 = rect1[2]
y2 = rect1[3]
#是第一个框
a1 = rect2[0]
b1 = rect2[1]
a2 = rect2[2]
b2 = rect2[3]
#是第二个框
S1 = (x2 - x1) * (y2 -y1)
S2 = (a2 - a1) * (b2 - b1)
#交集的框
AX = max(x1,a1)
AY = max(y1,b1)
BX = min(x2,a2)
BY = min(y2,b2)
w = BX - AX
h = BY - AY
S = w * h
if AX >=BX or AY>=BY:
return 0
iou = S / (S1+S2-S)
return iou
if __name__ =="__main__":
# rect1 = [12,23,34,56]
# rect2 = [23,45,21,34]
rect1=(663, 28, 683, 48)
rect2=(663, 28, 683, 48)
iou = compute_iou(rect1,rect2)
print(iou)
# if __name__ =="__main__":
# print(iou)