Ejemplo n.º 1
0
        def solve_and_refine(x, w, refine=True, **tv_args):

            opt = tv1_2d(x, w[0], **tv_args)
            if refine:  # Should we improve it with isotonic regression.
                opt = TotalVariationBase._refine(opt, x, w, w)

            return opt
Ejemplo n.º 2
0
    def forward(self, x, w):
        r"""Solve the total variation problem and return the solution.

        Arguments
        ---------
            x: :class:`torch:torch.Tensor`
                A tensor with shape ``(m, n)`` holding the input signal.
            weights_row: :class:`torch:torch.Tensor`
                The horizontal edge weights.

                Tensor of shape ``(m, n - 1)``, or ``(1,)`` if all weights
                are equal.
            weights_col: :class:`torch:torch.Tensor`
                The vertical edge weights.

                Tensor of shape ``(m - 1, n)``, or ``(1,)`` if all weights
                are equal.

        Returns
        -------
        :class:`torch:torch.Tensor`
            The solution to the total variation problem, of shape ``(m, n)``.
        """
        assert w.size() == (1, )
        opt = tv1_2d(x.numpy(), w.numpy()[0], **self.tv_args)

        if self.refine:  # Should we improve it with isotonic regression.
            opt = self._refine(opt, x, w, w)

        opt = torch.Tensor(opt).view_as(x)
        self.save_for_backward(opt)
        return opt
Ejemplo n.º 3
0
def reconstruct_images_TV(P_stacked, coefs, lam):
    # Reconstruct using TV regularization
    # We use the proxTV toolbox for this: https://github.com/albarji/proxTV

    import prox_tv as ptv
    lr = 0.5
    n_iter = 400

    x = np.zeros([IMAGE_DIM**2, coefs.shape[1]])
    for i in range(n_iter):
        delta = coefs - P_stacked.dot(x)
        x += lr * (P_stacked.T).dot(delta)
        for img in range(coefs.shape[1]):
            # Do each image separately
            x[:, img] = (ptv.tv1_2d(x[:, img].reshape((IMAGE_DIM, IMAGE_DIM)),
                                    lam * lr)).flatten()

        # Each image should be between 0 and 1 so we enforce this constraint
        x[x < 0] = 0
        x[x > 1] = 1

        if (i % 100 == 0):
            print('Number of iterations: ' + str(i))

    # Some code to manipulate and organize the reconstructions
    recons = np.zeros([coefs.shape[1], IMAGE_DIM, IMAGE_DIM])
    for i in range(coefs.shape[1]):
        vec_img = x[:, i]
        recon = vec_img.reshape((IMAGE_DIM, IMAGE_DIM))
        recons[i, :, :] = recon

    return recons
def tde_tosubspace(I, A, lam = 0):
    #(Matt Berger wrote part of this function)
    N = I.shape[1]
    P = I.shape[0]
    s_dim = A

    I_mean = (1.0/N)*I.dot(np.ones(N))
    I_centered = I - np.asarray([I_mean]*N).T
    # first, compute subspace for I, and corresponding subspace representations
    print 'forming input covariance matrix: ',
    start_time = time.time()
    input_covariance = I_centered.T.dot(I_centered)
    end_time = time.time()
    print end_time-start_time, " seconds"
    
    print 'computing data subspace: ',
    start_time = time.time()
    [data_eigenvalues, data_eigenvectors] = linalg.eigh(input_covariance)
    print 'input eigenvalues:',data_eigenvalues
    end_time = time.time()
    print end_time-start_time, " seconds"

    max_s_dim = N
    for idx,eigenvalue in enumerate(data_eigenvalues):
        if eigenvalue > 1e-12:
            break
        max_s_dim=max_s_dim-1
    if max_s_dim < A:
        s_dim = max_s_dim
        print 'subspace of dimensionality:',s_dim,'NOT',A
    data_subspace = I_centered.dot(data_eigenvectors[0:N,-s_dim:])
    data_subspace = data_subspace/np.sqrt(np.sum(data_subspace**2, 0))
    #print 'data eigenvalues:',data_eigenvalues

    if lam > 0:
        import prox_tv as ptv
        #Perform total variation denoising on the subspace columns
        #TODO: This is no longer an orthogonal subspace
        for i in range(data_subspace.shape[1]-A, data_subspace.shape[1]):
            print "Doing i = %i"%i
            v = np.reshape(data_subspace[:, i], IDims)
            vnew = np.zeros(v.shape)
            for k in range(3):
                vnew[:, :, k] = ptv.tv1_2d(v[:, :, k], lam)
            
            plt.subplot(121)
            v = np.reshape(data_subspace[:, i], IDims)
            plt.imshow(v/np.max(v))
            plt.title("Component %i"%i)
            plt.subplot(122)
            plt.imshow(vnew/np.max(vnew))
            plt.savefig("TV%i.png"%i, dpi=200)
            
            data_subspace[:, i] = vnew.flatten()

    # project data
    print 'projecting data...'
    s_I = data_subspace.T.dot(I_centered)
    return (data_subspace, s_I)    
