Exemplo n.º 1
0
def test_inpaint_biharmonic_2d_color_deprecated():
    img = img_as_float(data.astronaut()[:64, :64])

    mask = np.zeros(img.shape[:2], dtype=bool)
    mask[8:16, :16] = 1
    img_defect = img * ~mask[..., np.newaxis]
    mse_defect = mean_squared_error(img, img_defect)

    # providing multichannel argument positionally also warns
    channel_warning = "`multichannel` is a deprecated argument"
    matrix_warning = "the matrix subclass is not the recommended way"
    with expected_warnings([channel_warning + '|' + matrix_warning]):
        img_restored = inpaint.inpaint_biharmonic(img_defect,
                                                  mask,
                                                  multichannel=True)
    mse_restored = mean_squared_error(img, img_restored)

    assert mse_restored < 0.01 * mse_defect

    # providing multichannel argument positionally also warns
    channel_warning = "Providing the `multichannel` argument"
    with expected_warnings([channel_warning + '|' + matrix_warning]):
        img_restored = inpaint.inpaint_biharmonic(img_defect, mask, True)
    mse_restored = mean_squared_error(img, img_restored)

    assert mse_restored < 0.01 * mse_defect
Exemplo n.º 2
0
def test_inpaint_biharmonic_3d():
    img = np.tile(np.square(np.linspace(0, 1, 5)), (5, 1))
    img = np.dstack((img, img.T))
    mask = np.zeros_like(img)
    mask[2, 2:, :] = 1
    mask[1, 3:, :] = 1
    mask[0, 4:, :] = 1
    img[np.where(mask)] = 0
    out = inpaint.inpaint_biharmonic(img, mask)
    ref = np.dstack(
        (
            np.array(
                [
                    [0.0000, 0.0625, 0.25000000, 0.56250000, 0.53752796],
                    [0.0000, 0.0625, 0.25000000, 0.44443780, 0.53762210],
                    [0.0000, 0.0625, 0.23693666, 0.46621112, 0.68615592],
                    [0.0000, 0.0625, 0.25000000, 0.56250000, 1.00000000],
                    [0.0000, 0.0625, 0.25000000, 0.56250000, 1.00000000],
                ]
            ),
            np.array(
                [
                    [0.0000, 0.0000, 0.00000000, 0.00000000, 0.19621902],
                    [0.0625, 0.0625, 0.06250000, 0.17470756, 0.30140091],
                    [0.2500, 0.2500, 0.27241289, 0.35155440, 0.43068654],
                    [0.5625, 0.5625, 0.56250000, 0.56250000, 0.56250000],
                    [1.0000, 1.0000, 1.00000000, 1.00000000, 1.00000000],
                ]
            ),
        )
    )
    assert_allclose(ref, out)
def infill_small_regions(I):
    """Infill small nan regions within an image using a tiling approach to save
    on memory.

    Arguments:
        I: the image

    Returns:
        The infilled image.
    """
    n_tiles = 4 # ntiles horizontally.
    assert I.shape[0] == I.shape[1]
    tile_size = I.shape[0] // (n_tiles - 1)
    tile_delta = tile_size // 2

    k = 0
    I_stack = np.ones(I.shape + (2, 2)) * np.nan
    for j in range(n_tiles * 2 - 1):
        for i in range(n_tiles * 2 - 1):
            dy = slice(tile_delta * j, tile_delta * (j + 2))
            dx = slice(tile_delta * i, tile_delta * (i + 2))
            S = I[dy, dx]
            M = ndimage.binary_dilation(np.isnan(S), iterations=2)
            image_inpainted = inpaint.inpaint_biharmonic(S, M, multichannel=False)
            I_stack[dy, dx, j % 2, i % 2] = image_inpainted
            k += 1
    return np.nanmean(np.nanmean(I_stack, axis=2), axis=2)
def process_image(extend_img):

    print('yes')
    mask = np.where(np.isnan(extend_img), 255, 0)
    img_test = np.where(np.isnan(extend_img), 0, extend_img)
    extend_img = inpaint.inpaint_biharmonic(img_test, mask, multichannel=False)
    print('done')
    return extend_img
def visualize_natural_color_bi(product, B):
    #reference: https://stackoverflow.com/questions/42872293/channel-mix-with-pillow
    global product_name
    name = product_name + ".png"
    #read radiance channels
    # rads = product.load_radiances()
    # s1 = rads["S1_radiance_an"].values
    # s3 = rads["S3_radiance_an"].values
    # s5 = rads["S5_radiance_an"].values
    refs = product.load_reflectances()
    s1 = refs["S1_reflectance_an"].values * 255
    s3 = refs["S3_reflectance_an"].values * 255
    s5 = refs["S5_reflectance_an"].values * 255

    #----------test--------------
    start_time = time.time()
    row, col = s1.shape
    s1 = s1[5:row - 5, 100:col - 80]
    #fill s1 NaNs
    mask1 = numpy.isnan(s1)
    f_s1 = inpaint.inpaint_biharmonic(s1, mask1)
    print("--- %s seconds ---" % (time.time() - start_time))

    start_time = time.time()
    s3 = s3[5:row - 5, 100:col - 80]
    #fill s3 NaNs
    mask3 = numpy.isnan(s3)
    f_s3 = inpaint.inpaint_biharmonic(s3, mask3)
    print("--- %s seconds ---" % (time.time() - start_time))

    start_time = time.time()
    s5 = s5[5:row - 5, 100:col - 80]
    #fill s5 NaNs
    mask5 = numpy.isnan(s5)
    f_s5 = inpaint.inpaint_biharmonic(s5, mask5)
    print("--- %s seconds ---" % (time.time() - start_time))
    #-----------------------------

    # # Transform channel data
    r, g, b = (f_s3 + f_s5) / 2, f_s3, (f_s3 + f_s1) / 2

    # Merge channels
    out_data = numpy.stack((r, g, b), axis=2).astype('uint8')
    out_img = Image.fromarray(out_data)
    out_img.save(B + "/" + "origin/" + name, "PNG", optimize=True)
