matlab 非极大值抑制
时间: 2023-12-06 09:38:47 浏览: 131
以下是MATLAB中的非极大值抑制的实现方法:
```matlab
function [bboxes, scores] = nonMaxSuppression(boxes, scores, overlapThresh)
% boxes: 检测框坐标,格式为[x1, y1, x2, y2]
% scores: 检测框得分
% overlapThresh: 重叠率阈值
% bboxes: 非极大值抑制后的检测框坐标
% scores: 非极大值抑制后的检测框得分
% 计算检测框面积
areas = (boxes(:, 3) - boxes(:, 1) + 1) .* (boxes(:,4) - boxes(:, 2) + 1);
% 按照得分从大到小排序
[~, order] = sort(scores, 'descend');
% 保留的检测框的索引
keep = [];
while ~isempty(order)
% 取出得分最高的检测框
i = order(1);
keep = [keep; i];
% 计算当前检测框与其他检测框的重叠率
xx1 = max(boxes(i, 1), boxes(order(2:end), 1));
yy1 = max(boxes(i, 2), boxes(order(2:end), 2));
xx2 = min(boxes(i, 3), boxes(order(2:end), 3));
yy2 = min(boxes(i, 4), boxes(order(2:end), 4));
w = max(0, xx2 - xx1 + 1);
h = max(0, yy2 - yy1 + 1);
overlap = w .* h ./ areas(order(2:end));
% 保留重叠率小于阈值的检测框
inds = find(overlap < overlapThresh);
order = order(inds + 1);
end
bboxes = boxes(keep, :);
scores = scores(keep);
```
阅读全文