def shearlet_inpainting(self, masked_img, mask):
        """
        Inpaint a patch using iterative thresholding and pyshearlab
        :param masked_img: image that has been masked : void regions set to 0
        :type masked_img: numpy array
        :param mask: mask that is 0 in void regions and 1 otherwise
        :type mask: numpy array
        :return: tuple of inpainted image patch and patch coordinates
        """
        normalized_coeffs = psl.SLnormalizeCoefficients2D(
            psl.SLsheardec2D(masked_img, self.shearlet_system),
            self.shearlet_system)
        delta = np.max(np.abs(normalized_coeffs))
        decay = self.stop_factor**(1 / (max(1, self.iterations - 1)))
        inpainted_img = np.zeros(masked_img.shape)

        for i in range(self.iterations):
            res = mask * (masked_img - inpainted_img)
            coeffs = psl.SLsheardec2D(inpainted_img + res,
                                      self.shearlet_system)
            coeffs = coeffs * (np.abs(
                psl.SLnormalizeCoefficients2D(coeffs, self.shearlet_system)) >
                               delta)
            inpainted_img = psl.SLshearrec2D(coeffs, self.shearlet_system)
            delta = delta * decay

        return inpainted_img
def shearlets(matrix):
    #  hyperparameter settings (including level at which to threshold)
    sigma = 30
    scales = 2
    thresholdingFactor = shearletThresholdFactor
    # Converting 2D matrix into flaot
    matrix = matrix.astype(float)
    X = matrix
    ## create shearlets system
    shearletSystem = pyshearlab.SLgetShearletSystem2D(0, X.shape[0],
                                                      X.shape[1], scales)
    # decomposition, produces shearlet coefficients
    coeffs = pyshearlab.SLsheardec2D(X, shearletSystem)
    # calculating Root Mean Square value of each coeficient vector that is assosiated with a pixel
    # i.e for sherlet system made out of scales = 2, produces coeficient vector of length 17 for each pixel
    # RMS value is worked out on each of these vectors and the multiplied by the vector
    # A 1xnShearlets array containing the root mean squares (L2-norm divided by
    # sqrt(X*Y)) of all shearlets stored in shearletSystem["shearlets"]. These values can be used to normalize
    # shearlet coefficients to make them comparable.
    oldCoeffs = coeffs.copy()
    weights = np.ones(coeffs.shape)
    for j in range(len(shearletSystem["RMS"])):
        weights[:, :, j] = shearletSystem["RMS"][j] * np.ones(
            (X.shape[0], X.shape[1]))
    #  Thresholding the coefficients based on the setting in the hyperparameters and RMS weights
    #  Setting coefficients to 0 for value that do not pass the threshold
    coeffs = np.real(coeffs)
    zero_indices = np.abs(coeffs) / (thresholdingFactor * weights * sigma) < 1
    coeffs[zero_indices] = 0
    # reconstruction of the signal thresholded coefficients, returning the reconsturcted signal
    Xrec = pyshearlab.SLshearrec2D(coeffs, shearletSystem)
    return Xrec
Exemplo n.º 3
0
def shearlet_frame_thresh(frame, fraction_coeff):
    n, _ = frame.shape
    frame_wt = pyshearlab.SLsheardec2D(frame, shearletSystem)
    sorted_wt = np.sort(np.ravel(abs(frame_wt)))[::-1]
    n_pix, = sorted_wt.shape
    treshold_value = sorted_wt[int(n_pix * fraction_coeff)]
    frame_wt_T = np.multiply(frame_wt, (abs(frame_wt) > treshold_value))
    frame_T = pyshearlab.SLshearrec2D(frame_wt_T, shearletSystem)
    return frame_T
def test_inverse(dtype, shearletSystem):
    """Validate the inverse."""
    X = np.random.randn(*shearletSystem['size']).astype(dtype)

    # decomposition
    coeffs = pyshearlab.SLsheardec2D(X, shearletSystem)

    # reconstruction
    Xrec = pyshearlab.SLshearrec2D(coeffs, shearletSystem)
    assert Xrec.dtype == X.dtype
    assert Xrec.shape == X.shape

    assert np.linalg.norm(X - Xrec) < 1e-5 * np.linalg.norm(X)
def test_adjoint_of_inverse(dtype, shearletSystem):
    """Validate the adjoint of the inverse."""
    X = np.random.randn(*shearletSystem['size']).astype(dtype)

    # decomposition
    coeffs = pyshearlab.SLsheardec2D(X, shearletSystem)

    # reconstruction
    Xrec = pyshearlab.SLshearrec2D(coeffs, shearletSystem)
    Xrecadj = pyshearlab.SLshearrecadjoint2D(Xrec, shearletSystem)
    assert Xrecadj.dtype == X.dtype
    assert Xrecadj.shape == coeffs.shape

    # <A^-1x, A^-1x> = <A^-* A^-1 x, x>.
    assert (pytest.approx(np.vdot(Xrec, Xrec), rel=1e-3, abs=0) ==
            np.vdot(Xrecadj, coeffs))
Exemplo n.º 6
0
toc()
tic()
print("decomposition, thresholding and reconstruction...")

# decomposition
coeffs = pyshearlab.SLsheardec2D(Xnoisy, shearletSystem)

# thresholding
oldCoeffs = coeffs.copy()
weights = np.ones(coeffs.shape)

for j in range(len(shearletSystem["RMS"])):
    weights[:, :, j] = shearletSystem["RMS"][j] * np.ones(
        (X.shape[0], X.shape[1]))
coeffs = np.real(coeffs)
j = np.abs(coeffs) / (thresholdingFactor * weights * sigma) < 1
coeffs[j] = 0

# reconstruction
Xrec = pyshearlab.SLshearrec2D(coeffs, shearletSystem)
toc()
PSNR = pyshearlab.SLcomputePSNR(X, Xrec)
print("PSNR: " + str(PSNR))
#sio.savemat("PyShearLab_DenoisingExample.mat", {"weights": weights, "XPyNoisy": Xnoisy,
# "XPyDenoised": Xrec, "PyPSNR": PSNR, "coeffThrPy": coeffs, "oldCoeffs": oldCoeffs})
plt.gray()
plt.imshow(Xrec)
plt.colorbar()
plt.show()
 def _call(self, x):
     """Return ``self(x)``."""
     with self.op.mutex:
         x = np.moveaxis(x, 0, -1)
         return pyshearlab.SLshearrec2D(x, self.op.shearlet_system)