예제 #1
0
def clip_bboxes_TLBR(bboxes_in, im_width, im_height, warning=True, debug=True):
    '''
	this function clips bboxes inside the image boundary, the coordinates in the clipped bbox are half-included [x, y)

	parameters:     
	   bboxes_in:              TLBR format, a list of 4 elements, a listoflist of 4 elements: e.g., [[1,2,3,4], [5,6,7,8]], 
	                           a numpy array with shape or (N, 4) or (4, )
	   im_width/im_height:     scalar

	outputs:        
	   clipped_bboxes:    TLBR format, numpy array with shape of (N, 4)
	'''
    np_bboxes = safe_bbox(bboxes_in, warning=warning, debug=debug)
    if debug:
        assert isinteger(im_width) and isinteger(
            im_height), 'the image width and height are not correct'
        assert bboxcheck_TLBR(np_bboxes, warning=warning,
                              debug=debug), 'the input bboxes are not good'

    clipped_bboxes = np.zeros_like(np_bboxes)
    clipped_bboxes[:, 0] = np.maximum(np.minimum(np_bboxes[:, 0], im_width),
                                      0)  # x1 >= 0 & x1 <= width, included
    clipped_bboxes[:, 1] = np.maximum(np.minimum(np_bboxes[:, 1], im_height),
                                      0)  # y1 >= 0 & y1 <= height, included
    clipped_bboxes[:, 2] = np.maximum(np.minimum(np_bboxes[:, 2], im_width),
                                      0)  # x2 >= 0 & x2 <= width, not included
    clipped_bboxes[:,
                   3] = np.maximum(np.minimum(np_bboxes[:, 3], im_height),
                                   0)  # y2 >= 0 & y2 <= height, not included
    return clipped_bboxes
예제 #2
0
def bbox_enlarge(bbox, ratio=0.2, width_ratio=None, height_ratio=None, min_length=128, min_width=None, min_height=None, debug=True):
    '''
    enlarge the bbox around the edge

    parameters:
        bbox:   N X 4 numpy array, TLBR format
        ratio:  how much to enlarge, for example, the ratio=0.2, then the width and height will be increased by 0.2 times of original width and height
    '''

    if debug:
        assert bboxcheck_TLBR(bbox), 'the input bounding box should be TLBR format'

    if width_ratio is not None and height_ratio is not None:
        width = (bbox[:, 2] - bbox[:, 0]) * width_ratio
        height = (bbox[:, 3] - bbox[:, 1]) * height_ratio
    else:
        width = (bbox[:, 2] - bbox[:, 0]) * ratio
        height = (bbox[:, 3] - bbox[:, 1]) * ratio

    cur_width = bbox[:, 2] - bbox[:, 0]
    cur_height = bbox[:, 3] - bbox[:, 1]
    if min_height is not None and min_width is not None:
        width = max(width, min_width - cur_width)
        height = max(height, min_height - cur_height)
    else:
        width = max(width, min_length - cur_width)
        height = max(height, min_length - cur_height)

    bbox[:, 0] -= width / 2.0
    bbox[:, 2] += width / 2.0
    bbox[:, 3] += height / 2.0
    bbox[:, 1] -= height / 2.0

    return bbox
예제 #3
0
def bbox2center(bboxes_in, debug=True, vis=False):
    '''
    convert a bounding box to a point, which is the center of this bounding box

    parameter:
        bbox:   N x 4 numpy array, TLBR format

    return:
        center: 2 x N numpy array, x and y correspond to first and second row respectively
    '''
    np_bboxes = safe_bbox(bboxes_in, debug=debug)
    if debug:
        assert bboxcheck_TLBR(
            np_bboxes), 'the input bounding box should be TLBR format'

    num_bbox = np_bboxes.shape[0]
    center = np.zeros((num_bbox, 2), dtype='float32')
    center[:, 0] = (np_bboxes[:, 0] + np_bboxes[:, 2]) / 2.
    center[:, 1] = (np_bboxes[:, 1] + np_bboxes[:, 3]) / 2.

    # if vis:
    #     fig = plt.figure()
    #     plt.scatter(np_bboxes[0, 0], -np_bboxes[0, 1], color='b')         # -1 is to convert the coordinate from image to cartesian
    #     plt.scatter(np_bboxes[0, 2], -np_bboxes[0, 3], color='b')
    #     center_show = imagecoor2cartesian(center)
    #     plt.scatter(center_show[0], center_show[1], color='r')
    #     plt.show()
    #     plt.close(fig)
    return np.transpose(center)
예제 #4
0
def bbox_TLBR2TLWH(bboxes_in, debug=True):
	'''
	transform the input bounding box with TLBR format to TLWH format

	parameters:
	    bboxes_in: TLBR format, a list of 4 elements, a listoflist of 4 elements: e.g., [[1,2,3,4], [5,6,7,8]], 
	                a numpy array with shape or (N, 4) or (4, )

	outputs: 
	    bbox_TLWH: N X 4 numpy array, TLWH format
	'''
	np_bboxes = safe_bbox(bboxes_in, debug=debug)
	if debug: assert bboxcheck_TLBR(np_bboxes, debug=debug), 'the input bounding box should be TLBR format'

	bbox_TLWH = np.zeros_like(np_bboxes)
	bbox_TLWH[:, 0] = np_bboxes[:, 0]
	bbox_TLWH[:, 1] = np_bboxes[:, 1]
	bbox_TLWH[:, 2] = np_bboxes[:, 2] - np_bboxes[:, 0]
	bbox_TLWH[:, 3] = np_bboxes[:, 3] - np_bboxes[:, 1]
	return bbox_TLWH