LZW字典编码

1977年,两位以色列教授Lempel和Ziv提出了查找冗余字符和用较短的符号标记替代冗余字符的概念。1985年,由Welch加以充实而形成LZW,简称“LZW”技术。

  • 举例说明

  1. 首先将图像从左到右,从上到下扫描,将所有像素排成一列,叫做被处理像素
  2. 研究符号出现的规律--拼接
  • 构造当前识别序列,这个序列是动态产生的,其初始值为“空”
  • 以当前识别序列和当前被处理像素进行拼接

  • 如果所形成的符号串在字典中已经有了,则将拼接符号串置为当前识别序列,将下一个像素置为当前被处理像素,重复第(2)步

  • 如果拼接所形成的符号串在字典中没有
  • (输出当前识别序列在字典中的位置,这位就是该 符号序列的编码
  • 生成一个新的字典条目
  • 将当前被处理像素变成当前识别序列,将下一个像素置为当前被处理像素,重复第(2)步。 

 最后的编码:0,0,255,255,256,258,260,259,257,255

LZW的解码:根据编码查找字典 

编码值

象素

0

0

0

0

255

255

255

255

256

0-0

258

255-255

260

0-0-255

259

255-0

257

0-255

255

255

 

代码实现

以灰度图的LZW压缩编码为例,用Python实现

import cv2 as cv
import numpy as np
import argparse

parser = argparse.ArgumentParser(description='')
parser.add_argument("--mode", choices=["encode", "decode"], type=str, default=None)
parser.add_argument("--encodeFile", type=str, default="pout.png")
parser.add_argument("--decodeFile", type=str, default="lzw.npy")
args = parser.parse_args()


def encode_init():
    m_dict = {}
    for i in range(0, 256):
        m_dict[i] = [i]
    return m_dict


def LZW_encode(input_l, m_dict):
    previous_char = []
    output = []
    for s in input_l:
        # !!!!!! 用copy
        p_c = previous_char.copy()
        p_c.append(s)

        if p_c in m_dict.values():
            previous_char = p_c
        else:
            output.append(list(m_dict.keys())[list(m_dict.values()).index(previous_char)])
            m_dict[len(m_dict)] = p_c
            previous_char = [s]
    output.append(list(m_dict.keys())[list(m_dict.values()).index(previous_char)])

    return output


def LWZ_Decode(input_l, m_dict):
    output = []
    print(m_dict)
    for i in range(len(input_l)):
        value = m_dict[input_l[i]]
        for ss in value:
            output.append(ss)
    return output


def encode():
    origin = cv.imread(args.encodeFile, 0)
    origin_re = origin.ravel()
    dictionary = encode_init()
    print(dictionary)
    print(origin.shape[0])
    lzw_data = [origin.shape[0], origin.shape[1]]
    lzw_data = lzw_data + LZW_encode(origin_re, dictionary)
    print(len(lzw_data))

    np.save('LWZ', np.array(lzw_data))
    np.save('dictionary.npy', dictionary)


def decode():
    lzw_data = np.load(args.decodeFile).tolist()
    h = lzw_data[0]
    w = lzw_data[1]
    lwz_data = lzw_data[2:len(lzw_data)]
    dictionary = np.load('dictionary.npy', allow_pickle=True).item()
    result = LWZ_Decode(lwz_data, dictionary)
    print(len(result))
    k = 0
    res = np.zeros((h, w))
    for i in range(h):
        for j in range(w):
            if k >= len(result):
                res[i][j] = 0
            else:
                res[i][j] = result[k]
            k = k + 1
    print(res)

    cv.imwrite('result.png', res)


if __name__ == '__main__':
    if args.mode == "encode":
        encode()
    if args.mode == "decode":
        decode()

 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ace2NoU

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值