Exemplo n.º 6
0
 def _inpainting_fill(self, img, mask):
     """ 
     
     """
     
     img[np.where(mask ==1)] =1
     image_result = inpaint.inpaint_biharmonic(img, mask,
                                       multichannel=False)
     return image_result
Exemplo n.º 7
0
def process_cloud(cloud):
    # start = time.time()
    # remove all non ground points; gnd labels = [40, 44, 48, 49]
    # gnd, obs = segment_cloud(cloud,[40, 44, 48, 49])
    gnd, obs = segment_cloud(cloud,[40, 44, 48, 49,60,72])
    visualize = True

    if visualize:
        fig.clear()
        voxel_size = 1
        grid_size = np.array([-50, -50, 50, 50])
        gnd_img = lidar_to_img(np.copy(gnd), grid_size, voxel_size, fill = 1)
        gnd_heightmap = lidar_to_heightmap(np.copy(gnd), grid_size, voxel_size, max_points = 100)

        fig.add_subplot(2, 3, 1)
        plt.imshow(gnd_img, interpolation='nearest')
        kernel = np.ones((5,5),np.uint8)
        gnd_img_dil = cv2.dilate(gnd_img,kernel,iterations = 2)
        fig.add_subplot(2, 3, 2)
        plt.imshow(gnd_img_dil, interpolation='nearest')
        mask = gnd_img_dil - gnd_img
        fig.add_subplot(2, 3, 3)
        plt.imshow(mask, interpolation='nearest')
        # mask = mask.clip(min=0)
        fig.add_subplot(2, 3, 4)
        cs = plt.imshow(gnd_heightmap, interpolation='nearest')
        cbar = fig.colorbar(cs)

        image_result = inpaint.inpaint_biharmonic(gnd_heightmap, mask)
        fig.add_subplot(2, 3, 5)
        cs = plt.imshow(image_result, interpolation='nearest')
        cbar = fig.colorbar(cs)

        image_result = signal.convolve2d(image_result, kernel, boundary='wrap', mode='same')/kernel.sum()
        # image_result = inpaint.inpaint_biharmonic(image_result, mask)
        # image_result = cv2.dilate(image_result,kernel,iterations = 1)

        # kernel = np.array([[0,1,0],
        #                    [1,0,1],
        #                    [0,1,0]])
        # kernel = np.ones((7,7),np.uint8)
        # kernel[3,3] = 0
        # ind = mask == 1

        # for i in range(10):
        #     conv_out = signal.convolve2d(gnd_heightmap, kernel, boundary='wrap', mode='same')/kernel.sum()
        #     gnd_heightmap[ind] = conv_out[ind]

        fig.add_subplot(2, 3, 6)
        cs = plt.imshow(image_result, interpolation='nearest')
        cbar = fig.colorbar(cs)
        plt.show()
        # cbar.remove()

        seg = semantically_segment_cloud(cloud.copy(), grid_size, voxel_size, image_result)

    return gnd, image_result.T, seg
Exemplo n.º 8
0
def solve_frame(image):
    h, w, c = image.shape
    wt_size = 128

    mask = np.zeros((h, w), dtype=np.uint8)
    mask[h-10-wt_size:h-10, 10:10+wt_size] = 1

    # res = cv2.inpaint(image, mask, 3, cv2.INPAINT_TELEA)
    res = inpaint.inpaint_biharmonic(image, mask, multichannel=True)

    return res
def pseudo_healthy_with_texture(scan_slice,
                                lesions_all,
                                coords_all,
                                masks_all,
                                names_all,
                                texture,
                                iter_erosion_dilation=1,
                                plot=False,
                                Tp=20):
    '''1. Read all clusters' masks in a lesion (a cluster is a part of a lesion obtained with slic) 
    2. Replace them for the synthetic healthy texture. 3. Make a mask ring on the outer perimeter of
    the mask and inpaint to blend the image '''
    slice_healthy = copy(scan_slice)
    mask_for_inpain = np.zeros_like(slice_healthy)
    Tp = 20
    for idx_x, (lesion, coord, mask, name) in enumerate(
            zip(lesions_all, coords_all, masks_all, names_all)):
        coords_big = [int(i) for i in name.split('_')[1:5]]
        coords_sums = coord + coords_big
        # print('LOADING: ', coord, coords_big, coords_sums[0], coords_sums[2], name)
        new_coords_mask = np.where(mask == 1)[0] + coords_sums[0], np.where(
            mask == 1)[1] + coords_sums[2]
        slice_healthy[new_coords_mask] = texture[new_coords_mask]
        # rings to inpaint
        mask_closed = binary_fill_holes(mask, )
        mask_in = (mask_closed).astype('int') - binary_erosion(
            mask_closed, iterations=iter_erosion_dilation)
        mask_out = binary_dilation(
            mask_closed,
            iterations=iter_erosion_dilation) - (mask_closed).astype('int')
        mask_ring = mask_in + mask_out
        new_coords_mask_inpain = np.where(
            mask_ring == 1)[0] + coords_sums[0], np.where(
                mask_ring == 1)[1] + coords_sums[
                    2]  # mask outer rings for inpaint
        mask_for_inpain[new_coords_mask_inpain] = 1
    slice_healthy_inpain = inpaint.inpaint_biharmonic(slice_healthy,
                                                      mask_for_inpain)

    if plot:
        fig, ax = plt.subplots(1, 3, figsize=(18, 6))
        ax[0].imshow(scan_slice[coords_big[0] - Tp:coords_big[1] + Tp,
                                coords_big[2] - Tp:coords_big[3] + Tp])
        ax[0].imshow(scan_mask_slice[coords_big[0] - Tp:coords_big[1] + Tp,
                                     coords_big[2] - Tp:coords_big[3] + Tp],
                     alpha=.3)
        ax[1].imshow(slice_healthy[coords_big[0] - Tp:coords_big[1] + Tp,
                                   coords_big[2] - Tp:coords_big[3] + Tp])
        ax[2].imshow(
            slice_healthy_inpain[coords_big[0] - Tp:coords_big[1] + Tp,
                                 coords_big[2] - Tp:coords_big[3] + Tp])

    return slice_healthy_inpain
