面试题-手撕IOU计算

一般在面试中只会考察二维无旋转矩形框IOU的计算,更复杂的二维框带旋转、三维框等一般不会让写。

二维无旋转矩形框IOU计算

最简单的iou计算, 矩形框,无旋转
2个框的排布可能有下面三种情况,都需要考虑到
在这里插入图片描述

代码

#include <iostream>
#include <algorithm>

struct Rectangle {
    float x1, y1, x2, y2; // 左上角和右下角坐标
};

float intersectionArea(const Rectangle& rect1, const Rectangle& rect2) {
    float overlapWidth = std::max(0.0f, std::min(rect1.x2, rect2.x2) - std::max(rect1.x1, rect2.x1));
    float overlapHeight = std::max(0.0f, std::min(rect1.y2, rect2.y2) - std::max(rect1.y1, rect2.y1));

    return overlapWidth * overlapHeight;
}

float calculateIOU(const Rectangle& rect1, const Rectangle& rect2) {
    float area1 = (rect1.x2 - rect1.x1) * (rect1.y2 - rect1.y1);
    float area2 = (rect2.x2 - rect2.x1) * (rect2.y2 - rect2.y1);

    float intersection = intersectionArea(rect1, rect2);
    float unionArea = area1 + area2 - intersection;

    return intersection / unionArea;
}

int main() {
    Rectangle rect1 = {50, 50, 150, 150}; // 示例矩形1
    Rectangle rect2 = {100, 100, 200, 200}; // 示例矩形2

    float iou = calculateIOU(rect1, rect2);
    std::cout << "IOU: " << iou << std::endl;

    return 0;
}

相关推荐

  1. 面试缓存LRU

    2024-01-07 01:36:02       34 阅读
  2. 面试-NMS(非极大值抑制)

    2024-01-07 01:36:02       62 阅读
  3. 面试:多线程交叉打印ABC

    2024-01-07 01:36:02       46 阅读
  4. 面试】Vue(高频知识点四)

    2024-01-07 01:36:02       24 阅读
  5. 面试】Vue(高频知识点五)

    2024-01-07 01:36:02       35 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-07 01:36:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-07 01:36:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-07 01:36:02       87 阅读
  4. Python语言-面向对象

    2024-01-07 01:36:02       96 阅读

热门阅读

  1. 如何在 Ubuntu 20.04 上设置 SSH 密钥

    2024-01-07 01:36:02       52 阅读
  2. 如何在Windows Redis中设置密码?

    2024-01-07 01:36:02       63 阅读
  3. PHP 平滑重启 kill -SIGUSR2 <PID>

    2024-01-07 01:36:02       57 阅读
  4. Elasticsearch(实践一)相似度方法L1、L2 、cos

    2024-01-07 01:36:02       57 阅读
  5. 探索Elasticsearch内存应用的关键因素

    2024-01-07 01:36:02       56 阅读
  6. C语言中的输入输出详解

    2024-01-07 01:36:02       66 阅读
  7. 第七节 按需导入elementPlus

    2024-01-07 01:36:02       62 阅读
  8. [数理统计]中国科技技术大学缪柏其

    2024-01-07 01:36:02       45 阅读