DPCM编码解码的原理

 python代码如下

import cv2 as cv
import numpy as np  
import matplotlib.pyplot as plt
def DPCM(yBuffer, dBuffer, re, w, h, bitnum):
    # yBuffer raw buffer
    # dBuffer x buffer
    # rebuildBuffer reconstruct buffer
    x = 2 ** (8 - bitnum)
    y = 2 ** (9 - bitnum)
    flow_upper_bound = 2 ** bitnum - 1
    for i in range(0, h):
        prediction = 128
        pred_error = yBuffer[i * w] - prediction
        tmp = (pred_error + 128) // x
        dBuffer[i * w] = tmp
        inv_pred_error = dBuffer[i * w] * x - 128
        re[i * w] = inv_pred_error + prediction
        for j in range(1, w):
            prediction = re[i * w + j - 1]
            predErr = yBuffer[i * w + j] - prediction
            tmp = (predErr + 255) // y
            dBuffer[i * w + j] = tmp
            invPredErr = dBuffer[i * w + j] * y - 255
            re[i * w + j] = invPredErr + prediction
Img = cv.imread(r'C:\Users\ylhpc\Desktop\homework\lena.jpg')
Img = cv.cvtColor(Img, cv.COLOR_BGR2GRAY)
h = Img.shape[0]
w = Img.shape[1]
"""----------------------------------------1 bit 重构----------------------------------------"""
dBuffer1 = np.zeros((h * w), dtype=np.uint8)
rebuildBuffer1 = np.zeros((h * w), dtype=np.uint8)
yBuffer1 = Img.reshape(h*w)

DPCM(yBuffer1, dBuffer1, rebuildBuffer1, w, h, 1)
dBuffer1 = dBuffer1.reshape(h, w)
rebuildBuffer1 = rebuildBuffer1.reshape(h, w)

"""----------------------------------------2 bit 重构----------------------------------------"""
dBuffer2 = np.zeros((h * w), dtype=np.uint8)
rebuildBuffer2 = np.zeros((h * w), dtype=np.uint8)
yBuffer2 = Img.reshape(h*w)

DPCM(yBuffer2, dBuffer2, rebuildBuffer2, w, h, 2)
dBuffer2 = dBuffer2.reshape(h, w)
rebuildBuffer2 = rebuildBuffer2.reshape(h, w)

"""----------------------------------------4 bit 重构----------------------------------------"""
dBuffer4 = np.zeros((h * w), dtype=np.uint8)
rebuildBuffer4 = np.zeros((h * w), dtype=np.uint8)
yBuffer4 = Img.reshape(h*w)

DPCM(yBuffer4, dBuffer4, rebuildBuffer4, w, h, 4)
dBuffer4 = dBuffer4.reshape(h, w)
rebuildBuffer4 = rebuildBuffer4.reshape(h, w)

"""----------------------------------------8 bit 重构----------------------------------------"""
dBuffer8 = np.zeros((h * w), dtype=np.uint8)
rebuildBuffer8 = np.zeros((h * w), dtype=np.uint8)
yBuffer8 = Img.reshape(h*w)

DPCM(yBuffer8, dBuffer8, rebuildBuffer8, w, h, 8)
dBuffer8 = dBuffer8.reshape(h, w)
rebuildBuffer8 = rebuildBuffer8.reshape(h, w)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值