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
Example #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_call(dtype, shearletSystem):
    """Validate the regular call."""
    shape = tuple(shearletSystem['size'])

    # load data
    X = np.random.randn(*shape).astype(dtype)

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

    # Test parameters
    assert coeffs.dtype == X.dtype
    assert coeffs.shape == shape + (shearletSystem['nShearlets'],)
def test_inverse_of_adjoint(dtype, shearletSystem):
    """Validate the (pseudo-)inverse of the adjoint."""
    X = np.random.randn(*shearletSystem['size']).astype(dtype)

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

    # Validate that the inverse works.
    Xadj = pyshearlab.SLshearadjoint2D(coeffs, shearletSystem)
    Xadjrec = pyshearlab.SLshearrecadjoint2D(Xadj, shearletSystem)
    assert Xadjrec.dtype == X.dtype
    assert Xadjrec.shape == coeffs.shape

    assert np.linalg.norm(coeffs - Xadjrec) < 1e-5 * np.linalg.norm(coeffs)
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))
def test_adjoint(dtype, shearletSystem):
    """Validate the adjoint."""
    shape = tuple(shearletSystem['size'])

    # load data
    X = np.random.randn(*shape).astype(dtype)

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

    # adjoint
    Xadj = pyshearlab.SLshearadjoint2D(coeffs, shearletSystem)
    assert Xadj.dtype == X.dtype
    assert Xadj.shape == X.shape

    # <Ax, Ax> should equal <x, AtAx>
    assert (pytest.approx(np.vdot(coeffs, coeffs), rel=1e-3, abs=0) ==
            np.vdot(X, Xadj))
Example #9
0
# add noise
Xnoisy = X + sigma * np.random.randn(X.shape[0], X.shape[1])
toc()

tic()
print("generating shearlet system...")
## create shearlets
shearletSystem = pyshearlab.SLgetShearletSystem2D(0, X.shape[0], X.shape[1],
                                                  scales)

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()
 def _call(self, x):
     """Return ``self(x)``."""
     with self.mutex:
         result = pyshearlab.SLsheardec2D(x, self.shearlet_system)
         return np.moveaxis(result, -1, 0)