Exemplo n.º 10
0
def test_inpaint_nrmse(dtype, order, channel_axis, split_into_regions):
    image_orig = data.astronaut()[:, :200]
    float_dtype = np.float32 if dtype == np.float32 else np.float64
    image_orig = image_orig.astype(float_dtype, copy=False)

    # Create mask with six block defect regions
    mask = np.zeros(image_orig.shape[:-1], dtype=bool)
    mask[20:50, 3:20] = 1
    mask[165:180, 90:155] = 1
    mask[40:60, 170:195] = 1
    mask[-60:-40, 170:195] = 1
    mask[-180:-165, 90:155] = 1
    mask[-50:-20, :20] = 1

    # add a few long, narrow defects
    mask[200:205, -200:] = 1
    mask[150:255, 20:22] = 1
    mask[365:368, 60:130] = 1

    # add randomly positioned small point-like defects
    rstate = np.random.default_rng(0)
    for radius in [0, 2, 4]:
        # larger defects are less common
        thresh = 3.25 + 0.25 * radius  # larger defects less commmon
        tmp_mask = rstate.standard_normal(image_orig.shape[:-1]) > thresh
        if radius > 0:
            tmp_mask = binary_dilation(tmp_mask, disk(radius, dtype=bool))
        mask[tmp_mask] = 1

    # Defect image over the same region in each color channel
    image_defect = image_orig.copy()
    for layer in range(image_defect.shape[-1]):
        image_defect[np.where(mask)] = 0

    if channel_axis is None:
        image_orig = rgb2gray(image_orig)
        image_defect = rgb2gray(image_defect)

    image_orig = image_orig.astype(dtype, copy=False)
    image_defect = image_defect.astype(dtype, copy=False)

    image_defect = np.asarray(image_defect, order=order)
    image_result = inpaint.inpaint_biharmonic(
        image_defect,
        mask,
        channel_axis=channel_axis,
        split_into_regions=split_into_regions)
    assert image_result.dtype == float_dtype

    nrmse_defect = normalized_root_mse(image_orig, image_defect)
    nrmse_result = normalized_root_mse(img_as_float(image_orig), image_result)
    assert nrmse_result < 0.2 * nrmse_defect
Exemplo n.º 11
0
def create_background(img: "np.ndarray") -> "np.ndarray":
    mask = create_mask(img)

    kernel = np.ones((8, 8), np.uint8)
    mask_dilation = cv2.dilate(img_as_ubyte(mask), kernel, iterations=2)

    # Defect image over the same region in each color channel
    image_defect = img.copy()
    for layer in range(image_defect.shape[-1]):
        image_defect[np.where(mask_dilation)] = 0

    image_result = inpaint.inpaint_biharmonic(image_defect, mask_dilation, multichannel=True)
    return image_result
Exemplo n.º 12
0
def vec_to_sevelev_newlayout(x):
    ''' convert a vector consisting of 32 electrodes to a 7x11 matrix using 
    inpainting '''
    x = np.squeeze(x)
    w = 11
    h = 7
    elcpos = np.empty((h, w))
    elcpos[:] = np.nan
    elcpos[0, 4] = x[0]
    elcpos[1, 3] = x[1]
    elcpos[1, 2] = x[2]
    elcpos[2, 0] = x[3]
    elcpos[2, 2] = x[4]
    elcpos[2, 4] = x[5]
    elcpos[3, 3] = x[6]
    elcpos[3, 1] = x[7]
    elcpos[4, 0] = x[8]
    elcpos[4, 2] = x[9]
    elcpos[4, 4] = x[10]
    
    elcpos[5, 5] = x[11]
    elcpos[5, 3] = x[12]
    elcpos[5, 2] = x[13]
    elcpos[6, 4] = x[14]
    elcpos[6, 5] = x[15]
    elcpos[6, 6] = x[16]

    elcpos[5, 7] = x[17]
    elcpos[5, 8] = x[18]
    elcpos[4, 10] = x[19]
    elcpos[4, 8] = x[20]
    elcpos[4, 6] = x[21]
    elcpos[3, 5] = x[22]
    elcpos[3, 7] = x[23]
    elcpos[3, 9] = x[24]
    
    elcpos[2, 10] = x[25] # FT10
    elcpos[2, 8] = x[26]
    elcpos[2, 6] = x[27]
    elcpos[1, 7] = x[28]
    elcpos[1, 8] = x[29]
    elcpos[0, 6] = x[30]
    # elcpos[1, 5] = 5 Fz was reference
    # elcpos[6, 2] = 28 PO9 deleted
    # elcpos[6, 8] = 32 PO10 deleted

    mask = np.zeros((elcpos.shape))
    mask[np.isnan(elcpos)] = 1
        
    
    return inpaint.inpaint_biharmonic(elcpos, mask, multichannel=False)
