当我插入灰度图像时,如果我插入 rgb 图像,则该算法有效,输出错误。我使用 rgb 图像的方式是否正确?我不明白为什么有人可以帮助我?
使用的图书馆
from PIL import Image
从 cv2 导入 numpy 作为 np 导入 cv2
def lz77Compress (image,sw,lab):
img = cv2.imread(image)
print("Initial size", img)
flat = np.array(img).flatten()
#cv2.imshow("input", img)
row = img.shape[0]
col = img.shape[1]
tot = row * col
slidingWindows = sw
lookAhead = lab
元组和字符数组
encodedTuple = np.array([])
encodedChar = np.array([])
搜索缓冲区中的指针
sbPointer = 0
while sbSize + sbPointer < tot :
max_match = 0
max_match_go_back = 0
selChar = sbPointer + sbSize
空序列
seqY = np.array([])
for i in range(sbPointer,sbPointer + sbSize):
if(flat[i] == encodeCharacters):
seqY = np.append(seqY,i)
检查 lookAheadBuffer 中是否有匹配项
if(seqY.size == 0 ):
encodedTuple = np.append(encodedTuple,(0,0))
encodedChar = np.append (encodedChar,encodeCharacters)
else:
for j in seqY:
#lunghezza della corrispodenza
matchLenght= 0
returnBack = selChar -i
it = 0
while selChar + it < tot :
if flat[it + j] == flat[selChar + it]:
matchLenght +=1
it +=1
# se non trova corrispondenze
else:
break
if matchLenght>max_match:
max_match = matchLenght
returnBack = max_match_go_back
在数组中保存对应关系和元组
encodedTuple = np.append(encodedTuple,(max_match_go_back,max_match))
encodedChar = np.append(encodedChar,flat[selChar + max_match - 1])
sbPointer+= 1 +max_match
**保存encodedTuple、encodedChar、文件compresses.txt和解压的imsize **
print("Prova", encodedTuple, encodedChar)
print("ArrayBin",encodedTuple.tolist(), encodedChar.tolist())
np.save("encodedTuple", encodedTuple)
np.save("encodedChar", encodedChar)
a = encodedTuple.tolist()
b = encodedChar.tolist()
c= (a,b)
print("File compresso in : Copressed.txt")
output = open("Compressed.txt","w+")
output.write(str(c))
imgSize = open('imgSize.txt', "w")
imgSize.write(str(row) + '\n') # write row dimension
imgSize.write(str(col) + '\n') # write col dimension
cv2.waitKey(0)
cv2.destroyAllWindows()
**主要的 **
path = "./im3.jpg"
lz77Compress(路径,500,500)