def apply_rotation_tight(bbox_in, angle_in_degree, im_shape, debug=True): ''' return 4 points clockwise ''' if debug: assert isnparray(bbox_in) and bbox_in.size == 4, 'box is not correct' bbox_in = np.reshape(bbox_in, (4, )) bbox_tight = bbox_rotation_inv(bbox_in, angle_in_degree, im_shape, debug=debug) # get top left and bottom right coordinate of the rotated bbox in the image coordinate # print('bbox after inverse the rotation') # print(bbox_tight) pts_total = np.zeros((4, 2), dtype=np.int) pts_tl = np.array([bbox_tight[0], bbox_tight[1]]) pts_br = np.array([bbox_tight[2], bbox_tight[3]]) line1 = get_line(imagecoor2cartesian(pts_tl, debug=debug), angle_in_degree + 90.00, debug=debug) line2 = get_line(imagecoor2cartesian(pts_br, debug=debug), angle_in_degree, debug=debug) pts_bl = cartesian2imagecoor(get_intersection(line1, line2, debug=debug), debug=debug) pts_tr = cartesian2imagecoor(get_intersection(get_line(imagecoor2cartesian(pts_tl, debug=debug), angle_in_degree, debug=debug), get_line(imagecoor2cartesian(pts_br, debug=debug), angle_in_degree + 90.00, debug=debug), debug=debug), debug=debug) # print np.reshape(pts_tl, (1, 2)).shape # print pts_total[0, :].shape pts_total[0, :] = np.reshape(pts_tl, (1, 2)) pts_total[1, :] = np.reshape(pts_tr, (1, 2)) pts_total[2, :] = np.reshape(pts_br, (1, 2)) pts_total[3, :] = np.reshape(pts_bl, (1, 2)) return pts_total
def bbox_rotatedtight2rotatedloose(bbox_in, angle_in_degree, debug=True): ''' transfer the rotated bbox with tight version to loose version, both contains only two points (top left and bottom right) only a single box is feeded into ''' if debug: assert isnparray(bbox_in) and bbox_in.size == 4, 'box is not correct' pts_tl = np.array([bbox_in[0], bbox_in[1]]) pts_br = np.array([bbox_in[2], bbox_in[3]]) line1 = get_line(imagecoor2cartesian(pts_tl), angle_in_degree + 90.00) line2 = get_line(imagecoor2cartesian(pts_br), angle_in_degree) pts_bl = cartesian2imagecoor(get_intersection(line1, line2)) pts_tr = cartesian2imagecoor(get_intersection(get_line(imagecoor2cartesian(pts_tl), angle_in_degree), get_line(imagecoor2cartesian(pts_br), angle_in_degree + 90.00))) # assert_almost_equal(np.dot(pts_bl - pts_br, pts_bl - pts_tl), 0, err_msg='The intersection points are wrong') # assert_almost_equal(np.dot(pts_tr - pts_br, pts_tr - pts_tl), 0, err_msg='The intersection points are wrong') pts_tl_final = np.zeros((2), dtype=np.float32) pts_br_final = np.zeros((2), dtype=np.float32) pts_tl_final[0] = min({pts_tl[0], pts_br[0], pts_bl[0], pts_tr[0]}) pts_tl_final[1] = min({pts_tl[1], pts_br[1], pts_bl[1], pts_tr[1]}) pts_br_final[0] = max({pts_tl[0], pts_br[0], pts_bl[0], pts_tr[0]}) pts_br_final[1] = max({pts_tl[1], pts_br[1], pts_bl[1], pts_tr[1]}) # print(pts_tl_final) # print(pts_br_final) test = np.hstack((pts_tl_final, pts_br_final)) return test
def bbox2center(bbox, 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 ''' if debug: assert bboxcheck_TLBR(bbox), 'the input bounding box should be TLBR format' num_bbox = bbox.shape[0] center = np.zeros((num_bbox, 2), dtype='float32') center[:, 0] = (bbox[:, 0] + bbox[:, 2]) / 2. center[:, 1] = (bbox[:, 1] + bbox[:, 3]) / 2. if vis: fig = plt.figure() plt.scatter(bbox[0, 0], -bbox[0, 1], color='b') # -1 is to convert the coordinate from image to cartesian plt.scatter(bbox[0, 2], -bbox[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)
def pts2bbox(pts, debug=True, vis=False): ''' convert a set of 2d points to a bounding box parameter: pts: 2 x N numpy array, N should >= 2 return: bbox: 1 x 4 numpy array, TLBR format ''' if debug: assert is2dptsarray(pts) or is2dptsarray_occlusion(pts), 'the input points should have shape: 2 or 3 x num_pts vs %d x %s' % (pts.shape[0], pts.shape[1]) assert pts.shape[1] >= 2, 'number of points should be larger or equal than 2' bbox = np.zeros((1, 4), dtype='float32') bbox[0, 0] = np.min(pts[0, :]) # x coordinate of left top point bbox[0, 1] = np.min(pts[1, :]) # y coordinate of left top point bbox[0, 2] = np.max(pts[0, :]) # x coordinate of bottom right point bbox[0, 3] = np.max(pts[1, :]) # y coordinate of bottom right point if vis: fig = plt.figure() pts = imagecoor2cartesian(pts) plt.scatter(pts[0, :], pts[1, :], color='r') plt.scatter(bbox[0, 0], -bbox[0, 1], color='b') # -1 is to convert the coordinate from image to cartesian plt.scatter(bbox[0, 2], -bbox[0, 3], color='b') plt.show() plt.close(fig) return bbox