def flip_bdb3d(bdb3ds, cam_R, cam_R_flip): bdb3ds_flip = deepcopy(bdb3ds) for bdb_idx, bdb3d in enumerate(bdb3ds): centroid_flip = bdb3d['centroid'].dot( cam_R) # transform bdb centroid to camera system centroid_flip[2] = -1 * centroid_flip[2] # flip right-coordinate centroid_flip = centroid_flip.dot( cam_R_flip.T) # transform back to world system vectors_flip = np.diag(bdb3d['coeffs']).dot(bdb3d['basis']).dot( cam_R) # transform vectors to camera system vectors_flip[:, 2] = -1 * vectors_flip[:, 2] # flip right-coordinate vectors_flip = vectors_flip.dot( cam_R_flip.T) # transform back to world system coeffs_flip = np.linalg.norm(vectors_flip, axis=1) basis_flip = np.array( [normalize_point(vector) for vector in vectors_flip]) # keep the basis_flip[2,:] vector, because it stands for the forward direction of an object. basis_flip[0, :] = basis_flip[ 0, :] if np.linalg.det(basis_flip) > 0 else -basis_flip[0, :] bdb3ds_flip[bdb_idx]['basis'] = basis_flip bdb3ds_flip[bdb_idx]['coeffs'] = coeffs_flip bdb3ds_flip[bdb_idx]['centroid'] = centroid_flip return bdb3ds_flip
def process_bdb3d(bdb3ds): ''' transform sunrgbd layout to toward-up-right form in world system. :param layout: sunrgbd layout :return: toward-up-right form. ''' trans_mat = np.array([[0., 1., 0.], [0., 0., 1.], [1., 0., 0.]]) bdb3ds_t = [] for bdb3d in bdb3ds: centroid = trans_mat.dot(bdb3d['centroid'][0]) coeffs = bdb3d['coeffs'][0] basis = bdb3d['basis'].astype('float32') vectors = trans_mat.dot((trans_mat.dot( (np.diag(coeffs).dot(basis)).T)).T) # let z-axis face forward (consistent with suncg data.) vectors = np.array([vectors[2], vectors[1], -vectors[0]]) vectors[0] = vectors[0] if np.linalg.det(vectors) > 0. else -vectors[0] bdb3d_t = {} bdb3d_t['coeffs'] = np.linalg.norm(vectors, axis=1) bdb3d_t['basis'] = np.array( [normalize_point(vector) for vector in vectors]) if np.linalg.det(vectors) <= 0.: continue bdb3d_t['centroid'] = centroid bdb3d_t['class_id'] = get_NYU37_class_id(bdb3d['classname'])[1] bdb3ds_t.append(bdb3d_t) return bdb3ds_t
def flip_layout(layout, cam_R, cam_R_flip): ''' transform and flip sunrgbd layout to toward-up-right form. :param layout: sunrgbd layout :return: toward-up-right form. ''' # layout is the layout coordinates in world system (toward-up-right form). centroid_flip = layout['centroid'].dot( cam_R) # layout centroid in camera system centroid_flip[2] = -1 * centroid_flip[2] # flip right-coordinate values centroid_flip = centroid_flip.dot( cam_R_flip.T) # transform back to world system vectors_flip = np.diag(layout['coeffs']).dot(layout['basis']).dot( cam_R) # layout vectors in camera system vectors_flip[:, 2] = -1 * vectors_flip[:, 2] # flip right-coordinate values vectors_flip = vectors_flip.dot( cam_R_flip.T) # transform back to world system coeffs_flip = np.linalg.norm(vectors_flip, axis=1) basis_flip = np.array([normalize_point(vector) for vector in vectors_flip]) basis_flip[2, :] = basis_flip[ 2, :] if np.linalg.det(basis_flip) > 0 else -basis_flip[2, :] bdb_flip = {} bdb_flip['basis'] = basis_flip bdb_flip['coeffs'] = coeffs_flip bdb_flip['centroid'] = centroid_flip return bdb_flip
def get_layout_bdb_from_corners(layout_t): ''' get coeffs, basis, centroid from corners :param corners: 8x3 numpy array corners of a 3D bounding box [toward, up, right] coordinates :return: bounding box parameters ''' y_max = layout_t[:, 1].max() y_min = layout_t[:, 1].min() points_2d = layout_t[layout_t[:, 1] == y_max, :] points_2d = points_2d[np.argsort(points_2d[:, 0]), :] vector1 = points_2d[3] - points_2d[1] vector2 = points_2d[1] - points_2d[0] coeff1 = np.linalg.norm(vector1) coeff2 = np.linalg.norm(vector2) vector1 = normalize_point(vector1) vector2 = np.cross(vector1, [0, 1, 0]) centroid = np.array([ points_2d[0, 0] + points_2d[3, 0], float(y_max) + float(y_min), points_2d[0, 2] + points_2d[3, 2] ]) * 0.5 basis = np.array([vector1, [0, 1, 0], vector2]) coeffs = np.array([coeff1, y_max - y_min, coeff2]) * 0.5 assert np.linalg.det(basis) > 0. bdb = {'centroid': centroid, 'basis': basis, 'coeffs': coeffs} return bdb