[二维压缩] | 条件图像游程编码

部署运行你感兴趣的模型镜像


一、Run-Length Encoding

这种数据压缩方法背后的思想是:如果一个数据项d在输入流中连续出现n次,则用单对nd替换n次出现。然后,一个数据项的连续出现被称为运行长度为n,这种进行数据压缩的方法被称为运行长度编码或RLE。我们首先将这个想法应用于文本压缩,然后再应用于图像压缩。

二、图像游程编码

1.条件图像游程编码方法

假设一个具有n个灰度的图像,该方法首先根据每个像素的近邻为每个像素分配一个n位代码。然后它将n位代码转换为一个长字符串,并计算运行长度。运行长度分配在压缩流上的前缀代码。该方法将图像中的每条扫描线都视为一个二阶马尔可夫模型。在这样一个模型中,当前数据项的值仅取决于它过去的两个邻居,而不一定是两个直接的邻居。一组训练图像用于计算邻居a,b的每一对可能的值,即X的每个值出现的次数。如果A和B有相似的值,那么很自然地期望X会有相似的值。如果A和B有非常不同的值,我们期望X有许多不同的值,每个值的概率都很低在这里插入图片描述

2. 条件图像游程编码代码实现


def int2bin(n, count):
    """returns the binary of integer n,using count number of digits"""
    return "".join([str((n >> y) & 1) for y in range(count - 1, -1, -1)])


class Run_Length_Encoding:
    def __init__(self):
        self.lowbit = '0'
        self.highbit = '1'
        self.compression_factor_text = 3
        self.escape_character = '@'
        self.alphabet_bit = 14

    def condictional_run_length_encoding_image(self, input: list):
        import random
        approch_run_length_encoding = self.run_length_encoding_grayimage(input)
        # approch_run_length_encoding = [random.randint(0, 15) for i in range(100)]  # 做随机测试
        from Huffman import Huffman
        dic = Huffman().dictionary(approch_run_length_encoding)
        Huffmandic = Huffman().huffman_code(dic)


        A_list = [elem[:] for elem in dic.keys()]
        B_list = [elem[:] for elem in dic.keys()]
        C_list = []
        Out_list = []
        Name_list = []

        for i in A_list:
            for j in B_list:
                for c in range(len(approch_run_length_encoding) - 2):
                    if approch_run_length_encoding[c] == int(i) and approch_run_length_encoding[c + 1] == int(j):
                        C_list.append(approch_run_length_encoding[c + 2])
                C_dic = Huffman().dictionary(C_list)
                C_dic = Huffman().huffman_code(C_dic)
                C_list = []
                Out_list.append(C_dic) if len(C_dic) != 0 else None
                Name_list.append(i + "+" + j) if len(C_dic) != 0 else None

        condictional_rle_outstring = int2bin(approch_run_length_encoding[0], self.alphabet_bit) + int2bin(
            approch_run_length_encoding[1], self.alphabet_bit)

        Huffman_outstring = ''

        for k in range(2, len(approch_run_length_encoding)):
            alphabet = Out_list[Name_list.index(
                str(approch_run_length_encoding[k - 2]) + "+" + str(approch_run_length_encoding[k - 1]))]
            condictional_rle_outstring += alphabet[str(approch_run_length_encoding[k])]

        for k in range(len(approch_run_length_encoding)):
            Huffman_outstring += Huffmandic[str(approch_run_length_encoding[k])]

        print("Huffman_outstring : ", len(Huffman_outstring), Huffman_outstring)
        print("condictional_rle_outstring : ", len(condictional_rle_outstring), condictional_rle_outstring)

input =  [[0, 0, 0, 1], [1, 1, 1, 0], [1, 1, 1, 0]]
output :  [3, 4, 1, 3, 1]

总结

以上就是今天要讲的内容,本文仅仅简单介绍了条件图像游程编码的相关定义和其中简单代码实的实现方法。


参考

[1] Salomon, David, and Giovanni Motta. Handbook of data compression. London; New York: Springer, 2010.
[2] H. Gharavi, “Conditional Run-Length and Variable-Length Coding of Digital Pictures,” in IEEE Transactions on Communications, vol. 35, no. 6, pp. 671-677, June 1987, doi:10.1109/TCOM.1987.1096817.

本文部分参考自《Handbook of Data Compression 》,该内容仅供学习研究之用,如若喜欢请支持购买正品书籍。本文关于Huffman部分的代码参考博客Python 代码实现哈夫曼编码。本文关于run_length_encoding_grayimage 的实现部分参考前文图像游程编码博客。
转载本文请联系原作者获取授权,同时请注明本文来自池鱼醉故渊博客。

您可能感兴趣的与本文相关的镜像

Python3.10

Python3.10

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值