コード例 #1
0
def batch_displacement_warp2d(imgs, vector_fields):
    """
    warp images by free form transformation

    Parameters
    ----------
    imgs : tf.Tensor
        images to be warped
        [n_batch, xlen, ylen, n_channel]
    vector_fields : tf.Tensor
        [n_batch, 2, xlen, ylen]

    Returns
    -------
    output : tf.Tensor
    warped imagees
        [n_batch, xlen, ylen, n_channel]
    """
    n_batch = tf.shape(imgs)[0]
    xlen = tf.shape(imgs)[1]
    ylen = tf.shape(imgs)[2]

    grids = batch_mgrid(n_batch, xlen, ylen)

    T_g = grids + vector_fields
    output = batch_warp2d(imgs, T_g)
    return output
コード例 #2
0
def batch_affine_warp2d(imgs, theta):
    """
    affine transforms 2d images

    Parameters
    ----------
    imgs : tf.Tensor
        images to be warped
        [n_batch, xlen, ylen, n_channel]
    theta : tf.Tensor
        parameters of affine transformation
        [n_batch, 6]

    Returns
    -------
    output : tf.Tensor
        warped images
        [n_batch, xlen, ylen, n_channel]
    """
    n_batch = tf.shape(imgs)[0]
    xlen = tf.shape(imgs)[1]
    ylen = tf.shape(imgs)[2]
    theta = tf.reshape(theta, [-1, 2, 3])
    matrix = tf.slice(theta, [0, 0, 0], [-1, -1, 2])
    t = tf.slice(theta, [0, 0, 2], [-1, -1, -1])

    grids = batch_mgrid(n_batch, xlen, ylen)
    coords = tf.reshape(grids, [n_batch, 2, -1])

    T_g = tf.batch_matmul(matrix, coords) + t
    T_g = tf.reshape(T_g, [n_batch, 2, xlen, ylen])
    output = batch_warp2d(imgs, T_g)
    return output
コード例 #3
0
def batch_displacement_warp2d(imgs,
                              vector_fields,
                              vector_fields_in_pixel_space=False):
    """
    warp images by free form transformation

    Parameters
    ----------
    imgs : tf.Tensor
        images to be warped
        [n_batch, xlen, ylen, n_channel]
    vector_fields : tf.Tensor
        [n_batch, xlen, ylen, 2]
    vector_fields_in_pixel_space: boolean
        If vector_fields_in_pixel_space, it means that the displacements
        in the vector field are expressed in pixels.
        Therefore, they will be rescaled from imagen domain
        to [-1.,1.][-1.,1.] to make it compatible with the convention used
        by the warper.

    Returns
    -------
    output : tf.Tensor
    warped imagees
        [n_batch, xlen, ylen, n_channel]
    """
    # Transpose the vector field to [n_batch, 2, xlen, ylen]
    vector_fields_transposed = tf.transpose(vector_fields, [0, 3, 1, 2])

    n_batch = tf.shape(imgs)[0]
    xlen = tf.shape(imgs)[1]
    ylen = tf.shape(imgs)[2]

    grids = batch_mgrid(n_batch, xlen, ylen)

    if vector_fields_in_pixel_space:
        # Scale the vector field from image domain to [-1.,1.][-1.,1.]
        vector_fields_transposed_rescaled = tf.stack(
            [(2. * vector_fields_transposed[:, 0, :, :]) /
             (tf.to_float(xlen) - 1.),
             (2. * vector_fields_transposed[:, 1, :, :]) /
             (tf.to_float(ylen) - 1.)], 1)
        T_g = grids + vector_fields_transposed_rescaled
    else:
        T_g = grids + vector_fields_transposed

    return batch_warp2d(imgs, T_g)