首页 > 解决方案 > lzz 77 压缩图像 rgb

问题描述

如果我插入大小为 (205,205) 的图像,算法会非常快速地压缩它。但是如果我插入一个更大的图像,该算法需要很长时间来压缩它。我的意图是优化代码,从而加快压缩阶段你有什么建议吗?

图书馆

from PIL import Image
import numpy as np 
from cv2 import cv2

**function of compression**

def lz77Compress (image,sw,lab):
    img = cv2.imread(image)
    flat = np.array(img).flatten()
    row = img.shape[0]
    col = img.shape[1]
    ch = img.shape[2]
    tot = row * col * ch
    slidingWindows = sw
    lookAhead = lab 

元组和字符数组

 encodedTuple = np.array([], dtype=np.uint16) 
    encodedChar = np.array([], dtype=np.uint8)
    **# Lunghezza del Search Buffer**

sbSize = slidingWindows - lookAhead
    for it in range(sbSize):
        encodedTuple = np.append(encodedTuple, (0, 0))
        encodedChar = np.append(encodedChar, flat[it])
    **# pointer in the  Search Buffer**
 sbPointer = 0
while sbPointer < tot :
        max_match = 0
        max_match_go_back = 0        
        selChar = sbPointer + sbSize
     # corrispondenza del carattere in Sb da lookAd
        encodeCharacters = flat[selChar] 
    **#sequenza vuota[]**
       seqY = np.array([],dtype=np.int16)
        for i in range(sbPointer,sbPointer + sbSize):
            if(flat[i] == encodeCharacters):
                seqY = np.append(seqY,i)
        
    **check of corrispondence and insert in encodedtuple and encodedChar**
        if(seqY.size == 0 ):
            encodedTuple = np.append(encodedTuple,(0,0))
            encodedChar = np.append (encodedChar,encodeCharacters)

else:
            for j in seqY:
            **size of corrisponddence**
                matchLenght= 0
                returnBack= selChar - j
                it = 0 
                while selChar + it < tot :
                    if flat[it + j] == flat[selChar + it]:
                        matchLenght +=1
                        it +=1
                  **# if there is not corrispondence*
                    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
    **save encoedTupe and encodedChar and image size for the compression**
    np.save("encodedTuple", encodedTuple) 
    np.save("encodedChar", encodedChar)
    print("File compresso in : Copressed.txt")
    output = open("Compressed.txt","w+")
    output.write(str(c))
    

标签: image-processingcompressionrgblz77

解决方案


推荐阅读