safesight-edge/plugins/logic_gate/spatial_matcher.h

59 lines
1.7 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#include "frame/frame.h"
#include <vector>
namespace rk3588 {
// 空间关系类型
enum class SpatialRelation {
BELOW, // 目标在锚点下方
ABOVE, // 目标在锚点上方
INSIDE, // 目标在锚点内部
OVERLAP, // 目标与锚点重叠
NEAR // 目标在锚点附近
};
// 空间匹配配置
struct SpatialConfig {
SpatialRelation relation = SpatialRelation::BELOW;
float iou_threshold = 0.3f; // IOU阈值
float foot_ratio = 0.3f; // 脚下区域比例(人体高度)
float expand_ratio = 1.0f; // 框扩大系数
float max_distance = 0.5f; // 最大归一化距离NEAR模式用
};
// 匹配结果
struct SpatialMatchResult {
bool matched = false;
float iou = 0.0f;
float distance = 0.0f; // 归一化距离
Rect foot_region; // 脚下区域(用于可视化)
};
class SpatialMatcher {
public:
SpatialMatcher(const SpatialConfig& config);
// 检查目标是否在锚点的指定空间位置
SpatialMatchResult Match(const Detection& anchor, const Detection& target,
int img_w, int img_h);
// 计算脚下区域
Rect CalculateFootRegion(const Rect& person_bbox, int img_w, int img_h);
private:
SpatialConfig config_;
// 计算两个框的IOU
float CalculateIoU(const Rect& a, const Rect& b);
// 计算归一化中心点距离
float CalculateDistance(const Rect& anchor, const Rect& target, int img_w, int img_h);
// 检查目标是否在锚点下方
bool IsBelow(const Rect& anchor, const Rect& target, int img_w, int img_h);
};
} // namespace rk3588