def tde_tosubspace(I, A, lam=0):
    #(Matt Berger wrote part of this function)
    N = I.shape[1]
    P = I.shape[0]
    s_dim = A

    I_mean = (1.0 / N) * I.dot(np.ones(N))
    I_centered = I - np.asarray([I_mean] * N).T
    # first, compute subspace for I, and corresponding subspace representations
    print 'forming input covariance matrix: ',
    start_time = time.time()
    input_covariance = I_centered.T.dot(I_centered)
    end_time = time.time()
    print end_time - start_time, " seconds"

    print 'computing data subspace: ',
    start_time = time.time()
    [data_eigenvalues, data_eigenvectors] = linalg.eigh(input_covariance)
    print 'input eigenvalues:', data_eigenvalues
    end_time = time.time()
    print end_time - start_time, " seconds"

    max_s_dim = N
    for idx, eigenvalue in enumerate(data_eigenvalues):
        if eigenvalue > 1e-12:
            break
        max_s_dim = max_s_dim - 1
    if max_s_dim < A:
        s_dim = max_s_dim
        print 'subspace of dimensionality:', s_dim, 'NOT', A
    data_subspace = I_centered.dot(data_eigenvectors[0:N, -s_dim:])
    data_subspace = data_subspace / np.sqrt(np.sum(data_subspace**2, 0))
    #print 'data eigenvalues:',data_eigenvalues

    if lam > 0:
        import prox_tv as ptv
        #Perform total variation denoising on the subspace columns
        #TODO: This is no longer an orthogonal subspace
        for i in range(data_subspace.shape[1] - A, data_subspace.shape[1]):
            print "Doing i = %i" % i
            v = np.reshape(data_subspace[:, i], IDims)
            vnew = np.zeros(v.shape)
            for k in range(3):
                vnew[:, :, k] = ptv.tv1_2d(v[:, :, k], lam)

            plt.subplot(121)
            v = np.reshape(data_subspace[:, i], IDims)
            plt.imshow(v / np.max(v))
            plt.title("Component %i" % i)
            plt.subplot(122)
            plt.imshow(vnew / np.max(vnew))
            plt.savefig("TV%i.png" % i, dpi=200)

            data_subspace[:, i] = vnew.flatten()

    # project data
    print 'projecting data...'
    s_I = data_subspace.T.dot(I_centered)
    return (data_subspace, s_I)
def test_tv1_tvp_2d():
    """Tests that 2D-TVp == 2D-TV1 when p=1"""
    for _ in range(20):
        x = _generate2D()
        w = 20 * np.random.rand()
        solution1 = tv1_2d(x, w, max_iters=5000)
        solutionp = tvp_2d(x, w, w, 1, 1, max_iters=5000)
        assert np.allclose(solution1, solutionp, atol=1e-3)
Ejemplo n.º 7
0
def test_tv1_tvp_2d():
    """Tests that 2D-TVp == 2D-TV1 when p=1"""
    for _ in range(20):
        x = _generate2d()
        w = 20*np.random.rand()
        solution1 = tv1_2d(x, w, max_iters=5000)
        solutionp = tvp_2d(x, w, w, 1, 1, max_iters=5000)
        assert np.allclose(solution1, solutionp, atol=1e-3)
