non_maximum_suppression.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 from jsk_perception.srv import *
00004 from jsk_recognition_msgs.msg import Rect
00005 import rospy
00006 import numpy as np
00007 
00008 # Malisiewicz et al.
00009 def non_max_suppression_fast(boxes, overlapThresh):
00010         if len(boxes) == 0:
00011                 return []        
00012         if boxes.dtype.kind == "i":
00013                 boxes = boxes.astype("float")
00014         pick = []
00015         x1 = boxes[:,0]
00016         y1 = boxes[:,1]
00017         x2 = boxes[:,2]
00018         y2 = boxes[:,3]
00019         area = (x2 - x1 + 1) * (y2 - y1 + 1)
00020         idxs = np.argsort(y2)
00021         while len(idxs) > 0:
00022                 last = len(idxs) - 1
00023                 i = idxs[last]
00024                 pick.append(i)
00025                 xx1 = np.maximum(x1[i], x1[idxs[:last]])
00026                 yy1 = np.maximum(y1[i], y1[idxs[:last]])
00027                 xx2 = np.minimum(x2[i], x2[idxs[:last]])
00028                 yy2 = np.minimum(y2[i], y2[idxs[:last]])
00029                 w = np.maximum(0, xx2 - xx1 + 1)
00030                 h = np.maximum(0, yy2 - yy1 + 1)
00031                 overlap = (w * h) / area[idxs[:last]]
00032                 idxs = np.delete(idxs, np.concatenate(([last],
00033                         np.where(overlap > overlapThresh)[0])))
00034         return boxes[pick].astype("int")
00035 
00036 def non_max_suppression_equator(rect, threshold):
00037         _box = []
00038         for box in rect:
00039                 x1 = box.x 
00040                 x2 = x1 + box.width
00041                 y1 = box.y
00042                 y2 = y1 + box.height
00043                 r = (x1, y1, x2, y2)
00044                 _box.append(r)
00045         bbox = np.array(_box)
00046         return non_max_suppression_fast(bbox, threshold);
00047 
00048 def non_max_suppression_handler(req):
00049         pick = non_max_suppression_equator(req.rect, req.threshold)
00050         bbox = []
00051         i = 0
00052         while i < len(pick):
00053                 bx = pick[i][0]
00054                 by = pick[i][1]
00055                 bw = pick[i][2] - bx
00056                 bh = pick[i][3] - by
00057                 bbox.append(Rect(bx, by, bw, bh))
00058                 #print Rect(bx, by, bw, bh)
00059                 i+=1    
00060         return NonMaximumSuppressionResponse(bbox, len(pick))
00061 
00062 
00063 def non_max_suppression_server():
00064         rospy.init_node('non_maximum_suppression_server')
00065         s = rospy.Service('non_maximum_suppression',
00066                           NonMaximumSuppression, non_max_suppression_handler)
00067         print "DEBUG: Bounding Box NMS...."
00068         rospy.spin()
00069 
00070 if __name__ == "__main__":
00071         non_max_suppression_server()


jsk_perception
Author(s): Manabu Saito, Ryohei Ueda
autogenerated on Sun Oct 8 2017 02:43:23