Esempio n. 1
0
    def test_window_size_3(self):
        img = np.arange(1, 19)
        img = img.reshape(3, -1)
        expected_vectors = np.array([[1, 2, 3, 7, 8, 9, 13, 14, 15],
                                     [4, 5, 6, 10, 11, 12, 16, 17, 18]])

        npt.assert_almost_equal(vectorize(img, 3), expected_vectors)
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
Esempio n. 3
0
    def test_window_size_2(self):
        """
        Expected outcome as presented here https://i.imgur.com/wycSwR3.png
        """
        img = np.array([[1, 2, 5, 4], [3, 4, 3, 2], [4, 2, 4, 1], [3, 5, 5,
                                                                   2]])
        expected_vectors = np.array([[1, 2, 3, 4], [5, 4, 3, 2], [4, 2, 3, 5],
                                     [4, 1, 5, 2]])

        npt.assert_almost_equal(vectorize(img, 2), expected_vectors)
    def test_codes_from_vectors(self):
        from vector_quantization.vectorize import vectorize

        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)
        codes = codes_from_vectors(vectors, codebook)
        expected_codes = np.array([0, 1, 2, 0])
        npt.assert_almost_equal(expected_codes, codes)
    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 compression(img, codebook_size=2**10, window_size=4):
    vectors = vectorize(img, window_size=window_size)
    means = np.mean(vectors, axis=1, keepdims=True)
    residual_vectors = vectors - means
    initial_codebook = random_codebook(residual_vectors, length=codebook_size)
    codebook, _ = lbg(residual_vectors, initial_codebook, 50, 0.01)
    _, (codes,
        _) = mean_removal_quantize_from_codebook(residual_vectors, means,
                                                 codebook)

    codebook_bitcode = codebook_compression(img.shape, window_size, codes,
                                            codebook)
    means_bitcode = means_compression(img.shape, window_size, means)
    bitcode = codebook_bitcode + means_bitcode

    return bitcode
    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 main():
    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.lbg import lbg
    from vector_quantization.mean_removal_quantization import mean_removal_quantize
    from vector_quantization.mean_removal_quantization import mean_removal_quantize_from_codebook
    from vector_quantization.differential_encoding import (
        median_adaptive_predictor_encoding,
        median_adaptive_predictor_decoding,
    )

    import matplotlib.pyplot as plt

    img = load_image("lennagrey.bmp")

    # Performing quantization with removed means (MRVQ)
    window_size = 4
    vectors = vectorize(img, window_size=window_size)
    means = np.mean(vectors, axis=1, keepdims=True)  # mean should be in shape like smaller image.
    height, width = img.shape
    means_reshaped = means.reshape((height // window_size, width // window_size))
    means_reshaped = means_reshaped.astype(int)

    # Differential encoding means from MRVQ
    encoded_means = median_adaptive_predictor_encoding(means_reshaped)
    encoded_means = encoded_means.astype(int)

    # Compress encoded means with Golomb Coder
    bit_code = golomb_compress(encoded_means)

    print("bit_code length", len(bit_code))
    print("without compression", encoded_means.size * 9)
    print("CR", len(bit_code) / (encoded_means.size * 9))

    # Decompress encoded means with Golomb Coder
    decoded_means = golomb_decompress(bit_code)

    # Differential decoding
    decoded_image = median_adaptive_predictor_decoding(decoded_means)

    plt.imshow(decoded_image)
    plt.show()
Esempio n. 11
0
    def test_compression_decompression_with_diff_enc(self):
        """
        Integration with differential encoding
        """
        from vector_quantization.vectorize import vectorize
        from vector_quantization.differential_encoding import (
            median_adaptive_predictor_encoding,
            median_adaptive_predictor_decoding,
        )

        # Generate random image
        img = np.random.rand(24, 24) * 255

        # Performing quantization with removed means (MRVQ)
        window_size = 4
        vectors = vectorize(img, window_size=window_size)
        means = np.mean(
            vectors, axis=1,
            keepdims=True)  # mean should be in shape like smaller image.
        height, width = img.shape
        means_reshaped = means.reshape(
            (height // window_size, width // window_size))
        means_reshaped = means_reshaped.astype(int)

        # Differential encoding means from MRVQ
        encoded_means = median_adaptive_predictor_encoding(means_reshaped)
        encoded_means = encoded_means.astype(int)

        # Compress encoded means with Golomb Coder
        bit_code = golomb_compress(encoded_means)

        # Decompress encoded means with Golomb Coder
        decoded_means_diff = golomb_decompress(bit_code)

        # Differential decoding
        decoded_means = median_adaptive_predictor_decoding(decoded_means_diff)
        npt.assert_almost_equal(means_reshaped, decoded_means)
    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()