Ejemplo n.º 8
0
def test_tvgen_2d():
    """Tests that the general solver returns correct 2d solutions"""
    for _ in range(20):
        x = _generate2d()
        w = 20*np.random.rand()
        specific = tv1_2d(x, w, max_iters=1000)
        general = tvgen(x, [w, w], [1, 2], [1, 1], max_iters=1000)
        assert np.allclose(specific, general, atol=1e-2)
def test_tvgen_2d():
    """Tests that the general solver returns correct 2d solutions"""
    for _ in range(20):
        x = _generate2D()
        w = 20 * np.random.rand()
        specific = tv1_2d(x, w, max_iters=1000)
        general = tvgen(x, [w, w], [1, 2], [1, 1], max_iters=1000)
        assert np.allclose(specific, general, atol=1e-2)
Ejemplo n.º 10
0
def test_tv1w_2d_emengd():
    r"""Issue reported by emengd
    
    Make the solver fail due to missing checks on integer arguments
    """        
    a = -np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])/10.
    sol1 = tv1w_2d(a, np.array([[1, 1, 1], [1, 1, 1]]),
                   np.array([[1, 1], [1, 1], [1, 1]]), max_iters=100)
    sol2 = tv1_2d(a, 1)
    assert np.allclose(sol1, sol2, atol=1e-3)
Ejemplo n.º 11
0
def processInput(x, **kwargs):
    """
    Processes images based on the given input 1) List of filenames 2) z-index of stack mem-map

    if x is [str] the input to the script is assumed to be a folder of multiple .png/.tif image files

    if x is [int] the input to the script is assumed to be single tif stack
    """
    if isinstance(x, str):
        file_in = os.path.join(inputfolder, x)
        sys.stdout.write('Loading: ' + str(file_in) + ' -> ')
        name, extension = os.path.splitext(x)
        img = cv2.imread(file_in, cv2.IMREAD_UNCHANGED)
        file_out = os.path.join(outputfolder, str(name + '.png'))
    elif isinstance(x, int):
        mem_map_path = kwargs['mmap']
        stack_arr = load(mem_map_path, mmap_mode='r')
        sys.stdout.write('Loading: image layer no. ' + str(x + 1) + ' -> ')
        img = stack_arr[x, :, :]
        name_with_path, extension = os.path.splitext(inputfolder)
        out_name = str(
            os.path.basename(name_with_path)) + "_" + str(x + 1) + ".png"
        file_out = os.path.join(outputfolder, out_name)
    else:
        raise ValueError("Input supplied is incompatible")

    sys.stdout.write('Type: ' + str(img.dtype) + '\n')
    # Check 3rd dimension here, if loaded as RGB, remove 3rd dimension here
    if len(img.shape) > 2:
        # print('Converting RGB  to grey level image')
        img = img[:, :, 0]
    try:
        img = skimage.util.img_as_float(img)
    except:
        img = skimage.img_as_float64(img)
    # remove extreme outlier pixels before denoising
    img = skimage.exposure.rescale_intensity(img,
                                             in_range=(np.percentile(img, 1),
                                                       np.percentile(img, 99)),
                                             out_range=(0, 1))
    sigma_est1 = skimage.restoration.estimate_sigma(skimage.img_as_float(img))
    img = ptv.tv1_2d(img, sigma_est1 / 2, n_threads=num_threads)
    # img = skimage.restoration.denoise_tv_chambolle(img, weight=sigma_est1/2, multichannel=False)
    # img = skimage.restoration.denoise_tv_bregman(img, weight=sigma_est1/2, max_iter=100, eps=0.001, isotropic=True);
    img = skimage.exposure.rescale_intensity(
        img,
        in_range=(np.percentile(img,
                                cutperc), np.percentile(img, 100 - cutperc)),
        out_range=(0, 1))
    sigma_est2 = skimage.restoration.estimate_sigma(skimage.img_as_float(img))
    # sys.stdout.write(file_out + ": Estimated Gaussian noise stdev before " + str(sigma_est1) + " vs after denoising = " + str(sigma_est2))
    sys.stdout.write('Saving: ' + str(file_out) + '\n')
    img = 255 * img
    img = img.astype(np.uint8)
    cv2.imwrite(file_out, img)
Ejemplo n.º 12
0
def test_tv1w_2d_uniform_weights():
    for _ in range(20):
        x = _generate2d()
        rows = len(x)
        cols = len(x[0])
        w1 = np.random.rand()
        w_rows = np.ones([rows-1, cols]) * w1 
        w_cols = np.ones([rows, cols-1]) * w1 
        solw = tv1w_2d(x, w_rows, w_cols, max_iters=5000)
        solw1 = tv1_2d(x, w1, max_iters=5000)
        assert np.allclose(solw, solw1, atol=1e-3)
Ejemplo n.º 13
0
def test_tv1_2d():
    """Tests that all 2D-TV methods produce equivalent results"""
    methods = ('yang', 'condat', 'chambolle-pock', 'kolmogorov', 'pd', 'dr')
    for _ in range(20):
        x = _generate2d()
        w = 20*np.random.rand()
        solutions = [tv1_2d(x, w, method=method, max_iters=5000)
                     for method in methods]
        for i in range(1, len(solutions)):
            print(methods[i], ":", solutions[i])
            assert np.allclose(solutions[i], solutions[0], atol=1e-3)
Ejemplo n.º 14
0
def test_tv1w_2d_uniform_weights():
    for _ in range(20):
        rows = np.random.randint(1e1, 3e1)
        cols = np.random.randint(1e1, 3e1)
        x = 100 * np.random.randn(rows, cols)
        w1 = np.random.rand()
        w_rows = np.ones([rows - 1, cols]) * w1
        w_cols = np.ones([rows, cols - 1]) * w1
        solw = tv1w_2d(x, w_rows, w_cols, max_iters=5000)
        solw1 = tv1_2d(x, w1, max_iters=5000)
        assert np.allclose(solw, solw1, atol=1e-3)
Ejemplo n.º 15
0
def test_tv1w_2d_uniform_weights():
    for _ in range(20):
        rows = np.random.randint(1e1, 3e1)
        cols = np.random.randint(1e1, 3e1)
        x = 100*np.random.randn(rows, cols)
        w1 = np.random.rand()
        w_rows = np.ones([rows-1, cols]) * w1 
        w_cols = np.ones([rows, cols-1]) * w1 
        solw = tv1w_2d(x, w_rows, w_cols, max_iters=5000)
        solw1 = tv1_2d(x, w1, max_iters=5000)
        assert np.allclose(solw, solw1, atol=1e-3)
Ejemplo n.º 16
0
Archivo: owl.py Proyecto: VCCRI/Owl
def denoiseManual(inputFile, lamb):

    start = time.time()

    F = ptv.tv1_2d(inputFile, lamb, 1, 2)

    end = time.time()

    print('Time to denoise ' + str(end - start))

    return F
def test_tvgen_multireg():
    """Test applying several regularizers on same dimension"""
    for _ in range(20):
        x = _generate2D()
        w = 20 * np.random.rand()
        specific = tv1_2d(x, w, max_iters=1000)
        general = tvgen(x, [w / 2., w / 2., w / 3., w / 3., w / 3.],
                        [1, 1, 2, 2, 2], [1, 1, 1, 1, 1],
                        max_iters=1000)
        print("Max diff: " + str((specific - general).max()))
        assert np.allclose(specific, general, atol=1e-2)
def test_tv1w_2d_uniform_weights():
    for _ in range(20):
        x = _generate2D()
        rows = len(x)
        cols = len(x[0])
        w1 = np.random.rand()
        w_rows = np.ones([rows - 1, cols]) * w1
        w_cols = np.ones([rows, cols - 1]) * w1
        solw = tv1w_2d(x, w_rows, w_cols, max_iters=5000)
        solw1 = tv1_2d(x, w1, max_iters=5000)
        assert np.allclose(solw, solw1, atol=1e-3)
Ejemplo n.º 19
0
Archivo: owl.py Proyecto: VCCRI/Owl
def denoise(inputFile):

    print("Starting denoising")

    start = time.time()

    F = ptv.tv1_2d(inputFile, args.lamb, 1, 3)

    end = time.time()

    print('Time to denoise ' + str(end - start))
    return F
Ejemplo n.º 20
0
def test_tv1_tv1w_2d():
    """Tests that 2D-TV1w == 2D-TV1 for unit weights"""
    for _ in range(20):
        x = _generate2d()
        rows = len(x)
        cols = len(x[0])
        w = 20*np.random.rand()
        w_cols = w * np.ones((rows-1, cols))
        w_rows = w * np.ones((rows, cols-1))
        solution1 = tv1_2d(x, w, max_iters=5000)
        solutionp = tv1w_2d(x, w_cols, w_rows, max_iters=5000)
        assert np.allclose(solution1, solutionp, atol=1e-3)
Ejemplo n.º 21
0
def test_tv1w_2d_emengd():
    r"""Issue reported by emengd
    
    Make the solver fail due to missing checks on integer arguments
    """
    a = -np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) / 10.
    sol1 = tv1w_2d(a,
                   np.array([[1, 1, 1], [1, 1, 1]]),
                   np.array([[1, 1], [1, 1], [1, 1]]),
                   max_iters=100)
    sol2 = tv1_2d(a, 1)
    assert np.allclose(sol1, sol2, atol=1e-3)
def test_tv1_tv1w_2d():
    """Tests that 2D-TV1w == 2D-TV1 for unit weights"""
    for _ in range(20):
        x = _generate2D()
        rows = len(x)
        cols = len(x[0])
        w = 20 * np.random.rand()
        w_cols = w * np.ones((rows - 1, cols))
        w_rows = w * np.ones((rows, cols - 1))
        solution1 = tv1_2d(x, w, max_iters=5000)
        solutionp = tv1w_2d(x, w_cols, w_rows, max_iters=5000)
        assert np.allclose(solution1, solutionp, atol=1e-3)
def test_tv1_2d():
    """Tests that all 2D-TV methods produce equivalent results"""
    methods = ('yang', 'condat', 'chambolle-pock', 'kolmogorov', 'pd', 'dr')
    for _ in range(20):
        x = _generate2D()
        w = 20 * np.random.rand()
        solutions = [
            tv1_2d(x, w, method=method, max_iters=5000) for method in methods
        ]
        for i in range(1, len(solutions)):
            print(methods[i], ":", solutions[i])
            assert np.allclose(solutions[i], solutions[0], atol=1e-3)
Ejemplo n.º 24
0
def test_tvgen_multireg():
    """Test applying several regularizers on same dimension"""
    for _ in range(20):
        x = _generate2d()
        w = 20*np.random.rand()
        specific = tv1_2d(x, w, max_iters=1000)
        general = tvgen(
                x,
                [w/2., w/2., w/3., w/3., w/3.],
                [1, 1, 2, 2, 2],
                [1, 1, 1, 1, 1],
                max_iters=1000
        )
        print("Max diff: " + str((specific-general).max()))
        assert np.allclose(specific, general, atol=1e-2)
Ejemplo n.º 25
0
def test_tv1_2d():
    methods = ('yang', 'condat', 'chambolle-pock')
    for _ in range(20):
        rows = np.random.randint(1e1, 3e1)
        cols = np.random.randint(1e1, 3e1)
        x = 100*np.random.randn(rows, cols)
        w = 20*np.random.rand()
        solutions = [tv1_2d(x, w, method=method, max_iters=5000)
                     for method in methods]
        solutions.append([tvp_2d(x, w, w, 1, 1, max_iters=5000)])
        w_cols = w * np.ones((rows-1, cols))
        w_rows = w * np.ones((rows, cols-1))
        solutions.append(tv1w_2d(x, w_cols, w_rows, max_iters=5000))
        for i in range(1, len(solutions)):
            assert np.allclose(solutions[i], solutions[0], atol=1e-3)
Ejemplo n.º 26
0
def test_tv1_2d():
    methods = ('yang', 'condat', 'chambolle-pock')
    for _ in range(20):
        rows = np.random.randint(1e1, 3e1)
        cols = np.random.randint(1e1, 3e1)
        x = 100 * np.random.randn(rows, cols)
        w = 20 * np.random.rand()
        solutions = [
            tv1_2d(x, w, method=method, max_iters=5000) for method in methods
        ]
        solutions.append([tvp_2d(x, w, w, 1, 1, max_iters=5000)])
        w_cols = w * np.ones((rows - 1, cols))
        w_rows = w * np.ones((rows, cols - 1))
        solutions.append(tv1w_2d(x, w_cols, w_rows, max_iters=5000))
        for i in range(1, len(solutions)):
            assert np.allclose(solutions[i], solutions[0], atol=1e-3)
Ejemplo n.º 27
0
Archivo: owl.py Proyecto: VCCRI/Owl
def demo():

    # Load image
    here = os.path.dirname(os.path.abspath(__file__))
    X = io.imread(here + '/small.png')
    X = ski.img_as_float(X)
    X = color.rgb2gray(X)

    # Filter using 2D TV-L1
    lam = 0.005
    print('Filtering image with 2D TV-L1...')
    start = time.time()
    F = ptv.tv1_2d(X, lam, 1, 3)
    end = time.time()
    print('Elapsed time ' + str(end - start))
    io.imshow(X)
    plt.show()

    io.imshow(F)
    plt.show()
Ejemplo n.º 28
0
def ist_reconstruction(Y,
                       x_init,
                       lamda=0.01,
                       step=0.01,
                       n_iter=100,
                       verbose=1,
                       lamda_rate=1.):
    """ Iterative Shrinkage and Thresholding Based reconstruction from sparse views CT.

	Inputs:
	`Y` 	: Measured sparse views data
	`lamda` : Regularization parameter
	`n_iter`: Number of iterations
	`x_init`: Initialization
	`step`  : step size for gradient update
	`verbose`:
	`lamda_rate`: Reduce the value of `lamda` by this amount per iteration.
	"""

    # assert X.measurement != None, 'Please specify the low view measurement'

    x = x_init.copy()
    theta = np.linspace(0., 179., Y.shape[1])

    for i in range(n_iter):
        z = Y - radon(x, theta, circle=False)
        grad = -iradon(z, theta, circle=False, filter=None)

        x = prox_tv.tv1_2d(x - step * grad, lamda)
        lamda = lamda * lamda_rate

        if verbose == 1 and i % 10 == 0:
            print('Loss = ', la.norm(z))

    x_est = x

    return x_est
Ejemplo n.º 29
0
def tv_denoise(noisy_im, sigma):
    if sigma == 2:
        weight = 0.38828571428571435
    elif sigma == 5:
        weight = 1.7
    elif sigma == 10:
        weight = 4.228571428571429
    elif sigma == 20:
        weight = 10.857142857142858
    elif sigma == 30:
        weight = 19.142857142857142
    elif sigma == 40:
        weight = 24.857142857142858
    elif sigma == 60:
        weight = 38.857142857142854
    elif sigma == 80:
        weight = 48.857142857142854
    elif sigma == 100:
        weight = 56.0
    else:
        print('false')

    im_est = ptv.tv1_2d(noisy_im, w=weight)
    return im_est
Ejemplo n.º 30
0
# Load image
X = io.imread('QRbig.png')
X = ski.img_as_float(X)
X = color.rgb2gray(X)

# Introduce noise
noiseLevel = 0.2
N = util.random_noise(X, mode='gaussian', var=noiseLevel)

# Iterate over number of threads
lam=50./255.
times = []
for threads in THREADS:
    print('Filtering image with ' + str(threads) + ' threads...');
    start = time.time()
    F = ptv.tv1_2d(N, lam, n_threads=threads)
    end = time.time()
    times.append(end-start)
    print('Elapsed time ' + str(end-start))

# Plot filtering results
plt.subplot(1, 3, 1)
io.imshow(X)
plt.xlabel('Original')

plt.subplot(1, 3, 2)
io.imshow(N)
plt.title('2D TVL1 filtering')
plt.xlabel('Noisy')

