# Parameters.
IMAGE_PATH = "../../data/"
IMAGE_NAME = "lenna.png"
BLOCK_SIZE = 30
RHO = 1.0
ALPHA = 1.0
BASIS_OVERSAMPLING = 1.0

if __name__ == "__main__":

    # Import the image.
    img = misc.imresize(bf.rgb2gray(bf.imread(IMAGE_PATH + IMAGE_NAME)),
                        (60, 60)).astype(np.float32)
    
    # Get blocks.
    blocks = sketch.getBlocks(img, BLOCK_SIZE)
    print "Got %d blocks." % len(blocks)
    
    
    # Compress each block.
    print "Running CS on each block..."
    basis, block_coefficients = sketch.basisCompressedSenseDCTHuber(blocks,
                                                                    RHO,
                                                                    ALPHA,
                                                                    BASIS_OVERSAMPLING)

    # Get sparsity.
    sparsity = sketch.computeSparsity(block_coefficients)
    print "Sparsity: " + str(sparsity)
    
    # Compute reconstruction for each block.
from scipy import misc
import BasicFunctions as bf
import Sketching as sketch

# Parameters.
IMAGE_PATH = "../../data/"
IMAGE_NAME = "lenna.png"
SIZE = (50, 50)
ALPHA = 2
BASIS_OVERSAMPLING = 1.0

# Import the image.
img = misc.imresize(bf.rgb2gray(bf.imread(IMAGE_PATH + IMAGE_NAME)), SIZE)

# Obtain Fourier basis.
basis, coefficients = sketch.basisSketchL1(img, ALPHA, BASIS_OVERSAMPLING)

# Compute reconstruction.
reconstruction = (basis * coefficients).reshape(img.shape)

# Plot.
plt.figure(1)
plt.subplot(121)
plt.imshow(reconstruction, cmap="gray")

max_value = np.absolute(coefficients).max()
plt.title(
    "Reconstruction using random basis \n in image domain \n %.2f%% sparsity" %
    (100.0 - ((np.absolute(coefficients) > 0.01 * max_value).sum() * 100.0 /
              (SIZE[0] * SIZE[1]))))
from scipy import  misc
import BasicFunctions as bf
import Sketching as sketch

# Parameters.
IMAGE_PATH = "../../data/"
IMAGE_NAME = "lenna.png"
SIZE = (50, 50)
ALPHA = 2
BASIS_OVERSAMPLING = 1

# Import the image.
img = misc.imresize(bf.rgb2gray(bf.imread(IMAGE_PATH + IMAGE_NAME)), SIZE).astype(np.float32)

# Obtain Fourier basis.
basis, coefficients = sketch.basisSketchDCTL1(img, ALPHA, BASIS_OVERSAMPLING)

# Compute reconstruction.
reconstruction = (basis * coefficients).reshape(img.shape)
    
# Plot.
max_value = np.absolute(coefficients).max()
plt.figure(1)
plt.subplot(121)
plt.imshow(reconstruction, cmap="gray")

plt.title("Reconstruction using random basis \n in DCT domain \n %.2f%% sparsity" %
           (100.0-((np.absolute(coefficients) > 0.01 * max_value).sum()*100.0/(SIZE[0]*SIZE[1]))))


ax = plt.subplot(122)
from scipy import  misc
import BasicFunctions as bf
import Sketching as sketch

# Parameters.
IMAGE_PATH = "../../data/"
IMAGE_NAME = "lenna.png"
SIZE = (50, 50)
ALPHA = 100.0
BASIS_OVERSAMPLING = 1.0

# Import the image.
img = misc.imresize(bf.rgb2gray(bf.imread(IMAGE_PATH + IMAGE_NAME)), SIZE).astype(np.float32)

# Obtain Fourier basis.
basis, coefficients = sketch.basisCompressedSenseImgL1(img, ALPHA, BASIS_OVERSAMPLING)

# Compute reconstruction.
reconstruction = (basis * coefficients).reshape(img.shape)
    
