【图像压缩】基于小波结合spiht实现图像压缩附matlab代码

文章介绍了小波变换与SPIHT算法结合用于图像压缩的原理和流程。SPIHT算法通过小波系数的排序和阈值化实现高效编码,结合小波变换能提供高压缩比和良好图像质量。编码和解码过程包括小波变换、编码、量化、压缩比控制以及解码恢复。提供的代码示例展示了MATLAB中SPIHT编码器的部分实现。
摘要由CSDN通过智能技术生成

​✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。

🍎个人主页:Matlab科研工作室

🍊个人信条:格物致知。

更多Matlab仿真内容点击👇

智能优化算法       神经网络预测       雷达通信       无线传感器        电力系统

信号处理              图像处理               路径规划       元胞自动机        无人机 

⛄ 内容介绍

1.原理

小波变换与SPIHT(Set Partitioning in Hierarchical Trees)算法可以结合来实现图像压缩。

首先,小波变换将图像分解为不同频率的子带。这种分解可以提取出图像的局部细节和整体特征。常用的小波变换包括离散小波变换(DWT)和连续小波变换(CWT)。

接下来,SPIHT算法通过对小波系数进行编码和量化来实现压缩。该算法利用小波系数的统计特性和零树结构,逐渐减小编所需的比特数。具体而言,SPIHT算法采用排序和阈值化操作,将小波系数划分为重要系数和次要系数,并按照一定规则进行编码。该算法能够实现高压缩比和保持较好的图像质量。

最后,解码过程中使用相应的解码算法对经过编码和量化的数据进行恢复,从而得到原始图像。

小波结合SPIHT实现图像压缩的优点是可以在较高的压缩比下保持相对较好的图像质量。此外,由于SPIHT利用了零树结构,还能够实现快速的编码和解码过程,适用于实时应用和存储有限的资源环境。

2 算法流程

下面是使用小波变换和SPIHT算法实现图像压缩的基本流程:

  1. 将原始图像进行小波变换:使用离散小波变换(DWT)将原始图像分解为不同频率的子带。常用的小波函数包括Haar、Daubechies、Symlet等。

  2. 对小波系数进行码:对小波变换后得到的子带系数应用SPIHT算法进行编码。SPIHT算法通过排序和阈值化操作,将小波系数划分为重要系数和次要系数,并按照一定规则进行编码。编码过程中,采用漂移编码来减小位数。

  3. 量化:对编码后的小波系数进行量化操作,将系数的值映射到离散的取值范围内。量化可以去除掉一些细微的细节信息,从而减少数据的存储和传输成本。

  4. 压缩比控制:根据需要设定压缩比例,调整编码过程中的阈值或量化步长,以控制压缩后的图像质量和所占空间大小。

  5. 解码恢复:使用相应的解码算法将编码后的数据进行还原,得到经过解码的小波系数。

  6. 反向小波变换:对解码后的小波系数进行反向离散小波变换(IDWT),将小波系数重新组合成压缩后的图像。

最终,得到的图像是经过小波变换和SPIHT编码压缩的结果。需要注意的是,由于量化操作和压缩比控制,压缩后的图像可能会有一定程度的质量损失,因此可以根据需求适当地调整压缩参数以在图像质量和压缩率之间进行平衡。

⛄ 部分代码

function out = func_MySPIHT_Enc(m, max_bits, block_size, level)

% Matlab implementation of SPIHT (without Arithmatic coding stage)

%

% Encoder

%

% input:    m : 小波域输入图像

%           max_bits : 可用的最大比特数

%           block_size : 图像尺寸

%           level : 小波分解层数

%

% output:   out :输出比特流

%-----------   Initialization  -----------------

bitctr = 0;

out = 2*ones(1,max_bits);

