简体   繁体   English

运行长度编码图像

[英]Run Length Encoding an Image

I am writing an Run Length Image Encoder for an assignment. 我正在为作业编写一个运行长度图像编码器。 My code works very well for binary and 8 bit images but when I want to encode an 4 bit image it doesn't work correctly. 我的代码适用于二进制和8位图像,但是当我想编码4位图像时,它无法正常工作。 I am using Ubuntu 13.10, Python 3.3.4 and Pillow. 我使用的是Ubuntu 13.10,Python 3.3.4和Pillow。 Executing following code should print True and True but it prints True and False . 执行以下代码应该打印TrueTrue但它打印TrueFalse

To test my code I generate random list to simulate an image. 为了测试我的代码,我生成随机列表来模拟图像。 It works, first value presents it, but this image is not working. 它工作,第一个值呈现它,但这个图像不起作用。 There is 4 more image like this but none of them works. 还有4个这样的图像,但它们都不起作用。 Am I missing some point? 我错过了一些观点吗?

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))))

Algorithm from Data Compression The Complete Reference 4th Edition Page 26 数据压缩算法完整参考文献第4版第26页

Again, one bit is devoted to each byte to indicate whether the byte contains a grayscale value or a count. 同样,每个字节都有一位用于指示字节是包含灰度值还是计数。 This time, however, these extra bits are accumulated in groups of 8, and each group is written on the output stream preceding (or following) the 8 bytes it “corresponds to.” 然而,这次,这些额外的比特以8的组的形式累积,并且每个组被写在它“对应”的8个字节之前(或之后)的输出流上。

I changed it to support 4 bit images. 我改变它以支持4位图像。

Example Original image: 12, 12, 12, 12, 12, 12, 12, 12, 12, 14, 3, 7, 10,10, 10, 10, 5, 5, 5, 5, 5, 5, 1, . 示例原始图像:12,12,12,12,12,12,12,12,12,14,3,7,10,10,10,10,5,5,5,5,5,1,1, 。 . .

First step: Find repetitions 9, 12, 14, 3, 7, 4, 10, 6, 5, 1, . 第一步:找到重复9,12,14,3,7,4,10,6,5,1,.... . .

Second step: Generate map to identify which element is pixel value(0), which is repetition number(1) 1 0 0 0 0 1 0 1 0 . 第二步:生成映射以识别哪个元素是像素值(0),即重复次数(1)1 0 0 0 0 1 0 1 0。 . .

Third step: Fill values with zero to make length a multiple of eight. 第三步:将值填充为零,使长度为八的倍数。 Fill map with one to make length of multiple of eight. 用一个填充地图使长度为八的倍数。

Fourth step: Divide map to pieces of eight and convert every piece to integer. 第四步:将地图划分为八个部分,并将每个部分转换为整数。 133, . 133 ,. . .

Fifth step: Couple every two 4 bit to make one eight bit. 第五步:将每两个4位耦合到一个八位。 Shift left value to left 4 times and OR it with right number. 将左侧值向左移动4次,并使用正确的数字将其向右移动。 156, 227, 116, 166, 81, . 156,227,116,166,81 ,. . .

Sixth step: Merge map with values. 第六步:使用值合并地图。 Every integer number in map now presents 4 values in values. map中的每个整数现在在值中显示4个值。 133, 156, 227, 116, 166, . 133,156,227,116,166 ,. . .

Decoding is reverse of this operation. 解码与此操作相反。

I found my problem. 我发现了我的问题。 If repetations reachs to 15, it adds extra value to list. 如果重复次数达到15,则会为列表添加额外的值。 To prevent this I changed my code to this. 为了防止这种情况,我将代码更改为此。

    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