在RCNN系列目標檢測中,有一個重要的算法,用於消除一些冗余的bounding box,這就是non-maximum suppression算法。
這里有一篇博客寫的挺好的:
http://www.cnblogs.com/liekkas0626/p/5219244.html
借用博客里的兩張圖,如有問題,請聯系我刪除。
在目標檢測中,這些bounding box都表示檢測到了人臉,並且會給每一個bounding box一個score,最終我們需要只保留score最大的bounding box(記為bounding box1),將與bounding box1 overlap (重疊) 較大的一些bounding box消除,即只保留在這個局部區域score最高的一個bounding box。可能在一張圖中有多個人臉,那么我們就需要保留各個局部區域score最大的bounding box,兩個bounding box的重疊程度一般用IOU的值作為衡量,
IOU = 兩個bounding box的圍成的區域的交集 / 兩個bounding box圍成區域的並集。
1 function pick = nms(boxes, overlap) 2 % top = nms(boxes, overlap) 3 % Non-maximum suppression. (FAST VERSION) 4 % Greedily select high-scoring detections and skip detections 5 % that are significantly covered by a previously selected 6 % detection. 7 %################################################################ 8 % input ----- boxes: object detection window 9 % size(boxes) = (n,5) 10 % n --- the number of window 11 % (xMin, yMin, xMax, yMax, score) 12 % overlap: suppression threshold 13 % 14 % output ----- pick:the index of reserved window 15 if isempty(boxes) 16 pick = []; 17 return; 18 end 19 20 x1 = boxes(:,1); 21 y1 = boxes(:,2); 22 x2 = boxes(:,3); 23 y2 = boxes(:,4); 24 s = boxes(:,end); 25 26 % calculate the area of all detections 27 area = (x2-x1+1) .* (y2-y1+1); 28 % vals is the sorted elments in ascending order, I is the corresponding index 29 [vals, I] = sort(s); 30 31 pick = s*0; 32 counter = 1; 33 while ~isempty(I) 34 last = length(I); 35 i = I(last); 36 pick(counter) = i; 37 counter = counter + 1; 38 39 xx1 = max(x1(i), x1(I(1:last-1))); 40 yy1 = max(y1(i), y1(I(1:last-1))); 41 xx2 = min(x2(i), x2(I(1:last-1))); 42 yy2 = min(y2(i), y2(I(1:last-1))); 43 44 w = max(0.0, xx2-xx1+1); 45 h = max(0.0, yy2-yy1+1); 46 47 inter = w.*h; 48 o = inter ./ (area(i) + area(I(1:last-1)) - inter); 49 50 I = I(find(o<=overlap)); 51 end 52 53 pick = pick(1:(counter-1));