手撕IOU,用C++写/python写

拿张纸画个图

#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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值