这是lzw解码功能,它对输入图像进行解码。我不明白它是怎么做到的, 如果有人能为我解释它,我将不胜感激
def lzw_decode(encoded_img, decoded_img):
with open(encoded_img, "rb") as f:
code_arr, shape = pickle.load(f)
dictionary = dict()
dict_size = 256
for i in range(256):
dictionary[i] = chr(i)
img = ""
s = chr(code_arr.pop(0))
img += s
for k in code_arr:
if k in dictionary.keys():
entry = dictionary[k]
elif k == dict_size:
entry = s + s[0]
img += entry
dictionary[dict_size] = s + entry[0]
dict_size += 1
s = entry
img = [ord(x) for x in img]
img = np.array(img, dtype=np.uint16).reshape(shape)
cv2.imwrite(decoded_img, img)