Пример #1
0
def test_safe_bbox():
    print('test single list')
    bbox = [1, 2, 3, 4]
    good_bbox = safe_bbox(bbox)
    print(good_bbox)
    print(good_bbox.shape)
    assert CHECK_EQ_NUMPY(good_bbox, np.array(bbox).reshape((1, 4)))

    print('test list of list of 4 elements')
    bbox = [[1, 2, 3, 4], [5, 6, 7, 8]]
    good_bbox = safe_bbox(bbox)
    print(good_bbox)
    print(good_bbox.shape)
    assert CHECK_EQ_NUMPY(good_bbox, np.array(bbox).reshape((2, 4)))

    print('test (4, ) numpy array')
    bbox = np.array([1, 2, 3, 4])
    good_bbox = safe_bbox(bbox)
    print(bbox.shape)
    print(good_bbox.shape)
    assert CHECK_EQ_NUMPY(good_bbox, np.array(bbox).reshape((1, 4)))

    print('test (N, 4) numpy array')
    bbox = np.random.rand(10, 4)
    good_bbox = safe_bbox(bbox)
    print(bbox.shape)
    print(good_bbox.shape)
    assert CHECK_EQ_NUMPY(good_bbox, bbox)

    ######################################## test failure cases
    bbox = [1, 2, 4]
    try:
        good_bbox = safe_bbox(bbox)
        sys.exit('\nwrong! never should be here\n\n')
    except AssertionError:
        print('the length of list should be 4')

    bbox = [[1, 2, 4], [5, 7, 8]]
    try:
        good_bbox = safe_bbox(bbox)
        sys.exit('\nwrong! never should be here\n\n')
    except AssertionError:
        print('the length of list should be 4')

    bbox = np.array([1, 2, 4]).reshape(3, )
    try:
        good_bbox = safe_bbox(bbox)
        sys.exit('\nwrong! never should be here\n\n')
    except AssertionError:
        print('the numpy array should be columns of 4')

    bbox = np.array([[1, 2, 4], [5, 7, 8]])
    try:
        good_bbox = safe_bbox(bbox)
        sys.exit('\nwrong! never should be here\n\n')
    except AssertionError:
        print('the numpy array should be columns of 4')

    print('\n\nDONE! SUCCESSFUL!!\n')
Пример #2
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
Пример #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 clip_bboxes_TLWH(bboxes_in, im_width, im_height, warning=True, debug=True):
    '''
	this function clips bboxes inside the image boundary

	parameters:     
	   bboxes_in:              TLWH 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_TLWH:    TLWH 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_TLWH(np_bboxes, warning=warning,
                              debug=debug), 'the input bboxes are not good'

    bboxes_TLBR = bbox_TLWH2TLBR(np_bboxes, debug=debug)
    clipped_bboxes_TLBR = clip_bboxes_TLBR(bboxes_TLBR,
                                           im_width,
                                           im_height,
                                           warning=warning,
                                           debug=debug)
    clipped_bboxes_TLWH = bbox_TLBR2TLWH(clipped_bboxes_TLBR,
                                         warning=warning,
                                         debug=debug)
    return clipped_bboxes_TLWH
Пример #5
0
def pts_conversion_back_bbox(pts_array, bboxes_in, debug=True):
    '''
    convert pts in the cropped image to the pts in the original image 

    parameters:
        bboxes_in:      1 X 4 numpy array, TLBR or TLWH format
        pts_array:      2(3) x N numpy array, N should >= 1
    '''
    np_bboxes = safe_bbox(bboxes_in, debug=debug)
    if debug:
        assert is2dptsarray(pts_array) or is2dptsarray_occlusion(pts_array), 'the input points should have shape: 2 or 3 x num_pts vs %d x %s' % (pts_array.shape[0], pts_array.shape[1])
        assert isbbox(np_bboxes), 'the input bounding box is not correct'

    pts_out = pts_array.copy()
    pts_out[0, :] = pts_array[0, :] + np_bboxes[0, 0]
    pts_out[1, :] = pts_array[1, :] + np_bboxes[0, 1]

    return pts_out
Пример #6
0
def bbox_TLWH2TLBR(bboxes_in, debug=True):
	'''
	transform the input bounding box with TLWH format to TLBR format

	parameters:
	    bboxes_in: TLWH 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_TLBR: N X 4 numpy array, TLBR format
	'''
	np_bboxes = safe_bbox(bboxes_in, debug=debug)
	if debug: assert bboxcheck_TLWH(np_bboxes, debug=debug), 'the input bounding box should be TLBR format'

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