preprocessing.py
Go to the documentation of this file.
00001 # vim: expandtab:ts=4:sw=4
00002 import numpy as np
00003 import cv2
00004 
00005 
00006 def non_max_suppression(boxes, max_bbox_overlap, scores=None):
00007     """Suppress overlapping detections.
00008 
00009     Original code from [1]_ has been adapted to include confidence score.
00010 
00011     .. [1] http://www.pyimagesearch.com/2015/02/16/
00012            faster-non-maximum-suppression-python/
00013 
00014     Examples
00015     --------
00016 
00017         >>> boxes = [d.roi for d in detections]
00018         >>> scores = [d.confidence for d in detections]
00019         >>> indices = non_max_suppression(boxes, max_bbox_overlap, scores)
00020         >>> detections = [detections[i] for i in indices]
00021 
00022     Parameters
00023     ----------
00024     boxes : ndarray
00025         Array of ROIs (x, y, width, height).
00026     max_bbox_overlap : float
00027         ROIs that overlap more than this values are suppressed.
00028     scores : Optional[array_like]
00029         Detector confidence score.
00030 
00031     Returns
00032     -------
00033     List[int]
00034         Returns indices of detections that have survived non-maxima suppression.
00035 
00036     """
00037     if len(boxes) == 0:
00038         return []
00039 
00040     boxes = boxes.astype(np.float)
00041     pick = []
00042 
00043     x1 = boxes[:, 0]
00044     y1 = boxes[:, 1]
00045     x2 = boxes[:, 2] + boxes[:, 0]
00046     y2 = boxes[:, 3] + boxes[:, 1]
00047 
00048     area = (x2 - x1 + 1) * (y2 - y1 + 1)
00049     if scores is not None:
00050         idxs = np.argsort(scores)
00051     else:
00052         idxs = np.argsort(y2)
00053 
00054     while len(idxs) > 0:
00055         last = len(idxs) - 1
00056         i = idxs[last]
00057         pick.append(i)
00058 
00059         xx1 = np.maximum(x1[i], x1[idxs[:last]])
00060         yy1 = np.maximum(y1[i], y1[idxs[:last]])
00061         xx2 = np.minimum(x2[i], x2[idxs[:last]])
00062         yy2 = np.minimum(y2[i], y2[idxs[:last]])
00063 
00064         w = np.maximum(0, xx2 - xx1 + 1)
00065         h = np.maximum(0, yy2 - yy1 + 1)
00066 
00067         overlap = (w * h) / area[idxs[:last]]
00068 
00069         idxs = np.delete(
00070             idxs, np.concatenate(
00071                 ([last], np.where(overlap > max_bbox_overlap)[0])))
00072 
00073     return pick


jsk_perception
Author(s): Manabu Saito, Ryohei Ueda
autogenerated on Tue Jul 2 2019 19:41:07