当我插入灰度图像时,算法工作。如果我插入rgb图像,则输出错误。 我使用rgb图像的方式正确吗?我不明白为什么有人能帮我?
使用的库
from PIL import Image
将numpy作为np导入 从cv2导入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
tuyple和charactert数组
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、file 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"
LZ77压缩(路径,500500)
在大多数情况下,RGB图像有3个颜色通道,而灰度图像有1个颜色通道
img
是一个表示灰度图像的NumPy数组:img.shape
将是(rows, cols)
,而img.size
将是rows
*cols
李>img
是表示RGB(或BGR)图像的NumPy数组:img.shape
将是(rows, cols, ch)
,而img.size
是rows
*cols
*ch
。在RGB的情况下,
ch
=3李>我从github下载了您的代码。
我看到您已经更正了代码以支持RGB图像
我无法重现image.py第35行中的错误
你修好了吗
我能找到的问题是:
您没有关闭文件:
关闭
lz77Compress
末尾的文件:在
row
、col
、ch
中读取lz77Decompressor
之后,最好也关闭imsize
使用
cv2.imshow("output", decodeArray)
时,需要将decodeArray
的类型设置为uint8
。您可以使用以下代码显示输出:
如果您想同时支持gray和RGB,可以检查
len(img.shape)
是2
还是3
,并在解压缩程序中添加一些逻辑注:
我没有检查整个代码,所以可能有一些问题我遗漏了
相关问题 更多 >
编程相关推荐