示例#1
0
def problem_5():
    img = im.read_gecko_image()
    plt.figure()
    plt.subplot(1, 3, 1)
    plt.title('Original Image')
    plt.imshow(img, cm.Greys_r)

    # Forward Transform
    img_fwd = copy.deepcopy(img)
    dim = max(img.shape)
    while dim >= 8:
        P = wv.permutation_matrix(dim)
        T_a = wv.cdf_24_encoding_transform(dim)
        img_fwd[:dim, :dim] = P.dot(T_a).dot(
            img_fwd[:dim, :dim]).dot(T_a.T).dot(P.T)
        dim = dim / 2

    plt.subplot(1, 3, 2)
    plt.title('Transformed Image')
    plt.imshow(img_fwd, cm.Greys_r)

    # Threshold + Encode
    t, ltmax = quant.log_thresh(img_fwd, cutoff=0.98)
    img_encode = quant.encode(img_fwd, t, ltmax)

    # Store to file
    filename = 'encoded_image'
    img_encode.tofile(filename)
    file_size = os.stat(filename).st_size

    # Compress File
    subprocess.call(['gzip', filename])
    c_file_size = os.stat(filename + '.gz').st_size

    # Decompress File
    subprocess.call(['gunzip', filename + '.gz'])

    # # Read from file
    img_encode = np.fromfile(filename).reshape(img.shape)

    # Decode Image
    img_decode = quant.decode(img_encode, t, ltmax)

    # Inverse Transform
    img_inv = copy.deepcopy(img_decode)
    dim = 8
    while dim <= max(img.shape):
        P = wv.permutation_matrix(dim)
        T_b = wv.cdf_24_decoding_transform(dim)
        img_inv[:dim, :dim] = T_b.T.dot(P.T).dot(
            img_inv[:dim, :dim]).dot(P).dot(T_b)
        dim = dim * 2

    plt.subplot(1, 3, 3)
    plt.title('Recreated Image')
    plt.imshow(img_inv, cm.Greys_r)
    plt.show()

    print "Compression Level: %s" % (1 - float(c_file_size) / float(file_size))
def homework_3_problem_3():
    # load image
    img = I.read_gecko_image()
    org = deepcopy(img)

    fig = plt.gcf()
    fig.suptitle("Problem 3: Lloyd's Algorithm")

    plt.subplot(221)
    plt.title('original')
    plt.imshow(org, cm.Greys_r)

    # Apply haar transformation
    wavelet.forward(h, img)
    plt.subplot(222)
    plt.title('forward')
    plt.imshow(img, cm.Greys_r)

    # store sign and apply thresholding
    img, sign = quantization.thresh(img, cutoff=0.8)

    # logarithmic codebook guess
    x = img[np.where(img > 0)]
    init = np.array(
        [min(x) * 2**i for i in range(int(np.ceil(np.log2(max(x)/min(x)))))])

    # Apply k-means to find optimal codebook
    codebook, distortion = vq.kmeans(np.sort(img[np.where(img > 0)]), init)

    # Encode image
    img, dist = vq.vq(img, codebook)

    # Decode image
    img = np.array([s*codebook[i] for s, i in zip(sign, img)])
    img = img.reshape([256, 256])

    plt.subplot(223)
    plt.title('after quant')
    plt.imshow(img, cm.Greys_r)

    # Revert haar transformation
    wavelet.inverse(h, img)

    plt.subplot(224)
    plt.title('reverse')
    plt.imshow(img, cm.Greys_r)
    plt.show()
import utilities.wavelet as wavelet
import utilities.quantization as quantization
import utilities.image as I
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from copy import deepcopy
from math import sqrt

img = I.read_gecko_image()
org = deepcopy(img)

fig = plt.gcf()
fig.suptitle('Logarithmic quantizationuantization')

plt.subplot(231)
plt.title('original')
plt.imshow(org, cm.Greys_r)

haar_coefficients = [1/sqrt(2), 1/sqrt(2)]

wavelet.forward(h=haar_coefficients, image=img)
plt.subplot(232)
plt.title('forward')
plt.imshow(img, cm.Greys_r)

offset = 4
for idx, th in enumerate([0.8, 0.9, 0.95]):
    img_cp = deepcopy(img)
    thresh, lmaxt = quantization.log_thresh(img_cp, th)
    img_cp = quantization.encode(img_cp, thresh, lmaxt)
    img_cp = quantization.decode(img_cp, thresh, lmaxt)
import utilities.wavelet as wavelet
import utilities.quantization as quantization
import utilities.image as I
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from copy import deepcopy
from math import sqrt

img = I.read_gecko_image()
org = deepcopy(img)

fig = plt.gcf()
fig.suptitle('Logarithmic quantizationuantization')

plt.subplot(231)
plt.title('original')
plt.imshow(org, cm.Greys_r)

haar_coefficients = [1 / sqrt(2), 1 / sqrt(2)]

wavelet.forward(h=haar_coefficients, image=img)
plt.subplot(232)
plt.title('forward')
plt.imshow(img, cm.Greys_r)

offset = 4
for idx, th in enumerate([0.8, 0.9, 0.95]):
    img_cp = deepcopy(img)
    thresh, lmaxt = quantization.log_thresh(img_cp, th)
    img_cp = quantization.encode(img_cp, thresh, lmaxt)
    img_cp = quantization.decode(img_cp, thresh, lmaxt)