1. 程式人生 > >非最大抑制(NMS)

非最大抑制(NMS)

<span style="font-family: 'Microsoft YaHei';">轉之:</span>

http://blog.csdn.net/H2008066215019910120/article/details/25917609#

非極大值抑制(NMS)

      非極大值抑制顧名思義就是抑制不是極大值的元素,搜尋區域性的極大值。這個區域性代表的是一個鄰域,鄰域有兩個引數可變,一是鄰域的維數,二是鄰域的大小。這裡不討論通用的NMS演算法,而是用於在目標檢測中用於提取分數最高的視窗的。例如在行人檢測中,滑動視窗經提取特徵,經分類器分類識別後,每個視窗都會得到一個分數。但是滑動視窗會導致很多視窗與其他視窗存在包含或者大部分交叉的情況。這時就需要用到NMS來選取那些鄰域裡分數最高(是行人的概率最大),並且抑制那些分數低的視窗。

程式碼如下

function pick = nms(boxes, overlap)
 
% pick = nms(boxes,overlap)
% Non-maximumsuppression.
% Greedily selecthigh-scoring detections and skip detections
% that are significantlycovered by a previously selected detection.
 
% boxes = boxes';
 
if isempty(boxes)
    pick = [];
else
    x1 = boxes(:,1);
    y1 = boxes(:,2);
    x2 = boxes(:,3);
    y2 = boxes(:,4);
%     x1 = boxes(:,1);
%     y1 = boxes(:,2);
%     x2 = boxes(:,2);
%     y2 = boxes(:,4);
    s = boxes(:,end);
    area = (x2-x1+1) .* (y2-y1+1);%Çó³öËùÓÐÃæ»ý
   
    [vals, I] = sort(s);
    pick = [];
    while ~isempty(I)
        last = length(I);
        i = I(last);
        pick = [pick; i];
        suppress = [last];
        for pos = 1:last-1
            j = I(pos);
            xx1 = max(x1(i), x1(j));
            yy1 = max(y1(i), y1(j));
            xx2 = min(x2(i), x2(j));
            yy2 = min(y2(i), y2(j));
            w = xx2-xx1+1;
            h = yy2-yy1+1;
            if w > 0 && h > 0
                % compute overlap
                o = w * h / min(area(i),area(j));
%                 o = w/area(j);
                if o > overlap
                    suppress = [suppress; pos];
                end
            end
        end
        I(suppress) = [];
    end
end

 輸入的視窗的位置和分數,以及視窗面積交叉是多大比例進行抑制。boxes應當是N*5的矩陣,一行代表著一個視窗,包括[x,y,width,height ,score ],overlap是介於0~1的實數。輸出的是區域性分數最大的視窗序號序列。

程式解讀:首先計算出所有視窗的面積,對所有視窗的分數進行從小到大排序取出最高分數的序號。迴圈計算1到次高分數視窗與最高分數視窗的交叉面積與兩者間最小面積的比例,若超過overlap那麼把這一視窗(1~~last中的視窗)抑制了。交叉面積怎麼計算呢?如下圖對應於程式

            xx1 = max(x1(i), x1(j));
            yy1 = max(y1(i), y1(j));
            xx2 = min(x2(i), x2(j));
            yy2 = min(y2(i), y2(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

以下是python部分

http://www.pyimagesearch.com/2015/02/16/faster-non-maximum-suppression-python

# import the necessary packages
import numpy as np
 
# Malisiewicz et al.
def non_max_suppression_fast(boxes, overlapThresh):
	# if there are no boxes, return an empty list
	if len(boxes) == 0:
		return []
 
	# if the bounding boxes integers, convert them to floats --
	# this is important since we'll be doing a bunch of divisions
	if boxes.dtype.kind == "i":
		boxes = boxes.astype("float")
 
	# initialize the list of picked indexes	
	pick = []
 
	# grab the coordinates of the bounding boxes
	x1 = boxes[:,0]
	y1 = boxes[:,1]
	x2 = boxes[:,2]
	y2 = boxes[:,3]
 
	# compute the area of the bounding boxes and sort the bounding
	# boxes by the bottom-right y-coordinate of the bounding box
	area = (x2 - x1 + 1) * (y2 - y1 + 1)
	idxs = np.argsort(y2)
 
	# keep looping while some indexes still remain in the indexes
	# list
	while len(idxs) > 0:
		# grab the last index in the indexes list and add the
		# index value to the list of picked indexes
		last = len(idxs) - 1
		i = idxs[last]
		pick.append(i)
 
		# find the largest (x, y) coordinates for the start of
		# the bounding box and the smallest (x, y) coordinates
		# for the end of the bounding box
		xx1 = np.maximum(x1[i], x1[idxs[:last]])
		yy1 = np.maximum(y1[i], y1[idxs[:last]])
		xx2 = np.minimum(x2[i], x2[idxs[:last]])
		yy2 = np.minimum(y2[i], y2[idxs[:last]])
 
		# compute the width and height of the bounding box
		w = np.maximum(0, xx2 - xx1 + 1)
		h = np.maximum(0, yy2 - yy1 + 1)
 
		# compute the ratio of overlap
		overlap = (w * h) / area[idxs[:last]]
 
		# delete all indexes from the index list that have
		idxs = np.delete(idxs, np.concatenate(([last],
			np.where(overlap > overlapThresh)[0])))
 
	# return only the bounding boxes that were picked using the
	# integer data type
	return boxes[pick].astype("int")