我正在为图像实现lz77压缩。但是如果我插入了一个RGB图像,输出是错误的,你们能帮我吗?

2024-10-04 05:26:16 发布

您现在位置:Python中文网/ 问答频道 /正文

当我插入灰度图像时,算法工作。如果我插入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)


Tags: imgmatchnpitcv2maxwriteprint
1条回答
网友
1楼 · 发布于 2024-10-04 05:26:16

在大多数情况下,RGB图像有3个颜色通道,而灰度图像有1个颜色通道

  • 假设img是一个表示灰度图像的NumPy数组:
    img.shape将是(rows, cols),而img.size将是rows*cols
  • 假设img是表示RGB(或BGR)图像的NumPy数组:
    img.shape将是(rows, cols, ch),而img.sizerows*cols*ch
    在RGB的情况下,ch=3

我从github下载了您的代码。
我看到您已经更正了代码以支持RGB图像

我无法重现image.py第35行中的错误
你修好了吗

我能找到的问题是:

  • 您没有关闭文件:
    关闭lz77Compress末尾的文件:

     # We must close the files.
     output.close()
     imgSize.close()
    

rowcolch中读取lz77Decompressor之后,最好也关闭imsize

  • 使用cv2.imshow("output", decodeArray)时,需要将decodeArray的类型设置为uint8
    您可以使用以下代码显示输出:

     cv2.imshow("output", decodeArray.astype(np.uint8)) # Show as uint8
    

如果您想同时支持gray和RGB,可以检查len(img.shape)2还是3,并在解压缩程序中添加一些逻辑

注:
我没有检查整个代码,所以可能有一些问题我遗漏了

相关问题 更多 >