import torch


def transform_matrix_offset_center(matrix, x, y):
    """
    Apply offset to a transform matrix so that the image is transformed about the center of the image.

    Inputs:
    :Tensor matrix: 3x3 transformation matrix
    :int x: height of the image
    :int y: width of the image

    Outputs:
    :Tensor transform_matrix: the shifted 3x3 transform matrix
    """
    o_x = float(x) / 2 + 0.5
    o_y = float(y) / 2 + 0.5
    offset_matrix = torch.DoubleTensor([[1, 0, o_x], [0, 1, o_y], [0, 0, 1]])
    reset_matrix = torch.DoubleTensor([[1, 0, -o_x], [0, 1, -o_y], [0, 0, 1]])
    transform_matrix = torch.mm(torch.mm(offset_matrix, matrix), reset_matrix)
    return transform_matrix