n_max = floor(log2(abs(max(max(m)'))));

Bits_Header = 0;

Bits_LSP = 0;

Bits_LIP = 0;

Bits_LIS = 0;

%-----------   output bit stream header   ----------------

% 图像大小, 比特面数目, 小波分解层数 

out(1,[1 2 3]) = [size(m,1) n_max level]; bitctr = bitctr + 24;

index = 4;

Bits_Header = Bits_Header + 24;

%-----------   Initialize LIP, LSP, LIS   ----------------

temp = [];

bandsize = 2.^(log2(size(m, 1)) - level + 1);

temp1 = 1 : bandsize;

for i = 1 : bandsize

    temp = [temp; temp1];

end

LIP(:, 1) = temp(:);

temp = temp';

LIP(:, 2) = temp(:);

LIS(:, 1) = LIP(:, 1);

LIS(:, 2) = LIP(:, 2);

LIS(:, 3) = zeros(length(LIP(:, 1)), 1);

pstart = 1;

pend = bandsize / 2;

for i = 1 : bandsize / 2

    LIS(pstart : pend, :) = [];

    pdel = pend - pstart + 1;

    pstart = pstart + bandsize - pdel;

    pend = pend + bandsize - pdel;

end

LSP = [];

n = n_max;

%-----------   coding   ----------------

while(bitctr < max_bits)

        

    % Sorting Pass

    LIPtemp = LIP; temp = 0;

    for i = 1:size(LIPtemp,1)

        temp = temp+1;

        if (bitctr + 1) >= max_bits

            if (bitctr < max_bits)

                out(length(out))=[];

            end

            return

        end

        if abs(m(LIPtemp(i,1),LIPtemp(i,2))) >= 2^n % 1: positive; 0: negative

            out(index) = 1; bitctr = bitctr + 1;

            index = index +1; Bits_LIP = Bits_LIP + 1;

            sgn = m(LIPtemp(i,1),LIPtemp(i,2))>=0;

            out(index) = sgn; bitctr = bitctr + 1;

            index = index +1; Bits_LIP = Bits_LIP + 1;

            LSP = [LSP; LIPtemp(i,:)];

            LIP(temp,:) = []; temp = temp - 1;

        else

            out(index) = 0; bitctr = bitctr + 1;

            index = index +1;

            Bits_LIP = Bits_LIP + 1;

        end

    end

    

    LIStemp = LIS; temp = 0; i = 1;

    while ( i <= size(LIStemp,1))

        temp = temp + 1;

        if LIStemp(i,3) == 0

            if bitctr >= max_bits

                return

            end

            max_d = func_MyDescendant(LIStemp(i,1),LIStemp(i,2),LIStemp(i,3),m);

            if max_d >= 2^n

                out(index) = 1; bitctr = bitctr + 1;

                index = index +1; Bits_LIS = Bits_LIS + 1;

                x = LIStemp(i,1); y = LIStemp(i,2);

                

                if (bitctr + 1) >= max_bits

                    if (bitctr < max_bits)

                        out(length(out))=[];

                    end

                    return

                end

                if abs(m(2*x-1,2*y-1)) >= 2^n

                    LSP = [LSP; 2*x-1 2*y-1];

                    out(index) = 1; bitctr = bitctr + 1;

                    index = index +1; Bits_LIS = Bits_LIS + 1;

                    sgn = m(2*x-1,2*y-1)>=0;

                    out(index) = sgn; bitctr = bitctr + 1;

                    index = index +1; Bits_LIS = Bits_LIS + 1;

                else

                    out(index) = 0; bitctr = bitctr + 1;

                    index = index +1; Bits_LIS = Bits_LIS + 1;

                    LIP = [LIP; 2*x-1 2*y-1];

                end

                

                if (bitctr + 1) >= max_bits

                    if (bitctr < max_bits)

                        out(length(out))=[];

                    end

                    return

                end

                if abs(m(2*x-1,2*y)) >= 2^n

                    LSP = [LSP; 2*x-1 2*y];

                    out(index) = 1; bitctr = bitctr + 1;

                    index = index +1; Bits_LIS = Bits_LIS + 1;

                    sgn = m(2*x-1,2*y)>=0;

                    out(index) = sgn; bitctr = bitctr + 1;

                    index = index +1; Bits_LIS = Bits_LIS + 1;

                else

                    out(index) = 0; bitctr = bitctr + 1;

                    index = index +1; Bits_LIS = Bits_LIS + 1;

                    LIP = [LIP; 2*x-1 2*y];

                end

                

                if (bitctr + 1) >= max_bits

                    if (bitctr < max_bits)

                        out(length(out))=[];

                    end

                    return

                end

                if abs(m(2*x,2*y-1)) >= 2^n

                    LSP = [LSP; 2*x 2*y-1];

                    out(index) = 1; bitctr = bitctr + 1;

                    index = index +1; Bits_LIS = Bits_LIS + 1;

                    sgn = m(2*x,2*y-1)>=0;

                    out(index) = sgn; bitctr = bitctr + 1;

                    index = index +1; Bits_LIS = Bits_LIS + 1;

                else

                    out(index) = 0; bitctr = bitctr + 1;

                    index = index +1; Bits_LIS = Bits_LIS + 1;

                    LIP = [LIP; 2*x 2*y-1];

                end

                

                if (bitctr + 1) >= max_bits

                    if (bitctr < max_bits)

                        out(length(out))=[];

                    end

                    return

                end

                if abs(m(2*x,2*y)) >= 2^n

                    LSP = [LSP; 2*x 2*y];

                    out(index) = 1; bitctr = bitctr + 1;

                    index = index +1; Bits_LIS = Bits_LIS + 1;

                    sgn = m(2*x,2*y)>=0;

                    out(index) = sgn; bitctr = bitctr + 1;

                    index = index +1; Bits_LIS = Bits_LIS + 1;

                else

                    out(index) = 0; bitctr = bitctr + 1;

                    index = index +1; Bits_LIS = Bits_LIS + 1;

                    LIP = [LIP; 2*x 2*y];

                end

                

                if ((2*(2*x)-1) < size(m) & (2*(2*y)-1) < size(m))

                    LIS = [LIS; LIStemp(i,1) LIStemp(i,2) 1];

                    LIStemp = [LIStemp; LIStemp(i,1) LIStemp(i,2) 1];

                end

                LIS(temp,:) = []; temp = temp-1;

                

            else

                out(index) = 0; bitctr = bitctr + 1;

                index = index +1; Bits_LIS = Bits_LIS + 1;

            end

        else

            if bitctr >= max_bits

                return

            end

            max_d = func_MyDescendant(LIStemp(i,1),LIStemp(i,2),LIStemp(i,3),m);

            if max_d >= 2^n

                out(index) = 1; bitctr = bitctr + 1;

                index = index +1;

                x = LIStemp(i,1); y = LIStemp(i,2);

                LIS = [LIS; 2*x-1 2*y-1 0; 2*x-1 2*y 0; 2*x 2*y-1 0; 2*x 2*y 0];

                LIStemp = [LIStemp; 2*x-1 2*y-1 0; 2*x-1 2*y 0; 2*x 2*y-1 0; 2*x 2*y 0];

                LIS(temp,:) = []; temp = temp - 1;

            else

                out(index) = 0; bitctr = bitctr + 1;

                index = index +1; Bits_LIS = Bits_LIS + 1;

            end

        end

        i = i+1;

    end

    

    % Refinement Pass

    temp = 1;

    value = floor(abs(2^(n_max-n)*m(LSP(temp,1),LSP(temp,2))));

    while (value >= 2^(n_max+1) & (temp <= size(LSP,1)))

        if bitctr >= max_bits

            return

        end

        s = bitget(value,n_max+1);

        out(index) = s; bitctr = bitctr + 1;

        index = index +1; Bits_LSP = Bits_LSP + 1;

        temp = temp + 1;

        if temp <= size(LSP,1)

            value = floor(abs(2^(n_max-n)*m(LSP(temp,1),LSP(temp,2))));

        end

    end

    

    n = n - 1;

end

⛄ 运行结果

⛄ 参考文献

[1] 逯仁虎.基于整数小波变换的无人机侦查图像的压缩[D].哈尔滨理工大学,2010.DOI:10.7666/d.y1838416.

[2] 胡晖.基于小波变换的医学图像压缩研究[D].武汉理工大学,2010.DOI:CNKI:CDMD:2.2010.166098.

[3] 甘宸伊姚远杨彦伟刘小兵高荣.基于小波变换的图像压缩中小波基的评价与选取[J].四川兵工学报, 2016, 037(012):105-107,149.

⛳️ 代码获取关注我

❤️部分理论引用网络文献,若有侵权联系博主删除

❤️ 关注我领取海量matlab电子书和数学建模资料

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Matlab科研辅导帮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值