Last active
April 17, 2018 02:03
-
-
Save zgzhong/4b8052af1e5b44c953f4accd29456b21 to your computer and use it in GitHub Desktop.
non maxmium supression
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def nms(boxes, threshold, method): | |
| """ | |
| boxes: [x1, y1, x2, y2, score] | |
| """ | |
| # if input is empty return directly | |
| if boxes.size == 0: | |
| return np.empty(shape=[0]) | |
| x1 = boxes[:, 0] # up-left point | |
| y1 = boxes[:, 1] # of the bounding box | |
| x2 = boxes[:, 2] # bottom-right point | |
| y2 = boxes[:, 3] # of the bounding box | |
| s = boxes[:, 4] # score of the bounding box | |
| # area of the bounding boxes | |
| area = (x2 - x1 + 1) * (y2 - y1 + 1) | |
| # sort by score | |
| I = np.argsort(s) | |
| # initial | |
| pick = np.zeros_like(s, dtype=np.uint16) | |
| counter = 0 | |
| while I.size > 0: | |
| i = I[-1] # hightest score | |
| pick[counter] = i | |
| counter += 1 | |
| idx = I[0:-1] | |
| # area of intersection | |
| xx1 = np.maximum(x1[i], x1[idx]) | |
| yy1 = np.maximum(y1[i], y1[idx]) | |
| xx2 = np.minimum(x2[i], x2[idx]) | |
| yy2 = np.minimum(y2[i], y2[idx]) | |
| w = np.maximum(0.0, xx2 - xx1 + 1) | |
| h = np.maximum(0.0, yy2 - yy1 + 1) | |
| inter = w * h | |
| if method is 'Min': | |
| # inter_area/minium_area | |
| o = inter / np.minimum(area[i], area[idx]) | |
| else: | |
| # inter_area/union_area | |
| o = inter / (area[i] + area[idx] - inter) | |
| I = I[np.where(o <= threshold)] | |
| pick = pick[0:counter] | |
| return pick |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment