手撕IOU,用C++写

拿张纸画个图

#include   
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;



// }

你可能感兴趣的:(c++,算法,开发语言)