# print estimate of sparsity
print np.median(np.asarray(coefficients.T))

# Plot.
max_value = np.absolute(coefficients).max()
plt.figure(1)
plt.subplot(121)
plt.imshow(reconstruction, cmap="gray")
plt.title("Reconstruction using image basis \n %.2f%% sparsity" %
           (100.0-((np.absolute(coefficients) > 0.01*max_value).sum()*100.0/(SIZE[0]*SIZE[1]))))
Exemple #5
0
"""
Test script. Run Fourier decomposition on a 1D signal and see what we get.
"""

import numpy as np
import matplotlib.pyplot as plt
import Sketching as sketch

# Parameters.
LENGTH = 1000
K = 1

# Generate a random 1D signal.
signal = np.random.randn(LENGTH)

# Obtain Fourier basis.
basis, coefficients = sketch.basisFourier(signal, K)

# Compute reconstruction.
reconstruction = np.zeros(LENGTH)
for i in range(K):
    component = basis[:, i] * coefficients[i]
    reconstruction += np.real(component)

# Plot.
plt.plot(signal, 'b')
plt.plot(reconstruction, 'r-')
plt.title("Reconstructing a random signal with %d Fourier basis vectors." % K)
plt.show()
"""
Test script. Run Fourier decomposition on a 1D signal and see what we get.
"""

import numpy as np
import matplotlib.pyplot as plt
import Sketching as sketch

# Parameters.
LENGTH = 1000
K = 1

# Generate a random 1D signal.
signal = np.random.randn(LENGTH)

# Obtain Fourier basis.
basis, coefficients = sketch.basisFourier(signal, K)

# Compute reconstruction.
reconstruction = np.zeros(LENGTH)
for i in range(K):
    component = basis[:,i] * coefficients[i]
    reconstruction += np.real(component)

# Plot.
plt.plot(signal, 'b')
plt.plot(reconstruction, 'r-')
plt.title("Reconstructing a random signal with %d Fourier basis vectors." % K)
plt.show()
"""
Test script. Run sketching with L1 penalization on a 1D signal.
"""

import numpy as np
import matplotlib.pyplot as plt
import Sketching as sketch

# Parameters.
LENGTH = 1000
K = 100
ALPHA = 10.0

# Generate a random 1D signal.
signal = np.random.randn(LENGTH)

# Obtain Fourier basis.
basis, coefficients = sketch.basisSketchL1(signal, K, ALPHA)

# Compute reconstruction.
reconstruction = basis * coefficients

# Plot.
plt.plot(signal, 'b')
plt.plot(reconstruction, 'r-')
plt.title("Reconstructing a random signal with %d random basis vectors." % K)
plt.show()
Exemple #8
0
import BasicFunctions as bf
import Sketching as sketch

# Parameters.
IMAGE_PATH = "../../data/"
IMAGE_NAME = "lenna.png"
SIZE = (50, 50)
ALPHA = 100.0
BASIS_OVERSAMPLING = 1.0

# Import the image.
img = misc.imresize(bf.rgb2gray(bf.imread(IMAGE_PATH + IMAGE_NAME)),
                    SIZE).astype(np.float32)

# Obtain Fourier basis.
basis, coefficients = sketch.basisCompressedSenseImgL1(img, ALPHA,
                                                       BASIS_OVERSAMPLING)

# Compute reconstruction.
reconstruction = (basis * coefficients).reshape(img.shape)

# print estimate of sparsity
print np.median(np.asarray(coefficients.T))

