def init_NNF(source_image):
    """
    Return a matrix (im_shape[0] x im_shape[1] x 2) representing a random displacement field.
    """

    # get the shape of the source image
    im_shape = source_image.shape

    #
    # Generate a matrix f of size (im_shape[0] x im_shape[1] x 2) that
    # assigns random X and Y displacements to each pixel
    #

    # We first generate a matrix of random x coordinates
    x = np.random.randint(low=0,
                          high=im_shape[1],
                          size=(im_shape[0], im_shape[1]))
    # Then we generate a matrix of random y coordinates
    y = np.random.randint(low=0,
                          high=im_shape[0],
                          size=(im_shape[0], im_shape[1]))
    # To create matrix f, we stack those two matrices
    f = np.dstack((y, x))

    #
    # Now we generate a matrix g of size (im_shape[0] x im_shape[1] x 2)
    # such that g(y,x) = [y,x]
    #
    g = make_coordinates_matrix(im_shape)

    # define the NNF to be the difference of these two matrices
    f = f - g

    return f
Exemplo n.º 2
0
def create_NNF_vectors_image(source, target, f, patch_size,
                             server=True,
                             subsampling=100,
                             line_width=0.5,
                             line_color='k',
                             tmpdir='./'):
    """
    Display the nearest-neighbour field as a sparse vector field between source and target images
    """
    import matplotlib.pyplot as plt

    # get the shape of the source image
    im_shape = source.shape

    # if you are using matplotlib on a server
    if server:
        plt.switch_backend('agg')
    import matplotlib.patches as patches

    fig = plt.figure(frameon=False)
    ax = plt.Axes(fig, [0., 0., 1., 1.])
    ax.set_axis_off()
    fig.add_axes(ax)

    source = cv.cvtColor(source, cv.COLOR_BGR2RGB)
    target = cv.cvtColor(target, cv.COLOR_BGR2RGB)

    # create an image that contains the source and target side by side
    plot_im = np.concatenate((source, target), axis=1)
    ax.imshow(plot_im)

    vector_coords = make_coordinates_matrix(im_shape, step=subsampling)
    vshape = vector_coords.shape
    vector_coords = np.reshape(vector_coords, (vshape[0] * vshape[1], 2))

    for coord in vector_coords:
        rect = patches.Rectangle((coord[1] - patch_size / 2.0, coord[0] - patch_size / 2.0),
                                 patch_size,
                                 patch_size,
                                 linewidth=line_width,
                                 edgecolor=line_color,
                                 facecolor='none')
        ax.add_patch(rect)

        arrow = patches.Arrow(coord[1],
                              coord[0],
                              f[coord[0], coord[1], 1] + im_shape[1],
                              f[coord[0], coord[1], 0],
                              lw=line_width,
                              edgecolor=line_color)
        ax.add_patch(arrow)

    dpi = fig.dpi
    fig.set_size_inches(im_shape[1] * 2 / dpi, im_shape[0] / dpi)
    tmp_image = tmpdir+'/tmpvecs.png' \

    fig.savefig(tmp_image)
    plt.close(fig)
    return tmp_image