def FFIC(domain_chunks, range_chunks, inpface):
    ############################################################################
    # encode the range image using svd encoding 
    ############################################################################
    
    # encode image
    start = time.time()
    codebook = encode_svd(domain_chunks, range_chunks, verbose=False)
    svd_encoding_time = time.time() - start

    # for svd only we implement compression by 
    # dropping coefficients in the codebook
    codebook[:, COMPRESSION_FACTOR:] = 0

    # Use an input face to use as input for the reconstruction
    domain_chunks = utils.Partition(inpface, mode=MODE)

    # initialize psnr
    psnr = np.zeros(10);

    # decode image 100 times
    start = time.time()
    reconstructed_chunks = decode_svd(codebook, domain_chunks)
    reconstructed_chunks_1 = copy.deepcopy(reconstructed_chunks)
    psnr[0] = PSNR(range_image, reconstructed_chunks.image)
    
    for i in range(9):
        rec_dim = rescale(reconstructed_chunks.image, 0.5) 
        domain_chunks = utils.Partition(rec_dim, mode=MODE)
        reconstructed_chunks = decode_svd(codebook, domain_chunks)
        psnr[i+1] = PSNR(range_image, reconstructed_chunks.image)
    
    svd_decoding_time = time.time()-start

    # plot_image(reconstructed_chunks.image, 
    #     title=f"Reconstructed Image (SVD Encoding) \n {reconstructed_chunks.image.shape[0]}x{reconstructed_chunks.image.shape[0]}, {COMPRESSION_FACTOR}/{BLOCK_SIZE} Compression", 
    #     cmap='gray', y=0.97)

    ############################################################################
    # encoding and decoding time results
    ############################################################################

    print(f"svd mode encoding: {svd_encoding_time}")
    print(f"svd mode decoding: {svd_decoding_time}")

    ############################################################################
    # psnr results
    ############################################################################
    # psnr = PSNR(range_image, reconstructed_chunks.image)
    print(f"PSNR: {psnr} \t Coefficients Retained: {COMPRESSION_FACTOR}/{BLOCK_SIZE}")
    return psnr
def decode_svd(codebook, domain_blocks):
    # if domain_blocks.mode != 'equipartition4' or\
    #    domain_blocks.mode !=:
    #    raise Exception("encode_svd only supports 'equipartition4' partitioning")

    num_weights = len(domain_blocks[0].ravel())
    X = np.zeros([num_weights, len(domain_blocks)])

    for i in range(len(domain_blocks)):
        X[:, i] = np.ravel(domain_blocks[i])

    u, s, vh = randomized_svd(X, 
                            n_components=min(X.shape[1], 1000),
                            n_iter=5,
                            random_state=None)

    N = int(np.sqrt(len(codebook)))
    img = utils.init_range_image(N, domain_blocks)
    range_blocks = utils.Partition(img, mode=domain_blocks.mode)

    nrows = int(np.sqrt(num_weights))
    ncols = nrows
    for ridx, weights in enumerate(codebook):
        range_blocks[ridx] = np.dot(u, weights[:, np.newaxis])\
                             .reshape(nrows, ncols)

    return range_blocks
def decode_nmf(codebook, domain_blocks):
    # if domain_blocks.mode != 'equipartition4' or\
    #    domain_blocks.mode !=:
    #    raise Exception("encode_svd only supports 'equipartition4' partitioning")
    num_weights = len(domain_blocks[0].ravel())
    X = np.zeros([num_weights, len(domain_blocks)])

    for i in range(len(domain_blocks)):
        X[:, i] = np.ravel(domain_blocks[i])
    X = np.abs(X)
    model = NMF(n_components=16, init='random', random_state=0)
    _ = model.fit_transform(X)
    u = model.components_[:,:16]

    N = int(np.sqrt(len(codebook)))
    img = utils.init_range_image(N, domain_blocks)
    range_blocks = utils.Partition(img, mode=domain_blocks.mode)

    nrows = int(np.sqrt(num_weights))
    ncols = nrows
    for ridx, weights in enumerate(codebook):
        range_blocks[ridx] = np.dot(u, weights[:, np.newaxis])\
                             .reshape(nrows, ncols)

    return range_blocks
def decode(codebook, domain_blocks):
    """Decodes the codebook into estimates of the range blocks"""

    N = int(np.sqrt(len(codebook)))
    if N != np.sqrt(len(codebook)):
        raise Exception("Codebook size must correspond to a square range image")
    
    # create a range block partition to populate
    img = utils.init_range_image(N, domain_blocks)

    range_blocks = utils.Partition(img, mode=domain_blocks.mode)

    for ridx, (didx, permtype, alpha, t0) in enumerate(codebook):
        permtype = int(permtype)
        dref = domain_blocks[didx]
        db = permute(dref)[permtype]
        range_blocks[ridx] = (alpha * db) + t0
        
    return range_blocks
Example #5
0
        title=f"Range Image {range_image.shape[0]}x{range_image.shape[1]}",
        cmap='gray')

    ############################################################################
    # divide up the first image into chunks
    ############################################################################
    # domain image is a 50% downsampled range image
    domain_image = images.get_image(0, scale_factor=1 / 2)

    plot_image(
        domain_image,
        title=f"Domain Image {domain_image.shape[0]}x{domain_image.shape[1]}",
        cmap='gray')

    # each block is 4x4
    domain_chunks = utils.Partition(domain_image, mode=MODE)
    range_chunks = utils.Partition(range_image, mode=MODE)
    print("partitioned")

    ############################################################################
    # encode the range image
    ############################################################################
    codebook = encode_svd(domain_chunks, range_chunks, verbose=False)
    # codebook[:, 8:] = 0 # uncomment to compress representation

    # facedir = Path(__file__).resolve().parent.parent / "data" / "faces"
    if args.imagepath == 'mandrill.jpg':
        facedir = Path(__file__).resolve().parent.parent / "data"
        aaronface = Image.open(list(facedir.glob("lena_gray.jpg"))[0])
        print(aaronface)
        aaronface = np.asarray(aaronface.getdata()).reshape(512, 512)
    plot_image(range_image, 
        title=f"Range Image {range_image.shape[0]}x{range_image.shape[1]}",
        cmap='gray')

    ############################################################################
    # divide up the first image into chunks
    ############################################################################
    # domain image is a 50% downsampled range image
    domain_image = images.get_image(0, scale_factor=SCALE/2)

    plot_image(domain_image, 
        title=f"Domain Image {domain_image.shape[0]}x{domain_image.shape[1]}",
        cmap='gray')

    # each block is 4x4
    domain_chunks = utils.Partition(domain_image, mode=MODE)
    range_chunks = utils.Partition(range_image, mode=MODE)

    compression_ratios = np.arange(1/16,16/16,1/16)
    num_ratios = len(compression_ratios)


    ############################################################################
    # Use Mandrill for reconstruction
    ############################################################################
    # Initialize psnr matrix
    psnr_all_mandrill = np.zeros((num_ratios,10))

    # load an input face to use as input for the reconstruction
    inpface = load_mandrill()
    plot_image(inpface, 
Example #7
0
from context import fractal
from fractal import utils
import numpy as np

if __name__ == '__main__':
    X = np.eye(20)
    xp = utils.Partition(X, 'equipartition4')
    for i in xp:
        print(xp)
        print(xp.shape)
        print()