目录
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;
}