alignMajor.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:pytorch_fnet 作者: AllenCellModeling 项目源码 文件源码
def get_align_angles(img, axes="zyx"):
    """
    Returns the angles needed to rotate an image to align it with the specified axes
    :param img: A CZYX image as a 4d numpy array. The image that will be measured to get the
    alignment angles. The image will not be altered by this function
    :param axes: string, that must be an arrangement of 'xyz'
    The major axis will be aligned with the first one, the minor with the last one.
    'zyx' by default
    :return: A list of tuple pairs, containing the axis indices and angles to rotate along the paired
    axis. Meant to be passed into align_major
    """
    if getattr(img, 'ndim', 0) < 3:
        raise ValueError('img must be at least a 3d numpy array')
    axis_map = {'x': 0, 'y': 1, 'z': 2}
    if not isinstance(axes, str) or len(axes) != 3 or not all(a in axis_map for a in axes):
        raise ValueError("axes must be an arrangement of 'xyz'")
    # axes parameter string turned into a list of indices
    axis_list = [axis_map[a] for a in axes]
    maj_axis_i = axis_list[0]
    # unit vectors for x, y, and z axis
    axis_vectors = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
    # slices for selecting yz, xz, and xy components from vectors
    slices = (slice(1, 3), slice(0, 3, 2), slice(0, 2))
    # index of the major axis (0, 1, or 2)
    maj_axis = axis_vectors[axis_list[0]]
    min_axis = axis_vectors[axis_list[-1]]
    img_maj_axis, img_min_axis = get_major_minor_axis(img)
    angles = []
    for a in range(3):
        if a != maj_axis_i:
            # rotate around other two axis (e.g if aligning major to Z axis, rotate around Y and X to get there)
            angle = angle_between(maj_axis[slices[a]], img_maj_axis[slices[a]])
            angles.append([a, -angle])
            img_maj_axis = np.dot(_get_rotation_matrix(a, angle), img_maj_axis)
            img_min_axis = np.dot(_get_rotation_matrix(a, angle), img_min_axis)
    # final rotation goes around major axis to align the minor axis properly
    # has to be done last
    angles.append([maj_axis_i, angle_between(min_axis[slices[maj_axis_i]], img_min_axis[slices[maj_axis_i]])])
    return angles
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号