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()
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()
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()