def denoiseInpaint(imagen,mask,multichannel):
    noisy = img_as_float(imagen)
    image_orig = noisy

    # Afecta a la imagen original en las regiones marcadas por la mascara
    # Se puede cambiar de acuerdo a lo que se necesite
    # En este caso produce defectos en la zona
    image_defect = image_orig.copy()
    for layer in range(image_defect.shape[-1]):
        image_defect[np.where(mask)] = 1

    image_result = inpaint.inpaint_biharmonic(image_defect, mask, multichannel)

    return image_result
Exemplo n.º 14
0
def inpaint_biharmonic(frame, mask):

    """
    This function ...
    :param frame:
    :param mask:
    :return:
    """

    maximum = np.nanmax(frame)
    normalized = frame / maximum
    data = inpaint.inpaint_biharmonic(normalized, mask, multichannel=False)

    return data * maximum
Exemplo n.º 15
0
def test_inpaint_biharmonic_2d():
    img = np.tile(np.square(np.linspace(0, 1, 5)), (5, 1))
    mask = np.zeros_like(img)
    mask[2, 2:] = 1
    mask[1, 3:] = 1
    mask[0, 4:] = 1
    img[np.where(mask)] = 0
    out = inpaint.inpaint_biharmonic(img, mask)
    ref = np.array([[0., 0.0625, 0.25000000, 0.5625000, 0.73925058],
                    [0., 0.0625, 0.25000000, 0.5478048, 0.76557821],
                    [0., 0.0625, 0.25842878, 0.5623079, 0.85927796],
                    [0., 0.0625, 0.25000000, 0.5625000, 1.00000000],
                    [0., 0.0625, 0.25000000, 0.5625000, 1.00000000]])
    assert_allclose(ref, out)
Exemplo n.º 16
0
def inpaint_biharmonic(frame, mask):

    """
    This function ...
    :param frame:
    :param mask:
    :return:
    """

    maximum = np.nanmax(frame)
    normalized = frame / maximum
    data = inpaint.inpaint_biharmonic(normalized, mask, multichannel=False)

    return data * maximum
Exemplo n.º 17
0
def paintNaN(array):
    # Should be equivalent to the 3rd method in the MATLAB nanpaint function
    print("Painting NaNs")
    t0 = time.time()
    mask = np.zeros(array.shape)
    mask[np.isnan(array)] = 1
    fixed = inpaint.inpaint_biharmonic(array, mask)

    del mask

    t1 = time.time()

    print("Paint NaNs Time: " + str(t1 - t0))

    return fixed
Exemplo n.º 18
0
def test_inpaint_biharmonic_2d_float_dtypes(dtype):
    img = np.tile(np.square(np.linspace(0, 1, 5)), (5, 1))
    mask = np.zeros_like(img)
    mask[2, 2:] = 1
    mask[1, 3:] = 1
    mask[0, 4:] = 1
    img[np.where(mask)] = 0
    img = img.astype(dtype, copy=False)
    out = inpaint.inpaint_biharmonic(img, mask)
    assert out.dtype == img.dtype
    ref = np.array([[0., 0.0625, 0.25000000, 0.5625000, 0.73925058],
                    [0., 0.0625, 0.25000000, 0.5478048, 0.76557821],
                    [0., 0.0625, 0.25842878, 0.5623079, 0.85927796],
                    [0., 0.0625, 0.25000000, 0.5625000, 1.00000000],
                    [0., 0.0625, 0.25000000, 0.5625000, 1.00000000]])
    assert_allclose(ref, out, rtol=1e-5)
Exemplo n.º 19
0
def test_inpaint_biharmonic_2d():
    img = np.tile(np.square(np.linspace(0, 1, 5)), (5, 1))
    mask = np.zeros_like(img)
    mask[2, 2:] = 1
    mask[1, 3:] = 1
    mask[0, 4:] = 1
    img[np.where(mask)] = 0
    out = inpaint.inpaint_biharmonic(img, mask)
    ref = np.array(
        [[0., 0.0625, 0.25000000, 0.5625000, 0.73925058],
         [0., 0.0625, 0.25000000, 0.5478048, 0.76557821],
         [0., 0.0625, 0.25842878, 0.5623079, 0.85927796],
         [0., 0.0625, 0.25000000, 0.5625000, 1.00000000],
         [0., 0.0625, 0.25000000, 0.5625000, 1.00000000]]
    )
    assert_allclose(ref, out)
