Exemplo n.º 1
0
def deconvolution_demo(first_img):
    """
    Computes the spatially-variant focus parameters and deconvolve the image
    :param first_img: input image
    """
    model.train(False)
    step = 64
    psf_array_linear = []
    image_masked_array_bilinear=[]

    # Get focus map
    downscaled = first_img[128:-128, 128:-128]
    #downscaled = scale(downscale_local_mean(downscaled, (3, 3)))

    img, _ = live_moving_window(downscaled, step)
    img_downsampled = img[::step, ::step]
    output_filtered = [scipy.ndimage.filters.median_filter(img_downsampled[:,:,i], size=(2,2), mode='reflect') for i in range(img.shape[2])]
    output_filtered_scaled = [downscale_local_mean(output_filtered[i], (2,2)) for i in range(len(output_filtered))]
    flattened_map = [output_filtered_scaled[i].flatten() for i in range(len(output_filtered_scaled))]

    # Synthesize kernels
    for i in range(0, flattened_map[0].shape[0]):
        new_psf = gaussian_kernel(63, flattened_map[0][i], flattened_map[1][i])
        psf_array_linear.append(new_psf)
    psf_array_linear = np.asarray(psf_array_linear)

    # Deconvolve
    compute_grid(output_filtered_scaled[0],downscaled)
    grid_z1 = load_grid(psf_array_linear.shape[0])
    for i, current_psf in enumerate(psf_array_linear):
        log.info('Detected PSF {} with focus x {} y {}'.format(i, flattened_map[0][i],flattened_map[1][i]))
        image_masked_array_bilinear.append(np.multiply(grid_z1[i], downscaled))

    deconvolved = rl_deconv_all(image_masked_array_bilinear, psf_array_linear, iterations=20, lbd=0.1)

    # Figures
    io.imsave('original.png', scale(downscaled))
    io.imsave('deconvolved.png', scale(deconvolved))
    fig, ax = plt.subplots()
    ax1 = plt.subplot(2, 2, 1)
    ax2 = plt.subplot(2, 2, 3)
    ax3 = plt.subplot(2, 2, 2)
    ax4 = plt.subplot(2, 2, 4)

    im1 = ax1.imshow(downscaled)
    ax1.set_title('Original')
    im2 = ax2.imshow(output_filtered_scaled[0][:,:], vmin=0.5, vmax=4)
    ax2.set_title('Detected FWMH X (px)')
    im3 = ax3.imshow(deconvolved)
    ax3.set_title('Deconvolved RL TV')
    im4 = ax4.imshow(output_filtered_scaled[1][:,:], vmin=0.5, vmax=4)
    ax4.set_title('Detected FWMH Y (px)')
    plt.show()
Exemplo n.º 2
0
def bin_binary(mat, binary_dim=10):
    step = 1 / binary_dim
    if mat.ndim == 1:  # If the matrix is a vector
        mat = mat[..., np.newaxis]  # Transform into a matrix
    normalized_mat = data_utils.scale(mat, [0, 1])
    enc_input_data = np.repeat(np.zeros(mat.shape), binary_dim, axis=-1)
    for i_binary in range(binary_dim):
        enc_input_data[..., i_binary::binary_dim] = (
            normalized_mat > i_binary * step) * (normalized_mat <
                                                 (i_binary + 1) * step)
    return enc_input_data
Exemplo n.º 3
0
def live_moving_window(im, step=64):
    """
    Computes the focus paramter map on the input image using a moving window
    :param im: input impate
    :param step: resolution of the moving average window
    :return:
    """
    size = 128
    num_classes = 2
    x = size
    y = size
    im = im[0:im.shape[0] // size * size, 0:im.shape[1] // size * size]
    weight_image = np.zeros((im.shape[0], im.shape[1], num_classes))

    tile_dataset = []
    y_size = 0
    i = 0

    while x <= im.shape[0]:
        x_size = 0
        while y <= im.shape[1]:
            a = im[x - size:x, y - size:y]
            a = scale(a)
            tile_dataset.append(a[:])
            weight_image[x - size:x, y - size:y] += 1.0
            y += step
            x_size += 1
            i += 1
        y = size
        y_size += 1
        x += step

    tile_dataset = np.asarray(tile_dataset)
    tile_dataset = np.reshape(tile_dataset,
                              (tile_dataset.shape[0], 1, size, size))
    input_tensor = FloatTensor(tile_dataset)
    out = model(Variable(input_tensor).cuda())
    output_npy = out.data.cpu().numpy()
    output = np.zeros((im.shape[0], im.shape[1], output_npy.shape[1]))

    i = 0
    x = size
    y = size
    while x <= im.shape[0]:
        while y <= im.shape[1]:
            output[x - size:x, y - size:y] += output_npy[i, :]
            y += step
            i += 1
        y = size
        x += step

    output = output / weight_image
    return output, output_npy