# Plot.
max_value = np.absolute(coefficients).max()
plt.figure(1)
plt.subplot(121)
plt.imshow(reconstruction, cmap="gray")
plt.title("Reconstruction using image basis \n %.2f%% sparsity" %
          (100.0 -
           ((np.absolute(coefficients) > 0.01 * max_value).sum() * 100.0 /
# Parameters.
IMAGE_PATH = "../../data/"
IMAGE_NAME = "lenna.png"
BLOCK_SIZE = 10
ALPHA = 1.0
BASIS_OVERSAMPLING = 1.0
OVERLAP_PERCENT = 0.5;

if __name__ == "__main__":

    # Import the image.
    img = misc.imresize(bf.rgb2gray(bf.imread(IMAGE_PATH + IMAGE_NAME)), (20, 20)).astype(np.float32)

    
    # Get blocks.
    blocks = sketch.getBlocks_withOverlap(img, BLOCK_SIZE, OVERLAP_PERCENT)
    print "Got %d blocks." % len(blocks)
    
    
    # Compress each block.
    print "Running CS on each block..."
    basis, block_coefficients = sketch.basisCompressedSenseDCTL1(blocks,
                                                                 ALPHA,
                                                                 BASIS_OVERSAMPLING)

    # Get sparsity.
    sparsity = sketch.computeSparsity(block_coefficients)
    print "Sparsity: " + str(sparsity)
    
    # Compute reconstruction for each block.
    print "Reconstructing..."
Exemple #10
0
# Parameters.
IMAGE_PATH = "../../data/"
IMAGE_NAME = "lenna.png"
BLOCK_SIZE = 10
ALPHA = 1.0
BASIS_OVERSAMPLING = 1.0
OVERLAP_PERCENT = 0.5

if __name__ == "__main__":

    # Import the image.
    img = misc.imresize(bf.rgb2gray(bf.imread(IMAGE_PATH + IMAGE_NAME)),
                        (20, 20)).astype(np.float32)

    # Get blocks.
    blocks = sketch.getBlocks_withOverlap(img, BLOCK_SIZE, OVERLAP_PERCENT)
    print "Got %d blocks." % len(blocks)

    # Compress each block.
    print "Running CS on each block..."
    basis, block_coefficients = sketch.basisCompressedSenseDCTL1(
        blocks, ALPHA, BASIS_OVERSAMPLING)

    # Get sparsity.
    sparsity = sketch.computeSparsity(block_coefficients)
    print "Sparsity: " + str(sparsity)

    # Compute reconstruction for each block.
    print "Reconstructing..."
    reconstructed_blocks = []
    for i, coefficients in enumerate(block_coefficients):
Exemple #11
0
from scipy import misc
import BasicFunctions as bf
import Sketching as sketch

# Parameters.
IMAGE_PATH = "../../data/"
IMAGE_NAME = "lenna.png"
SIZE = (50, 50)
K = (SIZE[0] * SIZE[1] * 0.75, SIZE[0] * SIZE[1] * 0.5,
     SIZE[0] * SIZE[1] * 0.25)

# Import the image.
img = misc.imresize(bf.rgb2gray(bf.imread(IMAGE_PATH + IMAGE_NAME)), SIZE)

# Obtain Fourier basis.
basis, coefficients = sketch.basisFourier(img, K[0])

# Compute reconstruction.
reconstruction1 = (basis * coefficients).reshape(img.shape)
reconstruction1 = np.absolute(reconstruction1)

# Obtain Fourier basis.
basis, coefficients = sketch.basisFourier(img, K[1])

# Compute reconstruction.
reconstruction2 = (basis * coefficients).reshape(img.shape)
reconstruction2 = np.absolute(reconstruction2)

# Obtain Fourier basis.
basis, coefficients = sketch.basisFourier(img, K[2])
import matplotlib.pyplot as plt
from scipy import  misc
import BasicFunctions as bf
import Sketching as sketch

# Parameters.
IMAGE_PATH = "../../data/"
IMAGE_NAME = "lenna.png"
SIZE = (50, 50)
K = (SIZE[0]*SIZE[1]*0.75, SIZE[0]*SIZE[1]*0.5, SIZE[0]*SIZE[1]*0.25)

# Import the image.
img = misc.imresize(bf.rgb2gray(bf.imread(IMAGE_PATH + IMAGE_NAME)), SIZE).astype(np.float32)

# Obtain Fourier basis.
basis, coefficients = sketch.basisDCT(img, K[0])

# Compute reconstruction.
reconstruction1 = (basis * coefficients).reshape(img.shape)
reconstruction1 = np.absolute(reconstruction1)

# Obtain Fourier basis.
basis, coefficients = sketch.basisDCT(img, K[1])

# Compute reconstruction.
reconstruction2 = (basis * coefficients).reshape(img.shape)
reconstruction2 = np.absolute(reconstruction2)

# Obtain Fourier basis.
basis, coefficients = sketch.basisDCT(img, K[2])
# Parameters.
IMAGE_PATH = "../../data/"
IMAGE_NAME = "lenna.png"
BLOCK_SIZE = 30
RHO = 1.0
ALPHA = 1.0
BASIS_OVERSAMPLING = 1.0

if __name__ == "__main__":

    # Import the image.
    img = misc.imresize(bf.rgb2gray(bf.imread(IMAGE_PATH + IMAGE_NAME)),
                        (60, 60)).astype(np.float32)

    # Get blocks.
    blocks = sketch.getBlocks(img, BLOCK_SIZE)
    print "Got %d blocks." % len(blocks)

    # Compress each block.
    print "Running CS on each block..."
    basis, block_coefficients = sketch.basisCompressedSenseDCTHuber(
        blocks, RHO, ALPHA, BASIS_OVERSAMPLING)

    # Get sparsity.
    sparsity = sketch.computeSparsity(block_coefficients)
    print "Sparsity: " + str(sparsity)

    # Compute reconstruction for each block.
    print "Reconstructing..."
    reconstructed_blocks = []
    for i, coefficients in enumerate(block_coefficients):
Exemple #14
0
import BasicFunctions as bf
import Sketching as sketch

# Parameters.
IMAGE_PATH = "../../data/"
IMAGE_NAME = "lenna.png"
SIZE = (50, 50)
K = (SIZE[0] * SIZE[1] * 0.75, SIZE[0] * SIZE[1] * 0.5,
     SIZE[0] * SIZE[1] * 0.25)

# Import the image.
img = misc.imresize(bf.rgb2gray(bf.imread(IMAGE_PATH + IMAGE_NAME)),
                    SIZE).astype(np.float32)

# Obtain Fourier basis.
basis, coefficients = sketch.basisDCT(img, K[0])

# Compute reconstruction.
reconstruction1 = (basis * coefficients).reshape(img.shape)
reconstruction1 = np.absolute(reconstruction1)

# Obtain Fourier basis.
basis, coefficients = sketch.basisDCT(img, K[1])

# Compute reconstruction.
reconstruction2 = (basis * coefficients).reshape(img.shape)
reconstruction2 = np.absolute(reconstruction2)

# Obtain Fourier basis.
basis, coefficients = sketch.basisDCT(img, K[2])
import matplotlib.pyplot as plt
from scipy import  misc
import BasicFunctions as bf
import Sketching as sketch

# Parameters.
IMAGE_PATH = "../../data/"
IMAGE_NAME = "lenna.png"
SIZE = (50, 50)
K = (SIZE[0]*SIZE[1]*0.75, SIZE[0]*SIZE[1]*0.5, SIZE[0]*SIZE[1]*0.25)

# Import the image.
img = misc.imresize(bf.rgb2gray(bf.imread(IMAGE_PATH + IMAGE_NAME)), SIZE)

# Obtain Fourier basis.
basis, coefficients = sketch.basisFourier(img, K[0])

# Compute reconstruction.
reconstruction1 = (basis * coefficients).reshape(img.shape)
reconstruction1 = np.absolute(reconstruction1)

# Obtain Fourier basis.
basis, coefficients = sketch.basisFourier(img, K[1])

# Compute reconstruction.
reconstruction2 = (basis * coefficients).reshape(img.shape)
reconstruction2 = np.absolute(reconstruction2)

# Obtain Fourier basis.
basis, coefficients = sketch.basisFourier(img, K[2])