Exemplo n.º 20
0
def test_inpaint_biharmonic_2d_color(channel_axis):
    img = img_as_float(data.astronaut()[:64, :64])

    mask = np.zeros(img.shape[:2], dtype=bool)
    mask[8:16, :16] = 1
    img_defect = img * ~mask[..., np.newaxis]
    mse_defect = mean_squared_error(img, img_defect)

    img_defect = np.moveaxis(img_defect, -1, channel_axis)
    img_restored = inpaint.inpaint_biharmonic(img_defect,
                                              mask,
                                              channel_axis=channel_axis)
    img_restored = np.moveaxis(img_restored, channel_axis, -1)
    mse_restored = mean_squared_error(img, img_restored)

    assert mse_restored < 0.01 * mse_defect
Exemplo n.º 21
0
def inpaint_image(image, mask):
    image = np.array(image.copy())
    mask = np.array(mask.copy())
    image_orig = rescale(image, 1.0 / 5.0, anti_aliasing=False)
    mask = color.rgb2gray(mask)
    rescaled_mask = rescale(mask, 1.0 / 5.0, anti_aliasing=False)
    thresh = threshold_otsu(rescaled_mask)
    binary = rescaled_mask > thresh
    image_defect = image_orig.copy()
    for layer in range(image_defect.shape[-1]):
        image_defect[np.where(binary)] = 0
    image_result = inpaint.inpaint_biharmonic(image_defect, binary,
                                            multichannel=True)
    image_result = rescale(image_result, 5.0, anti_aliasing=False)

    return image_result, mask
Exemplo n.º 22
0
def test_invalid_input():
    img, mask = np.zeros([]), np.zeros([])
    with pytest.raises(ValueError):
        inpaint.inpaint_biharmonic(img, mask)

    img, mask = np.zeros((2, 2)), np.zeros((4, 1))
    with pytest.raises(ValueError):
        inpaint.inpaint_biharmonic(img, mask)

    img = np.ma.array(np.zeros((2, 2)), mask=[[0, 0], [0, 0]])
    mask = np.zeros((2, 2))
    with pytest.raises(TypeError):
        inpaint.inpaint_biharmonic(img, mask)
Exemplo n.º 23
0
def test_invalid_input():
    img, mask = np.zeros([]), np.zeros([])
    with testing.raises(ValueError):
        inpaint.inpaint_biharmonic(img, mask)

    img, mask = np.zeros((2, 2)), np.zeros((4, 1))
    with testing.raises(ValueError):
        inpaint.inpaint_biharmonic(img, mask)

    img = np.ma.array(np.zeros((2, 2)), mask=[[0, 0], [0, 0]])
    mask = np.zeros((2, 2))
    with testing.raises(TypeError):
        inpaint.inpaint_biharmonic(img, mask)
Exemplo n.º 24
0
def test_inpaint_biharmonic_2d(dtype, split_into_regions):
    img = np.tile(np.square(np.linspace(0, 1, 5, dtype=dtype)), (5, 1))
    mask = np.zeros_like(img)
    mask[2, 2:] = 1
    mask[1, 3:] = 1
    mask[0, 4:] = 1
    img[np.where(mask)] = 0
    out = inpaint.inpaint_biharmonic(img,
                                     mask,
                                     split_into_regions=split_into_regions)
    assert out.dtype == _supported_float_type(img)

    ref = np.array([[0., 0.0625, 0.25000000, 0.5625000, 0.73925058],
                    [0., 0.0625, 0.25000000, 0.5478048, 0.76557821],
                    [0., 0.0625, 0.25842878, 0.5623079, 0.85927796],
                    [0., 0.0625, 0.25000000, 0.5625000, 1.00000000],
                    [0., 0.0625, 0.25000000, 0.5625000, 1.00000000]])
    rtol = 1e-7 if dtype == np.float64 else 1e-6
    assert_allclose(ref, out, rtol=rtol)
Exemplo n.º 25
0
def test_inpaint_biharmonic_3d():
    img = np.tile(np.square(np.linspace(0, 1, 5)), (5, 1))
    img = np.dstack((img, img.T))
    mask = np.zeros_like(img)
    mask[2, 2:, :] = 1
    mask[1, 3:, :] = 1
    mask[0, 4:, :] = 1
    img[np.where(mask)] = 0
    out = inpaint.inpaint_biharmonic(img, mask)
    ref = np.dstack(
        (np.array([[0.0000, 0.0625, 0.25000000, 0.56250000, 0.53752796],
                   [0.0000, 0.0625, 0.25000000, 0.44443780, 0.53762210],
                   [0.0000, 0.0625, 0.23693666, 0.46621112, 0.68615592],
                   [0.0000, 0.0625, 0.25000000, 0.56250000, 1.00000000],
                   [0.0000, 0.0625, 0.25000000, 0.56250000, 1.00000000]]),
         np.array([[0.0000, 0.0000, 0.00000000, 0.00000000, 0.19621902],
                   [0.0625, 0.0625, 0.06250000, 0.17470756, 0.30140091],
                   [0.2500, 0.2500, 0.27241289, 0.35155440, 0.43068654],
                   [0.5625, 0.5625, 0.56250000, 0.56250000, 0.56250000],
                   [1.0000, 1.0000, 1.00000000, 1.00000000, 1.00000000]])))
    assert_allclose(ref, out)
