Exemplo n.º 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))
Exemplo n.º 2
0
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)
    wavelet.inverse(h=haar_coefficients, image=img_cp)

    plt.subplot(eval('23%s' % (offset + idx)))
    plt.title('%s' % th)
    plt.imshow(img_cp, cm.Greys_r)

plt.show()
Exemplo n.º 3
0
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)
    wavelet.inverse(h=haar_coefficients, image=img_cp)

    plt.subplot(eval('23%s' % (offset + idx)))
    plt.title('%s' % th)
    plt.imshow(img_cp, cm.Greys_r)

plt.show()