Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace the np.rot90 with affine transforms in apply_affine() #100

Closed
edyoshikun opened this issue Sep 19, 2023 · 1 comment · Fixed by #102
Closed

replace the np.rot90 with affine transforms in apply_affine() #100

edyoshikun opened this issue Sep 19, 2023 · 1 comment · Fixed by #102

Comments

@edyoshikun
Copy link
Contributor

The current code used the np.rot90(), which can now be replaced by properly multiplying the transforms. These will also be useful for properly cropping to the maximum overlapping area.

rotate90_affine = utils.rotate_affine(
    (Z_source, Y_source, X_source), 90, (Z_source, X_source, Y_source)
)
rotate_cam_rot_affine = utils.rotate_affine(
    (Z_source, X_source, Y_source), starting_angle, (Z_target, Y_target, X_target)
)
scaling_affine = utils.scale_affine((1, Y_target, X_target), scaling_factor_zyx)
zyx_translation_affine = np.array(
    [
        [1, 0, 0, shift[-3]],
        [0, 1, 0, shift[-2]],
        [0, 0, 1, shift[-1]],
        [0, 0, 0, 1],
    ]
)

compound_affine = (
    zyx_translation_affine @ scaling_affine @ rotate_cam_rot_affine @ rotate90_affine
)

By adding these transformations:

def scale_affine(start_shape_zyx, scaling_factor_zyx=(1, 1, 1), end_shape_zyx=None):
    center_Y_start, center_X_start = np.array(start_shape_zyx)[-2:] / 2
    if end_shape_zyx is None:
        center_Y_end, center_X_end = (center_Y_start, center_X_start)
    else:
        center_Y_end, center_X_end = np.array(end_shape_zyx)[-2:] / 2

    scaling_matrix = np.array(
        [
            [scaling_factor_zyx[-3], 0, 0, 0],
            [
                0,
                scaling_factor_zyx[-2],
                0,
                -center_Y_start * scaling_factor_zyx[-2] + center_Y_end,
            ],
            [
                0,
                0,
                scaling_factor_zyx[-1],
                -center_X_start * scaling_factor_zyx[-1] + center_X_end,
            ],
            [0, 0, 0, 1],
        ]
    )
    return scaling_matrix


def rotate_affine(start_shape_zyx, angle=0.0, end_shape_zyx=None):
    # TODO: make this 3D?
    center_Y_start, center_X_start = np.array(start_shape_zyx)[-2:] / 2
    if end_shape_zyx is None:
        center_Y_end, center_X_end = (center_Y_start, center_X_start)
    else:
        center_Y_end, center_X_end = np.array(end_shape_zyx)[-2:] / 2

    theta = np.radians(angle)

    rotation_matrix = np.array(
        [
            [1, 0, 0, 0],
            [
                0,
                np.cos(theta),
                -np.sin(theta),
                -center_Y_start * np.cos(theta)
                + np.sin(theta) * center_X_start
                + center_Y_end,
            ],
            [
                0,
                np.sin(theta),
                np.cos(theta),
                -center_Y_start * np.sin(theta)
                - center_X_start * np.cos(theta)
                + center_X_end,
            ],
            [0, 0, 0, 1],
        ]
    )

    affine_rot_n_scale_matrix_zyx = rotation_matrix

    return affine_rot_n_scale_matrix_zyx
@edyoshikun
Copy link
Contributor Author

this will be fixed by #102

@ieivanov ieivanov linked a pull request Dec 14, 2023 that will close this issue
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant