import torch


def coordinates_to_flow_field(coordinates):
    """
    This function shift the coordinates to a flow field
    :param coordinates: tensor with size [h, w, 2], coordinates starts from 0
    :return: a tensor that has the same size with coordinates, contain the flow
            filed. Values: x: -1, y: -1 is the left-top pixel of the input,
            and values: x: 1, y: 1 is the right-bottom pixel of the input.
    """
    coordinates = torch.Tensor(coordinates)
    h, w = coordinates.size()[0], coordinates.size()[1]
    half_h, half_w = h / 2, w / 2

    coordinates[:, :, 0] = (coordinates[:, :, 0]) / half_w # x
    coordinates[:, :, 1] = (coordinates[:, :, 1]) / half_h # y

    return coordinates