1

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

4

1 回答 1

0

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

  • 假设img是一个表示灰度图像的 NumPy 数组:
    img.shape将是(rows, cols),并且img.sizerows* 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()
    

你最好也关闭imsize后阅读row,,,colchlz77Decompressor

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

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

如果您想同时支持灰色和 RGB,您可以检查 if len(img.shape)is 2or 3,并在 Decompressor 处添加一些逻辑。

注意:
我没有查看整个代码,因此可能存在我遗漏的问题。

于 2021-04-03T22:09:01.343 回答