跳转到内容
新建笔记

深度神经网络模块

OpenCV: Deep Neural Network module

函数描述代码示例
NMSBoxes()
(1)NMSBoxes() & NMSBoxesBatched()
跳转到“(1)NMSBoxes() & NMSBoxesBatched()”

给出一张图片和上面许多物体检测的候选框(即每个框可能都代表某种物体),但是这些框很可能有互相重叠的部分,我们要做的就是只保留最优的框。假设有N个框,每个框被分类器计算得到的分数为Si, 1<=i<=N。

算法流程:

0)建造一个存放待处理候选框的集合H,初始化为包含全部N个框;建造一个存放最优框的集合M,初始化为空集。 1)将所有集合 H 中的框进行排序,选出分数最高的框 m,从集合 H 移到集合 M; 2)遍历集合 H 中的框,分别与框 m 计算交并比(Interection-over-union,IoU),如果高于某个阈值(一般为0~0.5),则认为此框与 m 重叠,将此框从集合 H 中去除。 3)回到第1步进行迭代,直到集合 H 为空。集合 M 中的框为我们所需。

bboxes: 这是一组需要应用非最大抑制的边界框。边界框通常是矩形框,表示为(x, y, width, height)的形式,其中(x, y)是框的左上角坐标,width和height分别是框的宽度和高度。

scores: 这是与bboxes中每个边界框相对应的置信度分数集合。这些分数表示模型对每个边界框包含目标对象的信心程度。

score_threshold: 这是一个阈值,用于过滤掉那些置信度低于此阈值的边界框。只有置信度高于或等于这个阈值的边界框才会被考虑进行NMS。

nms_threshold: 这是在非最大抑制过程中使用的阈值。它决定了在抑制重叠边界框时,哪些框应该被保留或被删除。

indices: 这是NMS操作后保留的边界框的索引列表。这些索引指向原始bboxes列表中被保留的边界框。

eta: 这是一个系数,用于自适应阈值公式nms_threshold_i+1=eta⋅nms_threshold_inms\_threshold\_{i+1} = eta\cdot nms\_threshold\_i。这个系数允许NMS阈值根据每个边界框的置信度进行调整。

top_k: 如果设置为大于0的值,表示在NMS操作后最多保留top_k个边界框。这可以用于进一步减少边界框的数量,只保留最有可能包含目标对象的框。

void cv::dnn::NMSBoxes(const std::vector<Rect>& bboxes,
const std::vector<float>& scores,
const float score_threshold,
const float nms_threshold,
std::vector<int>& indices,
const float eta = 1.f,
const int top_k = 0
);
void cv::dnn::NMSBoxes(const std::vector<Rect2d>& bboxes, const std::vector<float>& scores, const float score_threshold, const float nms_threshold, std::vector<int>& indices, const float eta = 1.f, const int top_k = 0);
void cv::dnn::NMSBoxes(const std::vector<RotatedRect>& bboxes, const std::vector<float>& scores, const float score_threshold, const float nms_threshold, std::vector<int>& indices, const float eta = 1.f, const int top_k = 0);
void cv::dnn::NMSBoxesBatched(const std::vector<Rect>& bboxes, const std::vector<float>& scores, const std::vector<int>& class_ids, const float score_threshold, const float nms_threshold, std::vector<int>& indices, const float eta = 1.f, const int top_k = 0);
void cv::dnn::NMSBoxesBatched(const std::vector<Rect2d>& bboxes, const std::vector<float>& scores, const std::vector<int>& class_ids, const float score_threshold, const float nms_threshold, std::vector<int>& indices, const float eta = 1.f, const int top_k = 0);
cv.dnn.NMSBoxes(bboxes, scores, score_threshold, nms_threshold[, eta[, top_k]]) -> indices
cv.dnn.NMSBoxesRotated(bboxes, scores, score_threshold, nms_threshold[, eta[, top_k]]) -> indices
cv.dnn.NMSBoxesBatched(bboxes, scores, class_ids, score_threshold, nms_threshold[, eta[, top_k]]) -> indices