when i insert a grayscale image then the algorithm works if i insert a rgb image the output is wrong. is the way I work with the rgb image correct? I don't understand why can someone help me?
library used
from PIL import Image
import numpy as np from cv2 import 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
array of tuyple and charactert
encodedTuple = np.array([])
encodedChar = np.array([])
pointer in the search buffer
sbPointer = 0
while sbSize + sbPointer < tot :
max_match = 0
max_match_go_back = 0
selChar = sbPointer + sbSize
empty sequence
seqY = np.array([])
for i in range(sbPointer,sbPointer + sbSize):
if(flat[i] == encodeCharacters):
seqY = np.append(seqY,i)
check if there is a match in the 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
save corrispondence and tuple in array
encodedTuple = np.append(encodedTuple,(max_match_go_back,max_match))
encodedChar = np.append(encodedChar,flat[selChar + max_match - 1])
sbPointer+= 1 +max_match
**save of encodedTuple, encodedChar , file compresses.txt and imsize of decompression **
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()
**Main **
path = "./im3.jpg"
lz77Compress (path,500,500)