gpt4 book ai didi

用于图像压缩的 C# 预测编码

转载 作者:行者123 更新时间:2023-11-30 12:16:19 25 4
gpt4 key购买 nike

我一直在研究图像上的霍夫曼压缩以在保持无损图像的同时减小尺寸,但我还读到您可以使用预测编码通过减少熵来进一步压缩图像数据。

据我了解,在无损 JPEG 标准中,每个像素都被预测为光栅顺序中已经遇到的相邻 4 个像素的加权平均值(上方三个,左侧一个)。例如,尝试根据 a 左侧和上方的先前像素 x 来预测像素 a 的值:

x x x
x a

然后计算和编码残差(预测值和实际值之间的差异)。

但我不明白的是,如果平均 4 个相邻像素不是 4 的倍数,你会得到分数吗?那部分应该被忽略吗?如果是这样,8 位图像(保存在 byte[] 中)的正确编码是否类似于:

public static void Encode(byte[] buffer, int width, int height)
{
var tempBuff = new byte[buffer.Length];

for (int i = 0; i < buffer.Length; i++)
{
tempBuff[i] = buffer[i];
}

for (int i = 1; i < height; i++)
{
for (int j = 1; j < width - 1; j++)
{
int offsetUp = ((i - 1) * width) + (j - 1);
int offset = (i * width) + (j - 1);

int a = tempBuff[offsetUp];
int b = tempBuff[offsetUp + 1];
int c = tempBuff[offsetUp + 2];
int d = tempBuff[offset];
int pixel = tempBuff[offset + 1];

var ave = (a + b + c + d) / 4;
var val = (byte)(ave - pixel);
buffer[offset + 1] = val;
}
}
}

public static void Decode(byte[] buffer, int width, int height)
{
for (int i = 1; i < height; i++)
{
for (int j = 1; j < width - 1; j++)
{
int offsetUp = ((i - 1) * width) + (j - 1);
int offset = (i * width) + (j - 1);

int a = buffer[offsetUp];
int b = buffer[offsetUp + 1];
int c = buffer[offsetUp + 2];
int d = buffer[offset];
int pixel = buffer[offset + 1];

var ave = (a + b + c + d) / 4;
var val = (byte)(ave - pixel);
buffer[offset + 1] = val;
}
}
}

我不明白这真的会如何减少熵?这将如何帮助进一步压缩我的图像,同时仍然是无损的?

谢谢指教

编辑:

所以在玩过预测编码图像之后,我注意到直方图数据显示了很多 +-1 的各种像素。在某些情况下,这会大大降低熵。这是一个屏幕截图:

enter image description here

最佳答案

是的,只是截断。没关系,因为您存储了差异。它减少了熵,因为您只存储小值,其中很多是 -1、0 或 1。顺便说一下,您的代码片段中有几个差一错误。

关于用于图像压缩的 C# 预测编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5628558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com