plt.subplot(1, 3, 3)
import os
import cv2
import math
import prox_tv as ptv

from utils import add_gaussian_noise

here = os.path.dirname(os.path.abspath(__file__))
im = cv2.imread(here + '/colors.png', cv2.IMREAD_GRAYSCALE)

sigma = 30
noisy_im = add_gaussian_noise(im, sigma)
im_h, im_w = noisy_im.shape

lam = (sigma / 255) * math.sqrt(math.log(im_h * im_w))
print(lam)
res_im = ptv.tv1_2d(noisy_im, lam)

cv2.imwrite('res_im.png', res_im)
Ejemplo n.º 32
0
    action='store_true',
    help='Show plot')
args = parser.parse_args(sys.argv[1:])
input = args.input
lam = args.lam
output = args.output
show_flag = args.show
# Load image
X = io.imread(input)
X = ski.img_as_float(X)
X = color.rgb2gray(X)

# Filter using 2D TV-L1
print('Filtering image with 2D TV-L1... lambda:', str(lam))
start = time.time()
F = ptv.tv1_2d(X, lam)
end = time.time()
print('Elapsed time ' + str(end - start))

if show_flag:
    # Plot results
    plt.subplot(1, 2, 1)
    io.imshow(X)
    plt.xlabel('Original')

    plt.subplot(1, 2, 2)
    io.imshow(F)
    plt.xlabel('Filtered')

    plt.show()
Ejemplo n.º 33
0
from skimage import data, io, filters, color, util

# Load image
X = io.imread('colors.png')
X = ski.img_as_float(X)
X = color.rgb2gray(X)

# Introduce noise
noiseLevel = 0.01
N = util.random_noise(X, mode='speckle', var=noiseLevel)

# Filter using 2D TV-L1
lam=0.15;
print('Filtering image with 2D TV-L1...')
start = time.time()
F = ptv.tv1_2d(N, lam)
end = time.time()
print('Elapsed time ' + str(end-start))

# Plot results
plt.subplot(1, 3, 1)
io.imshow(X)
plt.xlabel('Original')

plt.subplot(1, 3, 2)
io.imshow(N)
plt.title('2D TVL1 filtering')
plt.xlabel('Noisy')

plt.subplot(1, 3, 3)
io.imshow(F)
Ejemplo n.º 34
0
from skimage import data, io, filters, color, util

# Load image
X = io.imread('colors.png')
X = ski.img_as_float(X)
X = color.rgb2gray(X)

# Introduce noise
noiseLevel = 0.01
N = util.random_noise(X, mode='speckle', var=noiseLevel)

# Filter using 2D TV-L1
lam = 0.15
print('Filtering image with 2D TV-L1...')
start = time.time()
F = ptv.tv1_2d(N, lam)
end = time.time()
print('Elapsed time ' + str(end - start))

# Plot results
plt.subplot(1, 3, 1)
io.imshow(X)
plt.xlabel('Original')

plt.subplot(1, 3, 2)
io.imshow(N)
plt.title('2D TVL1 filtering')
plt.xlabel('Noisy')

