c++ 判断人脚位置是否合理

目录

opencv rect版;

自定义BOX_RECT


opencv rect版;

#include <opencv2/core.hpp>

// 计算重叠区域面积
int calculateOverlapArea(const cv::Rect_<float>& a, const cv::Rect_<float>& b) {
    // 找到交集区域的左、右、上、下边界
    int left = std::max(a.x, b.x);
    int right = std::min(a.x + a.width, b.x + b.width);
    int top = std::max(a.y, b.y);
    int bottom = std::min(a.y + a.height, b.y + b.height);

    // 如果没有交集,则返回 0
    if (left >= right || top >= bottom) {
        return 0;
    }

    // 计算交集面积
    int width = right - left;
    int height = bottom - top;
    return width * height;
}

// 计算修改后的 IoU
double calculateModifiedIoU(const cv::Rect_<float>& footBox, const cv::Rect_<float>& bodyBox) {
    // 计算交集区域面积
    int overlapArea = calculateOverlapArea(footBox, bodyBox);

    // 计算脚的面积
    int footBoxArea = footBox.width * footBox.height;

    // 返回修改后的 IoU,即交集面积除以脚的面积
    if (footBoxArea > 0) {
        return static_cast<double>(overlapArea) / footBoxArea;
    }
    else {
        return 0.0; // 避免除以 0
    }
}

// 检查脚是否OK
bool foot_if_ok(const cv::Rect_<float>& footBox, const cv::Rect_<float>& bodyBox) {
    // 计算修改后的 IoU
    double modifiedIoU = calculateModifiedIoU(footBox, bodyBox);

    // 判断是否OK
    bool foot_ok = false;
    if (modifiedIoU > 0.1) {
        int bodyHeight = bodyBox.height;
        int bottomQuarterTop = bodyBox.y + (3 * bodyHeight / 4);

        if (footBox.y >= bottomQuarterTop) {
            foot_ok = true;
        }
    }
    return foot_ok;
}

自定义BOX_RECT

#include <iostream>
#include <algorithm>

typedef struct _BOX_RECT {
    int left;
    int right;
    int top;
    int bottom;
} BOX_RECT;

int calculateOverlapArea(const BOX_RECT& a, const BOX_RECT& b) {
    // 找到交集区域的左、右、上、下边界
    int left = std::max(a.left, b.left);
    int right = std::min(a.right, b.right);
    int top = std::max(a.top, b.top);
    int bottom = std::min(a.bottom, b.bottom);

    // 如果没有交集,则返回 0
    if (left >= right || top >= bottom) {
        return 0;
    }

    // 计算交集面积
    int width = right - left;
    int height = bottom - top;
    return width * height;
}

double calculateModifiedIoU(const BOX_RECT& footBox, const BOX_RECT& bodyBox) {
    // 计算交集区域面积
    int overlapArea = calculateOverlapArea(footBox, bodyBox);

    // 计算脚的 BOX_RECT 面积
    int footBoxArea = (footBox.right - footBox.left) * (footBox.bottom - footBox.top);

    // 返回修改后的 IoU,即交集面积除以脚的面积
    if (footBoxArea > 0) {
        return static_cast<double>(overlapArea) / footBoxArea;
    }
    else {
        return 0.0; // 避免除以 0
    }
}

int main() {
    
    bool foot_ok = false;
    BOX_RECT footBox = { 10, 20, 15, 25 };

    // 定义人体的区域 box
    BOX_RECT bodyBox = { 5, 25, 10, 30 };

    // 计算修改后的 IoU
    double modifiedIoU = calculateModifiedIoU(footBox, bodyBox);

    if (modifiedIoU>0.1) {
    
        int bodyHeight = bodyBox.bottom - bodyBox.top;
        int bottomQuarterTop = bodyBox.bottom - (bodyHeight / 4);

        if (footBox.top >= bottomQuarterTop) {
            foot_ok = true;
        }
    }
    std::cout << "Modified IoU (Foot Box Overlap Proportion): " << modifiedIoU << std::endl;

    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法加油站

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值