一、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 的实现部分参考前文图像游程编码博客。
转载本文请联系原作者获取授权,同时请注明本文来自池鱼醉故渊博客。
1000

被折叠的 条评论
为什么被折叠?



