算法本质
搜索局部最大值,而抑制非极大值元素。
例子
3邻域请款下的NMS算法的实现过程,即找到相邻3个元素中中间最大的元素。
数学表达式:
数组
arr[1,w]
,存在
1<i<w
使得如下公式成立:
arr[i−1]<=arr[i]>arr[i+1]
则 i <script type="math/tex" id="MathJax-Element-195">i</script>为1个非极大值抑制点
NMS在物体检测中的应用
目标检测过程中为了消除多余的窗口, 找到最佳的目标检测位置。、
MATLAB 代码
function pickLocate = nms(boxes, overlap)
% Non-maximum suppression.
% In object detect algorithm, select high score detections and skip windows
% covered by a previously selected detection.
%
% input - boxes : object detect windows.
% xMin yMin xMax yMax score.
% overlap : suppression threshold.
% output - pickLocate : number of local maximum score.
boxes = double(boxes);
if isempty(boxes)
pickLocate = [];
else
xMin = boxes(:, 1);
yMin = boxes(:, 2);
xMax = boxes(:, 3);
yMax = boxes(:, 4);
s = boxes(:, end);
% area of every detected windows.
area = (xMax - xMin + 1) .* (yMax - yMin + 1);
% sort detected windows based on the score.
[vals, I] = sort(s);
pickLocate = [];
while ~isempty(I)
last = length(I);
i = I(last);
pickLocate = [pickLocate; i];
suppress = [last];
for pos = 1 : last - 1
j = I(pos);
% covered area.
xx1 = max(xMin(i), xMin(j));
yy1 = max(yMin(i), yMin(j));
xx2 = min(xMax(i), xMax(j));
yy2 = min(yMax(i), yMax(j));
w = xx2 - xx1 + 1;
h = yy2 - yy1 + 1;
if ((w > 0) && (h > 0))
% compute overlap.
o = w * h / min(area(i), area(j));
if (o > overlap)
suppress = [suppress; pos];
end
end
% xx1 = max(x1(i), x1(I(1:last-1)));
% yy1 = max(y1(i), y1(I(1:last-1)));
% xx2 = min(x2(i), x2(I(1:last-1)));
% yy2 = min(y2(i), y2(I(1:last-1)));
% w = max(0.0, xx2-xx1+1);
% h = max(0.0, yy2-yy1+1);
% inter = w.*h;
% o = inter ./ (area(i) + area(I(1:last-1)) - inter);
% saving the windows which o less than threshold.
% I = I(o <= overlap);
end
I(suppress) = [];
end
end
主要摘录于非极大值抑制算法