def save_image_in_actual_size(data_set, saving_folder):
    """Takes a data set and save it as .png image"""
    global product_name
    global name
    sleep(0.01)
    #dpi = 80
    dpi = matplotlib.rcParams['figure.dpi']
    img = data_set.values
    r, c = img.shape
    #img = img[5:r-5,100:c-80]
    #fill NaNs
    mask = numpy.isnan(img)
    start_time = time.time()
    print("Start processing: " + product_name + "--" + data_set.name)
    try:
        im_data = inpaint.inpaint_biharmonic(img, mask)
        print("--- %s seconds ---" % (time.time() - start_time))
        height, width = im_data.shape
        print(im_data.shape)

        # What size does the figure need to be in inches to fit the image?
        figsize = width / float(dpi), height / float(dpi)

        # Create a figure of the right size with one axes that takes up the full figure
        fig = plt.figure(figsize=figsize)
        ax = fig.add_axes([0, 0, 1, 1])

        # Hide spines, ticks, etc.
        ax.axis('off')

        # Save the image.
        name = product_name + ".png"
        ax.imshow(im_data, cmap='gray')
        plt.savefig(saving_folder + name)
        plt.close()

    except ValueError:  #raised if xx is empty.
        print("++++++++ValueError in this product: " + product_name + "--" +
              data_set.name)
        pass
Exemplo n.º 27
0
    def call_inpaint(self, coordinates):
        image_orig = imageio.imread(self.image1.image_path)
        mask = np.zeros(image_orig.shape[:-1])
        mask[coordinates[0][1]:coordinates[1][1], coordinates[0][0]:coordinates[1][0]] = 1

        image_defect = image_orig.copy()

        for layer in range(image_defect.shape[-1]):
            image_defect[np.where(mask)] = 0

        image_result = inpaint.inpaint_biharmonic(image_defect, mask, multichannel=True)

        if debug:
            fig, axes = plt.subplots(ncols=2, nrows=2)
            ax = axes.ravel()

            ax[0].set_title('Original image')
            ax[0].imshow(image_orig)

            ax[1].set_title('Mask')
            ax[1].imshow(mask, cmap=plt.cm.gray)

            ax[2].set_title('Defected image')
            ax[2].imshow(image_defect)

            ax[3].set_title('Inpainted image')
            ax[3].imshow(image_result)

            for a in ax:
                a.axis('off')

            fig.tight_layout()
            plt.show()

        imageio.imwrite(self.image1.image_path, image_result)
        self.image1.selection.hide()
Exemplo n.º 28
0
def _make_movie(h5file,
                outfile,
                timestep,
                duration,
                title='',
                usewcs=False,
                startw=None,
                stopw=None,
                startt=None,
                stopt=None,
                fps=False,
                showaxes=False,
                inpainting=False,
                cps_cutoff=5000,
                maskbadpix=False,
                colormap='Blue',
                dpi=400):
    """returns the movie frames"""
    global _cache
    movietype = os.path.splitext(outfile)[1].lower()
    if movietype not in ('.mp4', '.gif'):
        raise ValueError('Only mp4 and gif movies are supported')

    try:
        cube, wcs, nfo = _cache
        if (h5file, timestep, startt, stopt, usewcs, startw, stopw) != nfo:
            raise ValueError
    except Exception:
        getLogger(__name__).info(
            'Fetching temporal cube from {}'.format(h5file))
        of = mkidpipeline.hdf.photontable.Photontable(h5file)
        cube = of.getTemporalCube(firstSec=startt,
                                  integrationTime=stopt,
                                  timeslice=timestep,
                                  startw=startw,
                                  stopw=stopw,
                                  applyWeight=True,
                                  applyTPFWeight=True)
        wcs = of.get_wcs(wcs_timestep=startt) if usewcs else None
        del of
        _cache = cube, wcs, (h5file, timestep, startt, stopt, usewcs, startw,
                             stopw)
        getLogger(__name__).info(
            'Retrieved a temporal cube of shape {}'.format(
                str(cube['cube'].shape)))
    if inpainting:
        getLogger(__name__).info(
            'Inpainting requested - note this will significantly slow down movie creation'
        )
        full_frames, times = cube['cube'].T, cube['timeslices']
        input_frames = full_frames[:, 10:140, 80:122]
        masked_array = np.ma.masked_where(input_frames < cps_cutoff * timestep,
                                          input_frames)
        count_masks = np.ma.getmaskarray(masked_array)
        masks = count_masks
        frames = np.zeros(full_frames.shape)
        for i, mask in enumerate(masks):
            frames[i, 10:140,
                   80:122] = inpaint.inpaint_biharmonic(input_frames[i],
                                                        mask,
                                                        multichannel=False)
        getLogger(__name__).info('Completed inpainting process!')
    else:
        frames, times = cube['cube'].T, cube['timeslices']
    if len(frames) == 0:
        getLogger(__name__).info(
            'No frames in the specified timerange - check your stop and start times'
        )
        raise ValueError

    if not fps:
        fps = frames.shape[2] / duration

    #Load the writer
    Writer = manimation.writers[
        'ffmpeg'] if movietype is 'mp4' else manimation.writers['imagemagick']
    comment = 't0={:.0f} dt={:.1f}s {:.0f} - {:.0f} nm'.format(
        times[0], timestep, startw if startw is not None else 0,
        stopw if stopw is not None else np.inf)
    metadata = dict(title=title,
                    artist=__name__,
                    genre='Astronomy',
                    comment=comment)
    writer = Writer(fps=fps, metadata=metadata, bitrate=-1)

    fig = plt.figure()
    if usewcs:
        plt.subplot(projection=wcs)
    if maskbadpix:
        of = mkidpipeline.hdf.photontable.Photontable(h5file)
        for i in range(frames.shape[0]):
            frames[i][of.pixelBadMask.T] = np.nan
    im = plt.imshow(frames[0],
                    interpolation='none',
                    origin='lower',
                    vmin=0,
                    vmax=cps_cutoff * timestep,
                    cmap=plt.get_cmap(colormap))
    im.cmap.set_bad('black')
    cbar = plt.colorbar()
    ticks = cbar.get_ticks()
    cbar.set_label('Photons/s')
    cbar.set_ticks(ticks)
    cbar.set_ticklabels(list(map('{:.0f}'.format, ticks / timestep)))

    if not showaxes:
        fig.patch.set_visible(False)
        plt.gca().axis('off')
    else:
        if usewcs:
            plt.xlabel('RA')
            plt.ylabel('RA')
        else:
            plt.xlabel('Pixel')
            plt.ylabel('Pixel')
        plt.tight_layout()

    with writer.saving(fig, outfile, dpi):
        for a in frames:
            im.set_array(a)
            writer.grab_frame()

    return frames
Exemplo n.º 29
0
plt.imshow(np.log(out[251]))
plt.show()

eros = ndi.grey_erosion(signal, footprint=ball(3))
eros2 = ndi.grey_erosion(image, footprint=disk(3))

opened = opening(image)
closed = closing(image)

opened = opening(signal)
closed = closing(signal)

gf = ndi.gaussian_filter(image, 1)
opened = opening(gf)
closed = closing(gf)
eros = erosion(gf)

# Finding local maxima
# http://scikit-image.org/docs/dev/auto_examples/segmentation/plot_peak_local_max.html

coordinates = peak_local_max(image, min_distance=20)
plt.imshow(np.log(image), cmap=plt.cm.gray)
plt.plot(coordinates[:, 1], coordinates[:, 0], 'r.')
plt.show()

# Inpainting
# http://scikit-image.org/docs/dev/auto_examples/filters/plot_inpaint.html
s = signal[251]
result = inpaint.inpaint_biharmonic(s, np.isnan(s))
Exemplo n.º 30
0
from skimage.restoration import inpaint
#2
# Import the module from restoration
from skimage.restoration import inpaint

# Show the defective image
show_image(defect_image, 'Image to restore')
#3
# Import the module from restoration
from skimage.restoration import inpaint

# Show the defective image
show_image(defect_image, 'Image to restore')

# Apply the restoration function to the image using the mask
restored_image = inpaint.inpaint_biharmonic(defect_image, get_mask(defect_image), multichannel=True)
show_image(restored_image)


--------------------------------------------------
# Exercise_2 
# Initialize the mask
mask = np.zeros(image_with_logo.shape[:-1])

# Set the pixels where the logo is to 1
mask[210:272, 360:425] = 1

# Apply inpainting to remove the logo
image_logo_removed = inpaint.inpaint_biharmonic(image_with_logo,
                                  mask,
                                  multichannel=True)
Exemplo n.º 31
0
def inpainting(image, mask_map):
    ex = norm(image)
    ex[np.where(mask_map)] = 0
    return inpaint.inpaint_biharmonic(ex, mask_map)
Exemplo n.º 32
0
for d in detected:  
    # Obtain the face rectangle from detected coordinates
    face = getFaceRectangle(d)
    
    # Apply gaussian filter to extracted face
    blurred_face = gaussian(face, multichannel=True, sigma = 8)
    
    # Merge this blurry face to our final image and show it
    resulting_image = mergeBlurryFace(group_image, blurred_face) 
show_image(resulting_image, "Blurred faces")

--------------------------------------------------
# Exercise_9 
# Import the necessary modules
from skimage.restoration import denoise_tv_chambolle, inpaint
from skimage import transform

# Transform the image so it's not rotated
upright_img = rotate(damaged_image, 20)

# Remove noise from the image, using the chambolle method
upright_img_without_noise = denoise_tv_chambolle(upright_img,weight=0.1, multichannel=True)

# Reconstruct the image missing parts
mask = get_mask(upright_img)
result = inpaint.inpaint_biharmonic(upright_img_without_noise, mask, multichannel=True)

show_image(result)

