Ejemplo n.º 1
0
    def local_dct(matrix, dst, size=8):
        y, cr, cb = encode.split_to_three_colors(matrix)

        y_shape = encode.shape_for_contacting(y.shape, size)
        cb_shape = encode.shape_for_contacting(cb.shape, size)
        cr_shape = encode.shape_for_contacting(cr.shape, size)

        print("Split and padding to submatrices")
        y = [
            matrix
            for matrix in encode.split_matrix_into_sub_matrices(y, size)
        ]
        cr = [
            matrix
            for matrix in encode.split_matrix_into_sub_matrices(cr, size)
        ]
        cb = [
            matrix
            for matrix in encode.split_matrix_into_sub_matrices(cb, size)
        ]

        print("dct submatrices")
        y = [dct.dct(submatrix) for submatrix in y]
        cr = [dct.dct(submatrix) for submatrix in cr]
        cb = [dct.dct(submatrix) for submatrix in cb]

        print("Quantization submatrices")
        y = [dct.quantization(submatrix) for submatrix in y]
        cr = [dct.quantization(submatrix) for submatrix in cr]
        cb = [dct.quantization(submatrix) for submatrix in cb]

        print("Invert dct and UnQuantization")
        y = [
            dct.inverse_dct(dct.un_quantization(submatrix)) for submatrix in y
        ]
        cr = [
            dct.inverse_dct(dct.un_quantization(submatrix)) for submatrix in cr
        ]
        cb = [
            dct.inverse_dct(dct.un_quantization(submatrix)) for submatrix in cb
        ]

        print("Concatenate")
        y = encode.concatenate_sub_matrices_to_big_matrix(y, y_shape)
        cr = encode.concatenate_sub_matrices_to_big_matrix(cr, cr_shape)
        cb = encode.concatenate_sub_matrices_to_big_matrix(cb, cb_shape)

        print("Save")
        imagetools.save_matrix(encode.concatenate_three_colors(
            y, cr, cb, matrix),
                               mode='YCrCb',
                               dest=os.path.join(src_dir, dst + '.png'))
Ejemplo n.º 2
0
    def test_quantization(self):
        original = np.array(
            [[-415.38, -30.19, -61.20, 27.24, 56.12, -20.10, -2.39, 0.46],
             [4.47, -21.86, -60.76, 10.25, 13.15, -7.09, -8.54, 4.88],
             [-46.83, 7.37, 77.13, -24.56, -28.91, 9.93, 5.42, -5.65],
             [-48.53, 12.07, 34.10, -14.76, -10.24, 6.30, 1.83, 1.95],
             [12.12, -6.55, -13.20, -3.95, -1.87, 1.75, -2.79, 3.14],
             [-7.73, 2.91, 2.38, -5.94, -2.38, 0.94, 4.30, 1.85],
             [-1.03, 0.18, 0.42, -2.42, -0.88, -3.02, 4.12, -0.66],
             [-0.17, 0.14, -1.07, -4.19, -1.17, -0.10, 0.50, 1.68]])
        expected = np.array([[-26, -3, -6, 2, 2, -1, 0, 0],
                             [0, -2, -4, 1, 1, 0, 0, 0],
                             [-3, 1, 5, -1, -1, 0, 0, 0],
                             [-3, 1, 2, -1, 0, 0, 0, 0],
                             [1, 0, 0, 0, 0, 0, 0,
                              0], [0, 0, 0, 0, 0, 0, 0, 0],
                             [0, 0, 0, 0, 0, 0, 0, 0],
                             [0, 0, 0, 0, 0, 0, 0, 0]])
        actual = dct.quantization(original)

        np.testing.assert_array_equal(
            expected, actual,
            "The original matrix- {} converted to {} and not to {} that expected"
            .format(original, actual, expected))
