簡體   English   中英

運行長度編碼圖像

[英]Run Length Encoding an Image

我正在為作業編寫一個運行長度圖像編碼器。 我的代碼適用於二進制和8位圖像,但是當我想編碼4位圖像時,它無法正常工作。 我使用的是Ubuntu 13.10,Python 3.3.4和Pillow。 執行以下代碼應該打印TrueTrue但它打印TrueFalse

為了測試我的代碼,我生成隨機列表來模擬圖像。 它工作,第一個值呈現它,但這個圖像不起作用。 還有4個這樣的圖像,但它們都不起作用。 我錯過了一些觀點嗎?

from PIL import Image
import random

def _encodeImage4bit(imagePixels, width, height):
    encodedImage = bytearray()

    count = 0

    prev = imagePixels[0]
    tempmap = ""

    for pixel in imagePixels:
        if count >= 15:
            encodedImage.append(15)
            encodedImage.append(pixel)
            tempmap += "1"
            tempmap += "0"
            count = 0

        if pixel == prev:
            count += 1
        else:
            if count > 1:
                encodedImage.append(count)
                tempmap += "1"
            encodedImage.append(prev)
            tempmap += "0"
            count = 1
            prev = pixel

    if count > 1:
        encodedImage.append(count)
        tempmap += "1"

    encodedImage.append(prev)
    tempmap += "0"

    encodedImage.extend([0] * _remaining(len(encodedImage)))
    tempmap += "1"*_remaining(len(tempmap))

    encodedImage = _set4bitMap(tempmap, encodedImage)

    return encodedImage

def _set4bitMap(imgMap, encodedImage):
    newImgMap = _divideByRow(imgMap, 8)

    tempImg = [_merge4bitTo8bit(encodedImage[i], encodedImage[i + 1]) for i in range(0, len(encodedImage), 2)]
    tempImg = _divideByRow(list(tempImg), 4)

    return bytearray(_flattenListOfList(_mergeMap(tempImg, newImgMap)))

def _decodeImage4bit(encodedImage, width, height):
    decodedImage = []

    imgMap, encImg = _get4bitMap(encodedImage)

    for index, i in enumerate(imgMap):
        if i == '1' and encImg[index] == 0:
            break

        if i == '1':
            decodedImage.extend([encImg[index + 1]] * encImg[index])
        elif imgMap[index - 1] != '1' or index == 0:
            decodedImage.append(encImg[index])

    return decodedImage

def _get4bitMap(encodedImage):
    imgMap = ""

    newEncodedImage = list(encodedImage)

    I = range(0, len(newEncodedImage), 5)

    for i in I:
        imgMap += '{0:08b}'.format(newEncodedImage[i])

    for i in sorted(list(I), reverse = True):
        del newEncodedImage[i]

    newEncodedImage = _flattenListOfList([_split8bitTo4bit(i) for i in newEncodedImage])

    return (imgMap, newEncodedImage)

def _split8bitTo4bit(eightbit):
    leftmask = 240
    rightmask = 15
    left = (eightbit & leftmask) >> 4
    right = eightbit & rightmask

    return (left, right)

def _merge4bitTo8bit(left, right):
    return (left << 4) | right

_remaining = lambda x, y = 8: 0 if x % y == 0 else y - (x % y)
_mergeMap = lambda z, x:[[int(x[index], 2)] + i for index, i in enumerate(z)]
_flattenListOfList = lambda flat:[item for sublist in flat for item in sublist]
_divideByRow = lambda flat, size: [flat[i:i + size] for i in range(0, len(flat), size)]


if __name__ == "__main__":
    img = [15] * 100
    img.extend([random.randrange(0, 16) for n in range(300)])
    encImg = _encodeImage4bit(img, 20, 20)
    decImg = _decodeImage4bit(encImg, 20, 20)
    print(str(decImg == img))

    imgpath = "../../images/4bit/baboon_4bit.bmp"
    img2 = Image.open(imgpath)
    encImg2 = _encodeImage4bit(list(img2.getdata(0)), img2.size[0], img2.size[1])
    decImg2 = _decodeImage4bit(encImg2, img2.size[0], img2.size[1])
    print(str(decImg2 == list(img2.getdata(0))))

數據壓縮算法完整參考文獻第4版第26頁

同樣,每個字節都有一位用於指示字節是包含灰度值還是計數。 然而,這次,這些額外的比特以8的組的形式累積,並且每個組被寫在它“對應”的8個字節之前(或之后)的輸出流上。

我改變它以支持4位圖像。

示例原始圖像:12,12,12,12,12,12,12,12,12,14,3,7,10,10,10,10,5,5,5,5,5,1,1, 。

第一步:找到重復9,12,14,3,7,4,10,6,5,1,....

第二步:生成映射以識別哪個元素是像素值(0),即重復次數(1)1 0 0 0 0 1 0 1 0。

第三步:將值填充為零,使長度為八的倍數。 用一個填充地圖使長度為八的倍數。

第四步:將地圖划分為八個部分,並將每個部分轉換為整數。 133 ,.

第五步:將每兩個4位耦合到一個八位。 將左側值向左移動4次,並使用正確的數字將其向右移動。 156,227,116,166,81 ,.

第六步:使用值合並地圖。 map中的每個整數現在在值中顯示4個值。 133,156,227,116,166 ,.

解碼與此操作相反。

我發現了我的問題。 如果重復次數達到15,則會為列表添加額外的值。 為了防止這種情況,我將代碼更改為此。

    for pixel in imagePixels:
    if count >= 15:
        encodedImage.append(15)
        encodedImage.append(prev) #changed line
        tempmap += "1"
        tempmap += "0"
        count = 0
        prev = pixel #new line

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM