计算两个边界框集合的iou
1 import numpy as np 2 3 4 def iou(gtboxes, dtboxes): 5 '''numpy version of calculating IoU between two set of 2D bboxes. 6 7 Args: 8 gtboxes (np.ndarray): Shape (B,4) of .., 4 present [x1,y1,x2,y2] 9 dtboxes,np.ndarray,shape:(N,4), 4 present [x1,y1,x2,y2]. 10 11 Returns: 12 np.ndarray: Shape (B,N) . 13 ''' 14 gtboxes = gtboxes[:, np. 15 newaxis, :] #converse gtboxes:(B,4) to gtboxes:(B,1,4) 16 ixmin = np.maximum(gtboxes[:, :, 0], dtboxes[:, 0]) 17 iymin = np.maximum(gtboxes[:, :, 1], dtboxes[:, 1]) 18 ixmax = np.minimum(gtboxes[:, :, 2], dtboxes[:, 2]) 19 iymax = np.minimum(gtboxes[:, :, 3], dtboxes[:, 3]) 20 intersection = (ixmax - ixmin + 1) * (iymax - iymin + 1) 21 union = (gtboxes[:,:,2]-gtboxes[:,:,0]+1)*(gtboxes[:,:,3]-gtboxes[:,:,1]+1) 22 +(dtboxes[:,2]-dtboxes[:,0]+1)*(dtboxes[:,3]-dtboxes[:,1]+1)-intersection 23 return intersection / union