--------------------------------------------------
Exemplo n.º 33
0
def preproc_field(field,
                  mask,
                  inpaint=True,
                  median=True,
                  med_size=(3, 1),
                  downscale=True,
                  dscale_size=(2, 2),
                  sigmoid=False,
                  scale=None,
                  expon=None,
                  only_inpaint=False,
                  gradient=False,
                  log_scale=False):
    """
    Preprocess an input field image with a series of steps:
        1. Inpainting
        2. Median
        3. Downscale
        4. Sigmoid
        5. Scale
        6. Remove mean

    Parameters
    ----------
    field : np.ndarray
    mask : np.ndarray
        Data mask.  True = masked
    inpaint : bool, optional
        if True, inpaint masked values
    median : bool, optional
        If True, apply a median filter
    med_size : tuple
        Median window to apply
    downscale : bool, optional
        If True downscale the image
    dscale_size : tuple, optional
        Size to rescale by
    scale : float
        Scale the SSTa values by this multiplicative factor
    expon : float
        Exponate the SSTa values by this exponent
    gradient : bool, optional
        If True, apply a Sobel gradient enhancing filter

    Returns
    -------
    pp_field, meta_dict : np.ndarray, dict
        Pre-processed field, mean temperature

    """
    meta_dict = {}
    # Inpaint?
    if inpaint:
        if mask.dtype.name != 'uint8':
            mask = np.uint8(mask)
        field = sk_inpaint.inpaint_biharmonic(field, mask, multichannel=False)

    if only_inpaint:
        if np.any(np.isnan(field)):
            return None, None
        else:
            return field, None

    # Capture more metadata
    srt = np.argsort(field.flatten())
    meta_dict['Tmax'] = field.flatten()[srt[-1]]
    meta_dict['Tmin'] = field.flatten()[srt[0]]
    i10 = int(0.1 * field.size)
    i90 = int(0.9 * field.size)
    meta_dict['T10'] = field.flatten()[srt[i10]]
    meta_dict['T90'] = field.flatten()[srt[i90]]

    # Median
    if median:
        field = median_filter(field, size=med_size)

    # Reduce to 64x64
    if downscale:
        field = downscale_local_mean(field, dscale_size)

    # Check for junk
    if np.any(np.isnan(field)):
        return None, None

    # De-mean the field
    mu = np.mean(field)
    pp_field = field - mu
    meta_dict['mu'] = mu

    # Sigmoid?
    if sigmoid:
        pp_field = special.erf(pp_field)

    # Scale?
    if scale is not None:
        pp_field *= scale

    # Exponate?
    if expon is not None:
        neg = pp_field < 0.
        pos = np.logical_not(neg)
        pp_field[pos] = pp_field[pos]**expon
        pp_field[neg] = -1 * (-1 * pp_field[neg])**expon

    # Sobel Gradient?
    if gradient:
        pp_field = filters.sobel(pp_field)

    # Log?
    if log_scale:
        if not gradient:
            raise IOError("Only implemented with gradient=True so far")
        # Set 0 values to the lowest non-zero value
        zero = pp_field == 0.
        if np.any(zero):
            min_nonz = np.min(pp_field[np.logical_not(zero)])
            pp_field[zero] = min_nonz
        # Take log
        pp_field = np.log(pp_field)

    # Return
    return pp_field, meta_dict
Exemplo n.º 34
0
image_orig = data.astronaut()[0:200, 0:200]

# Create mask with three defect regions: left, middle, right respectively
mask = np.zeros(image_orig.shape[:-1])
mask[20:60, 0:20] = 1
mask[160:180, 70:155] = 1
mask[30:60, 170:195] = 1

# Defect image over the same region in each color channel
image_defect = image_orig.copy()
for layer in range(image_defect.shape[-1]):
    image_defect[np.where(mask)] = 0

image_result = inpaint.inpaint_biharmonic(image_defect,
                                          mask,
                                          multichannel=True)

fig, axes = plt.subplots(ncols=2, nrows=2)
ax = axes.ravel()

ax[0].set_title('Original image')
ax[0].imshow(image_orig)

ax[1].set_title('Mask')
ax[1].imshow(mask, cmap=plt.cm.gray)

ax[2].set_title('Defected image')
ax[2].imshow(image_defect)

ax[3].set_title('Inpainted image')
Exemplo n.º 35
0
def biharmonic_eq(im_path, mask_path):
    im = io.imread(im_path)
    mask = io.imread(mask_path)
    dst = inpaint.inpaint_biharmonic(image_defect, mask, multichannel=True)
    return dst
Exemplo n.º 36
0
from skimage.restoration import inpaint

image_orig = data.astronaut()

# Create mask with three defect regions: left, middle, right respectively
mask = np.zeros(image_orig.shape[:-1])
mask[20:60, 0:20] = 1
mask[200:300, 150:170] = 1
mask[50:100, 400:430] = 1

# Defect image over the same region in each color channel
image_defect = image_orig.copy()
for layer in range(image_defect.shape[-1]):
    image_defect[np.where(mask)] = 0

image_result = inpaint.inpaint_biharmonic(image_defect, mask, multichannel=True)

fig, axes = plt.subplots(ncols=2, nrows=2)
ax0, ax1, ax2, ax3 = axes.ravel()

ax0.set_title('Original image')
ax0.imshow(image_orig)
ax0.axis('off')

ax1.set_title('Mask')
ax1.imshow(mask, cmap=plt.cm.gray)
ax1.axis('off')

ax2.set_title('Defected image')
ax2.imshow(image_defect)
ax2.axis('off')
# Loaded as defect_image.
# We'll work on an image from the data module, obtained by data.astronaut(). Some of the pixels have been replaced by 1s using a binary mask, on purpose, to simulate a damaged image. Replacing pixels with 1s turns them totally black. The defective image is saved as an array called defect_image.

# The mask is a black and white image with patches that have the position of the image bits that have been corrupted. We can apply the restoration function on these areas. This mask is preloaded as mask.

# Remember that inpainting is the process of reconstructing lost or deteriorated parts of images and videos.

# Instructions 1/3
# 35 XP
# Import the inpaint function in the restoration module in scikit-image (skimage).

# Instructions 2/3
# 35 XP
# Show the defective image using show_image().

# Instructions 3/3
# 30 XP
# Call the correct function from inpaint. Use the corrupted image as the first parameter, then the mask and multichannel boolean.

# Import the module from restoration (Instruction 1)
from skimage.restoration import inpaint

# Show the defective image (Instruction 2)
show_image(defect_image, 'Image to restore')

# Apply the restoration function to the image using the mask (Instruction 3)
restored_image = inpaint.inpaint_biharmonic(defect_image,
                                            mask,
                                            multichannel=True)
show_image(restored_image)