一、Run-Length Encoding
这种数据压缩方法背后的思想是:如果一个数据项d在输入流中连续出现n次,则用单对nd替换n次出现。然后,一个数据项的连续出现被称为运行长度为n,这种进行数据压缩的方法被称为运行长度编码或RLE。我们首先将这个想法应用于文本压缩,然后再应用于图像压缩。
二、图像游程编码
1.图像游程编码方法
RLE是压缩图形数据的自然候选对象。数字图像由被称为像素的小点组成。每个像素可以是一位,表示一个黑色或一个白点,也可以是几个位,表示几种颜色或灰色阴影中的一种。我们假设像素存储在内存中一个称为位图的数组中,因此位图是图像的输入流。像素通常以扫描线排列在位图中,所以第一个位图像素是图像左上角的点,最后一个像素是右下角的点。使用RLE压缩图像是基于这样的观察,即如果我们在图像中随机选择一个像素,它的邻居很有可能会有相同的颜色。因此,压缩器逐行扫描位图,寻找相同颜色的像素运行。如果位图开始,例如,17个白色像素,1个黑色像素,55个白色像素,等等,那么只有数字17、1、55,…需要写入输出流。
2. 图像游程编码代码实现
class Run_Length_Encoding:
def __init__(self):
self.lowbit = '0'
self.highbit = '1'
self.compression_factor_text = 4
self.escape_character = '@'
def run_length_encoding_grayimage(self, input: list):
if len(input) == 0:
return []
outstring = []
import numpy as np
input = np.array(input)
input = input.flatten()
count = 1
for alphabet_index in range(0, len(input)):
if alphabet_index + 1 < len(input) and input[alphabet_index] == input[alphabet_index + 1]:
count += 1
else:
outstring.append(count)
count = 1
return 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.
本文部分参考自《Handbook of Data Compression 》,该内容仅供学习研究之用,如若喜欢请支持购买正品书籍。
转载本文请联系原作者获取授权,同时请注明本文来自池鱼醉故渊博客。