Ejemplo n.º 3
0
def compress_image(src_path, dest_path, entropy=False,
                   size=8):  # pragma: no cover
    print("Reading file")
    bitmap = imagetools.get_bitmap_from_bmp(src_path)

    if entropy:
        print("Bitmap entropy: " + str(ent.entropy(bitmap)))

    print("Crop image")
    ycrcb_crop = crop_bitmap(bitmap)

    print("Converting to YCrCb")
    ycrcb_bitmap = imagetools.bgr_to_ycrcb(ycrcb_crop)

    print("Separating bitmap to Y, Cb, Cr matrices")
    y, cb, cr = split_to_three_colors(ycrcb_bitmap)

    print("Downsampling")
    cb_downsample = downsample(cb)
    cr_downsample = downsample(cr)

    y_shape = shape_for_contacting(y.shape, size)
    cb_shape = shape_for_contacting(cb_downsample.shape, size)
    cr_shape = shape_for_contacting(cr_downsample.shape, size)

    print("Splitting to 8x8 sub-matrices")
    y_split = split_matrix_into_sub_matrices(y)
    cb_split = split_matrix_into_sub_matrices(cb_downsample)
    cr_split = split_matrix_into_sub_matrices(cr_downsample)

    print("DCT")
    y_dct = [dct.DCT(sub_matrix) for sub_matrix in y_split]
    cb_dct = [dct.DCT(sub_matrix) for sub_matrix in cb_split]
    cr_dct = [dct.DCT(sub_matrix) for sub_matrix in cr_split]

    print("Quantization")
    y_quantization = [dct.quantization(submatrix) for submatrix in y_dct]
    cb_quantization = [dct.quantization(submatrix) for submatrix in cb_dct]
    cr_quantization = [dct.quantization(submatrix) for submatrix in cr_dct]

    if entropy:
        print("Compressed entropy: " + str(
            ent.entropy(
                np.array([y_quantization, cb_quantization, cr_quantization]))))

    print("UnQuantization")
    y_un_quantization = [
        dct.un_quantization(submatrix) for submatrix in y_quantization
    ]
    cb_un_quantization = [
        dct.un_quantization(submatrix) for submatrix in cb_quantization
    ]
    cr_un_quantization = [
        dct.un_quantization(submatrix) for submatrix in cr_quantization
    ]

    print("Invert DCT")
    y_invert_dct = [dct.inverse_DCT(matrix) for matrix in y_un_quantization]
    cb_invert_dct = [dct.inverse_DCT(matrix) for matrix in cb_un_quantization]
    cr_invert_dct = [dct.inverse_DCT(matrix) for matrix in cr_un_quantization]

    print("Concatenate")
    y_big = concatenate_sub_matrices_to_big_matrix(y_invert_dct, y_shape)
    cb_big = concatenate_sub_matrices_to_big_matrix(cb_invert_dct, cb_shape)
    cr_big = concatenate_sub_matrices_to_big_matrix(cr_invert_dct, cr_shape)

    print("upsample")
    cb_upsample = upsample(cb_big)
    cr_upsample = upsample(cr_big)

    new_image = concatenate_three_colors(y_big, cb_upsample, cr_upsample)

    imagetools.save_matrix(new_image, mode='YCrCb', dest=dest_path + '.png')
