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)
In most cases, RGB image has 3 color channels, and gray image has 1 color channel.
imgis a NumPy array that represents a gray image:img.shapeis going to be(rows, cols), andimg.sizeisrows*cols.imgis a NumPy array that represents a RGB (or BGR) image:img.shapeis going to be(rows, cols, ch), andimg.sizeisrows*cols*ch.In case of RGB,
ch= 3.I downloaded your code from github.
I saw you already corrected the code to support RGB images.
I can't reproduce the error in line 35 of image.py
Did you fix it?
The issues I could find:
You are not closing the files:
Close the files at the end of
lz77Compress:You better also close
imsizeafter readingrow,col,chinlz77Decompressor.When using
cv2.imshow("output", decodeArray), you need the type ofdecodeArrayto beuint8.You may use the following code to show the output:
In case you want to support both gray and RGB, you can check if
len(img.shape)is2or3, and add some logic at the Decompressor.Note:
I didn't review the entire code, so there might be problems that I missed.