plt.subplot(1, 3, 3)
io.imshow(F)
Ejemplo n.º 35
0
def bm3d_2nd_step(sigma, img_noisy, img_basic, nWien, kWien, NWien, pWien,
                  tauMatch, useSD, tau_2D, lam):
    height, width = img_noisy.shape[0], img_noisy.shape[1]

    row_ind = ind_initialize(height - kWien + 1, nWien, pWien)
    column_ind = ind_initialize(width - kWien + 1, nWien, pWien)

    kaiserWindow = get_kaiserWindow(kWien)
    ri_rj_N__ni_nj, threshold_count = precompute_BM(img_basic,
                                                    kHW=kWien,
                                                    NHW=NWien,
                                                    nHW=nWien,
                                                    tauMatch=tauMatch)
    group_len = int(np.sum(threshold_count))
    group_3D_table = np.zeros((group_len, kWien, kWien))
    weight_table = np.ones((height, width))

    noisy_patches = image2patches(img_noisy, k=kWien,
                                  p=pWien)  # i_j_ipatch_jpatch__v
    basic_patches = image2patches(img_basic, k=kWien,
                                  p=pWien)  # i_j_ipatch_jpatch__v
    if tau_2D == 'DCT':
        fre_noisy_patches = dct_2d_forward(noisy_patches)
        fre_basic_patches = dct_2d_forward(basic_patches)
    elif tau_2D == 'BIOR':  # 'BIOR'
        fre_noisy_patches = bior_2d_forward(noisy_patches)
        fre_basic_patches = bior_2d_forward(basic_patches)
    else:
        fre_noisy_patches = np.zeros_like(noisy_patches)
        for i, patch in enumerate(noisy_patches):
            fre_noisy_patches[i] = ptv.tv1_2d(patch, lam)
        fre_basic_patches = np.zeros_like(basic_patches)
        for i, patch in enumerate(basic_patches):
            fre_basic_patches[i] = ptv.tv1_2d(patch, lam)

    fre_basic_patches = fre_basic_patches.reshape(
        (height - kWien + 1, height - kWien + 1, kWien, kWien))

    acc_pointer = 0
    for i_r in row_ind:
        for j_r in column_ind:
            nSx_r = threshold_count[i_r, j_r]
            group_3D_est = build_3D_group(fre_basic_patches,
                                          ri_rj_N__ni_nj[i_r, j_r], nSx_r)
            group_3D = group_3D_est
            group_3D = group_3D.transpose((2, 0, 1))

            group_3D_table[acc_pointer:acc_pointer + nSx_r] = group_3D
            acc_pointer += nSx_r

            if useSD:
                weight = sd_weighting(group_3D)

            weight_table[i_r, j_r] = weight

    if tau_2D == 'DCT':
        group_3D_table = dct_2d_reverse(group_3D_table)
    elif tau_2D == 'BIOR':  # 'BIOR'
        group_3D_table = bior_2d_reverse(group_3D_table)
    else:
        pass

    # for i in range(1000):
    #     patch = group_3D_table[i]
    #     print(i, '----------------------------')
    #     print(patch)
    #     cv2.imshow('', patch.astype(np.uint8))
    #     cv2.waitKey()

    group_3D_table *= kaiserWindow

    numerator = np.zeros_like(img_noisy, dtype=np.float64)
    denominator = np.zeros_like(img_noisy, dtype=np.float64)
    acc_pointer = 0
    for i_r in row_ind:
        for j_r in column_ind:
            if i_r == 264 and j_r == 264:
                print()

            nSx_r = threshold_count[i_r, j_r]
            N_ni_nj = ri_rj_N__ni_nj[i_r, j_r]
            group_3D = group_3D_table[acc_pointer:acc_pointer + nSx_r]
            acc_pointer += nSx_r
            weight = weight_table[i_r, j_r]
            for n in range(nSx_r):
                ni, nj = N_ni_nj[n]
                patch = group_3D[n]

                numerator[ni:ni + kWien, nj:nj + kWien] += patch * weight
                denominator[ni:ni + kWien,
                            nj:nj + kWien] += kaiserWindow * weight

    img_denoised = numerator / denominator
    return img_denoised
Ejemplo n.º 36
0
# Load image
X = io.imread('QRbig.png')
X = ski.img_as_float(X)
X = color.rgb2gray(X)

# Introduce noise
noiseLevel = 0.2
N = util.random_noise(X, mode='gaussian', var=noiseLevel)

# Iterate over number of threads
lam = 50. / 255.
times = []
for threads in THREADS:
    print('Filtering image with ' + str(threads) + ' threads...')
    start = time.time()
    F = ptv.tv1_2d(N, lam, n_threads=threads)
    end = time.time()
    times.append(end - start)
    print('Elapsed time ' + str(end - start))

# Plot filtering results
plt.subplot(1, 3, 1)
io.imshow(X)
plt.xlabel('Original')

plt.subplot(1, 3, 2)
io.imshow(N)
plt.title('2D TVL1 filtering')
plt.xlabel('Noisy')

plt.subplot(1, 3, 3)