Ejemplo n.º 4
0
def compress_image(src_path, dest_path, entropy=False, size=8) -> bool:
    print("Reading file")
    bitmap = imagetools.get_bitmap_from_bmp(src_path)

    if entropy:
        print("Bitmap entropy: " + str(ent.entropy(bitmap)))

    print("Crop image")
    bitmap = crop_bitmap(bitmap)

    print("Converting to YCrCb")
    bitmap = imagetools.bgr_to_ycrcb(bitmap)

    print("Separating bitmap to Y, Cb, Cr matrices")
    y, cb, cr = split_to_three_colors(bitmap)

    print("Downsampling")
    cr = downsample(cr)
    cb = downsample(cb)

    y_shape = shape_for_contacting(y.shape, size)
    cr_shape = shape_for_contacting(cr.shape, size)
    cb_shape = shape_for_contacting(cb.shape, size)

    print("Splitting to {0}x{0} sub-matrices".format(size))
    y = split_matrix_into_sub_matrices(y, size)
    cr = split_matrix_into_sub_matrices(cr, size)
    cb = split_matrix_into_sub_matrices(cb, size)

    print("dct")
    y = [dct.dct(sub_matrix) for sub_matrix in y]
    cr = [dct.dct(sub_matrix) for sub_matrix in cr]
    cb = [dct.dct(sub_matrix) for sub_matrix in cb]

    print("Quantization")
    y = [dct.quantization(submatrix) for submatrix in y]
    cr = [dct.quantization(submatrix) for submatrix in cr]
    cb = [dct.quantization(submatrix) for submatrix in cb]

    if entropy:
        print("Compressed entropy: " + str(
            ent.entropy(np.dstack([np.dstack(
                y), np.dstack(cr), np.dstack(cb)]))))

    print("UnQuantization")
    y = [dct.un_quantization(submatrix) for submatrix in y]
    cr = [dct.un_quantization(submatrix) for submatrix in cr]
    cb = [dct.un_quantization(submatrix) for submatrix in cb]

    print("Invert dct")
    y = [dct.inverse_dct(matrix) for matrix in y]
    cr = [dct.inverse_dct(matrix) for matrix in cr]
    cb = [dct.inverse_dct(matrix) for matrix in cb]

    print("Concatenate")
    y = concatenate_sub_matrices_to_big_matrix(y, y_shape)
    cr = concatenate_sub_matrices_to_big_matrix(cr, cb_shape)
    cb = concatenate_sub_matrices_to_big_matrix(cb, cr_shape)

    print("upsample")
    cr = upsample(cr)
    cb = upsample(cb)

    print("concatenate")
    concatenate_three_colors(y, cr, cb, bitmap)

    # print("ycrcb_to_bgr")
    # bitmap = imagetools.ycrcb_to_bgr(bitmap)

    print("save_matrix")
    imagetools.save_matrix(bitmap, mode='YCrCb', dest=dest_path + '.png')
Ejemplo n.º 5
0
    def local_dct(y, cr, cb, dst, size=8):
        y_shape = encode.shape_for_contacting(y.shape, size)
        cb_shape = encode.shape_for_contacting(cb.shape, size)
        cr_shape = encode.shape_for_contacting(cr.shape, size)
        print("Split and padding to submatrices")
        img_channel_y_split = [
            matrix
            for matrix in encode.split_matrix_into_sub_matrices(y, size)
        ]
        img_channel_cr_split = [
            matrix
            for matrix in encode.split_matrix_into_sub_matrices(cr, size)
        ]
        img_channel_cb_split = [
            matrix
            for matrix in encode.split_matrix_into_sub_matrices(cb, size)
        ]

        print("DCT submatrices")
        img_channel_y_split = [
            dct.DCT(submatrix) for submatrix in img_channel_y_split
        ]
        img_channel_cr_split = [
            dct.DCT(submatrix) for submatrix in img_channel_cr_split
        ]
        img_channel_cb_split = [
            dct.DCT(submatrix) for submatrix in img_channel_cb_split
        ]

        if size == 8:
            print("Quantization submatrices")
            img_channel_y_split = [
                dct.quantization(submatrix)
                for submatrix in img_channel_y_split
            ]
            img_channel_cr_split = [
                dct.quantization(submatrix)
                for submatrix in img_channel_cr_split
            ]
            img_channel_cb_split = [
                dct.quantization(submatrix)
                for submatrix in img_channel_cb_split
            ]
        print("Invert DCT and UnQuantization")
        img_channel_y_split = [
            dct.inverse_DCT(dct.un_quantization(submatrix))
            for submatrix in img_channel_y_split
        ]
        img_channel_cr_split = [
            dct.inverse_DCT(dct.un_quantization(submatrix))
            for submatrix in img_channel_cr_split
        ]
        img_channel_cb_split = [
            dct.inverse_DCT(dct.un_quantization(submatrix))
            for submatrix in img_channel_cb_split
        ]

        print("Concatenate")
        y_big = encode.concatenate_sub_matrices_to_big_matrix(
            img_channel_y_split, y_shape)
        cb_big = encode.concatenate_sub_matrices_to_big_matrix(
            img_channel_cr_split, cb_shape)
        cr_big = encode.concatenate_sub_matrices_to_big_matrix(
            img_channel_cb_split, cr_shape)

        imagetools.save_matrix(encode.concatenate_three_colors(
            y_big, cb_big, cr_big),
                               mode='YCrCb',
                               dest=os.path.join(src_dir, dst + '.png'))