def quantize(image, window_size, codebook_fun, codebook_size, verbose=False):
    quantized_img = image.copy()
    vectors = vectorize(quantized_img, window_size=window_size)
    codebook = codebook_fun(vectors, length=codebook_size)

    quantized_vectors = quantize_from_codebook(vectors, codebook)

    quantized_img = image_from_vectors(quantized_vectors, quantized_img)
    return quantized_img
    def test_quantize(self):
        from vector_quantization.codebooks import random_codebook
        from vector_quantization.vectorize import vectorize, image_from_vectors

        codebook = np.array([[1, 1, 1, 1], [2, 2, 2, 2], [5, 5, 5, 5]])
        image = np.array([[1, 1, 3, 3], [1, 1, 3, 3], [4, 4, 0, 0],
                          [4, 4, 0, 0]])
        vectors = vectorize(image, 2)
        quantized_vectors = quantize_from_codebook(vectors, codebook)
        quantized_image = image_from_vectors(quantized_vectors, image)
        expected_quantized_image = np.array([[1, 1, 2, 2], [1, 1, 2, 2],
                                             [5, 5, 1, 1], [5, 5, 1, 1]])
        npt.assert_almost_equal(quantized_image, expected_quantized_image)
def mean_removal_quantize(image,
                          window_size,
                          codebook_fun,
                          codebook_size,
                          verbose=False):
    quantized_img = image.copy()
    vectors = vectorize(quantized_img, window_size=window_size)
    means = np.mean(vectors, axis=1, keepdims=True)
    residual_vectors = vectors - means
    initial_codebook = codebook_fun(residual_vectors, length=codebook_size)
    codebook, distortions = lbg(residual_vectors, initial_codebook, 50, 0.01)
    quantized_vectors, (codes, _) = mean_removal_quantize_from_codebook(
        residual_vectors, means, codebook), img.copy()
    quantized_img = image_from_vectors(quantized_vectors)
    return quantized_img, (codes, means)
    def test_quantize(self):
        from vector_quantization.codebooks import random_codebook
        from vector_quantization.vectorize import vectorize, image_from_vectors

        codebook = np.array([[1, 1, 1, 1], [2, 2, 2, 2], [5, 5, 5, 5]])
        image = np.array([[1, 1, 3, 3], [1, 1, 3, 3], [4, 4, 0, 0],
                          [4, 4, 0, 0]])
        vectors = vectorize(image, 2)
        means = np.mean(vectors, axis=1, keepdims=True)
        residual_vectors = vectors - means
        quantized_vectors, _ = mean_removal_quantize_from_codebook(
            residual_vectors, means, codebook)
        quantized_image = image_from_vectors(quantized_vectors, image)
        expected_quantized_image = np.array([[2, 2, 4, 4], [2, 2, 4, 4],
                                             [5, 5, 1, 1], [5, 5, 1, 1]])
        npt.assert_almost_equal(quantized_image, expected_quantized_image)
    def test_vectors_from_codes(self):
        from vector_quantization.vectorize import vectorize, image_from_vectors

        codes = np.array([0, 1, 2, 0])
        codebook = np.array([[1, 1, 1, 1], [2, 2, 2, 2], [5, 5, 5, 5]])

        expected_quantized_image = np.array([[1, 1, 2, 2], [1, 1, 2, 2],
                                             [5, 5, 1, 1], [5, 5, 1, 1]])
        expected_vectors = vectorize(expected_quantized_image, 2)

        vectors = vectors_from_codes(codes, codebook)
        npt.assert_almost_equal(expected_vectors, vectors)

        quantized_image = image_from_vectors(vectors, expected_quantized_image)
        expected_quantized_image = np.array([[1, 1, 2, 2], [1, 1, 2, 2],
                                             [5, 5, 1, 1], [5, 5, 1, 1]])
        npt.assert_almost_equal(quantized_image, expected_quantized_image)
def decompression(bitcode):
    img_shape, window_size, codes, codebook, ptr = codebook_decompression(
        bitcode)

    # Recreate residual vectors from codebook
    residual_vectors = vectors_from_codes(codes, codebook)

    # Means decompression (golomb decompression and differential decoding)
    means_bitcode = bitcode[ptr:]
    means = means_decompression(means_bitcode, window_size)

    # Recreating vectors from residual vectors and means
    vectors = residual_vectors + means

    img_tmp = np.zeros(img_shape)

    img = image_from_vectors(vectors, img_tmp)
    return img
    residual_vectors = vectors - means
    initial_codebook = codebook_fun(residual_vectors, length=codebook_size)
    codebook, distortions = lbg(residual_vectors, initial_codebook, 50, 0.01)
    quantized_vectors, (codes, _) = mean_removal_quantize_from_codebook(
        residual_vectors, means, codebook), img.copy()
    quantized_img = image_from_vectors(quantized_vectors)
    return quantized_img, (codes, means)


def mean_removal_quantize_from_codebook(vectors, means, codebook):
    quantized_vectors = np.zeros_like(vectors)
    codes, _ = vq(vectors, codebook)
    for idx, vector in enumerate(vectors):
        quantized_vectors[idx, :] = codebook[codes[idx], :] + means[idx]
    return quantized_vectors, (codes, means)


if __name__ == "__main__":
    img = load_image("balloon.bmp")
    vectors = vectorize(img, window_size=4)
    means = np.mean(vectors, axis=1, keepdims=True)
    residual_vectors = vectors - means
    initial_codebook = random_codebook(residual_vectors, length=32)
    codebook, distortions = lbg(residual_vectors, initial_codebook, 50, 0.01)
    quantized_vectors, (codes, _) = mean_removal_quantize_from_codebook(
        residual_vectors, means, codebook)
    quantized_img_lbg = image_from_vectors(quantized_vectors, img.copy())

    print("PSNR:", PSNR(img, quantized_img_lbg))
    Image.fromarray(quantized_img_lbg).show()
Example #8
0
from PIL import Image

from vector_quantization.metrics import PSNR
from vector_quantization.image import load_image, save_image
from vector_quantization.codebooks import random_codebook
from vector_quantization.vectorize import vectorize, image_from_vectors
from vector_quantization.quantization import quantize, quantize_from_codebook
from vector_quantization.lbg import lbg

if __name__ == "__main__":
    img = load_image("balloon.bmp")
    vectors = vectorize(img, window_size=4)
    initial_codebook = random_codebook(vectors, length=32)
    codebook, distortions = lbg(vectors, initial_codebook, 50, 0.01)
    quantized_img_lbg = image_from_vectors(quantize_from_codebook(vectors, codebook), img.copy())
    Image.fromarray(quantized_img_lbg).show()

    print(codebook)
    print(distortions)