SPIHT小波编解码和97小波编解码图像解压缩

up目录

一、理论基础

1.1 SPIHT小波

1.2 9/7小波

二、核心程序

三、测试结果


一、理论基础

1.1 SPIHT小波

     SPIHT算法基于分层树的集合划分算法(SetPartitioninginHierarchicalTrees,SPIHT)改进了内嵌零树编码算法(EZW)。在对图像进行小波变换后,它更有效地利用了不同尺度子带重要系数间的相似性。它呈现出良好的特性:不依赖傅立叶变换而在空间域中构造小波;较高的PSNR(PeakSignalNoiseRatio,峰值信噪比)保证了良好的重现图像质量。

       SPIHT编码作为一种优越的嵌入式小波编码,在各种图像压缩中应用广泛。它能够支持图像渐进传输、压缩效率高,比特流按照重要性排序并且能够随时结束编码,允许达到一个目标比特率或失真。该算法充分挖掘了小波图像不同子带之间的相似性,是公认的一种高效图像压缩编码,因此研究SPIHT编码并更好地利用它是非常必要的。提出了只加密SPIHT压缩编码前两层数据的灰度图像部分加密方法;基于SPIHT提出了一种彩色图像的部分加密算法,基于SPIHT提出了一种灰度图像部分加密算法。与编码过程相结合的加密方案能够在编码的同时完成加密,提高了效率,并且能够满足格式兼容性,适应网络实时传输。该算法的实质类似于将码流中的某些关键数据用随机比特替换,容易受到已知明文攻击和选择明文攻击。因此在设计此类加密算法时要考虑如何提高这方面的安全性。

       图像在经过小波变换和量化后,还未实现真正意义上的数据量的压缩。因此,需要通过 SPHIT 编码算法对小波系数进行编码压缩。对图像的编码方式有很多,这些编码包括:游程编码、huffman编码以及算数编码等等。

        对系数矩阵进行了二维提升小波变换后,小波系数有了空间上的特殊结构,这种特殊的结构称为零树结构。SPIHT 算法编码(set partitioning in hierarchical trees)即多级树集合分裂编码。

       SPIHT 算法的主要思想是利用给定的阈值与小波系数集合中的每个系数进行比较,如果值大于阈值,则输出一个二进制的数字作为该系数重要性的标志;这个二进制的标志位就是对图像系数进行编码后所生成的码流。当把所有的小波变换的系数值遍历完以后将阈值减半,再进行扫描小波系数集合,与更新的阈值对比,然后输出相应的图像压缩码流,直到阈值变为 1。

       该算法基于嵌入式零树小波变换编码思想,以 EZW 算法的零树结构为基础,将某树节点及其所有后继节点规划为同一集合,创建 LSP、LIP 和 LIS 三个表项,并采用适当的集合分割排序策略,通过初始化、排序过程、细化过程和量化步长更新这四个子过程完成嵌入式编码。

      SPIHT 编码是一种渐进的对于不重要的系数集合不断的进行分裂,同时空间方向树也在不断变化的过程,这种过程可以使误差逐渐减小。

 

     SPIHT算法对图像信息采用如下的编码步骤。

  首先,定义三个队列:不显著性系数队列LIP,显著性系数队列LSP和不显著性集合队列LIS。

  设,O(i,j)表示节点(i,j)的直接节点的集合;D(i,j)表示节点(i,j)的子节点集合;L(i,j)表示子节点中排除直接节点后的集合。

  在队列中,每个元素由一个坐标唯一识别,它在LIP和LSP中代表孤立系数(无子节点的根节点),在LIS中代表第一类元素的D(i,j)或者第二类元素的L(i,j)。

  对某个阈值T进行显著性测试。将大于T的元素移入LSP,并在LIP队列中移除该元素。对LIS也进行同样的测试,将显著的元素移入LSP,其他的再进行树的分裂。

       SPIHT算法是一种嵌入式编码,将待编码的比特流按重要性的不同进行排序根据目标码率或失真度大小要求随时结束编码。它具有良好的渐进传输特性,利用集合划分以及有序的位平面传输,优先传输重要系数,优先传输重要系数的重要比特位,其主要步骤如下:

1)初始化:对图像进行小波分解,用固定比特位数表示变换后的系数Ci,j,输出n=|log2 (max(i,j)){|ci,i|}到信道;

2)排序过程:输出满足条件2n≤|ci,j|<2n+1的系数Ci,j的个数Z,以及对应系数的Z对坐标和Z个符号位;

3)细化过程:对于所有满足|ci,j|≥2n+1系数,输出第n位的值;

4)迭代:如需迭代,则n-1转到2)。

1.2 9/7小波

       标准在去相关模块中建议采用9/7整数小波变换,即是对原始图像先进行行变换,然后对变换后的数据再进行列变换。需要三级这样的二维小波变换,将图像分为10个子带。假设原始图像为零级小波变换后的LL0,那么后一级就是对前一级变换之后得到的LLi(i=0,1,2)再进行整数提升小波变换所得的结果。由于需要三级整数提升小波变换,每一次变换需要变换的部分的边长都为前一级的1/2,所以要求待变换的图像宽高是8的倍数。如果不满足要求,则需要对图像进行扩边处理。

CCSDS标准中采用的9/7整数小波变换就是这样一种提升小波变换,并且它只需一次提升。其算法公式可通过改变式得到:

逆变换公式如下:

二、核心程序

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


%-----------   Initialization  -----------------
bitctr = 0;
out = 2*ones(1,max_bits - 14);
n_max = floor(log2(abs(max(max(m)'))));
Bits_Header = 0;
Bits_LSP = 0;
Bits_LIP = 0;
Bits_LIS = 0;

 
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+1)*m(LSP(temp,1),LSP(temp,2))));
    while (value >= 2^(n_max+2) & (temp <= size(LSP,1)))
        if bitctr >= max_bits
            return
        end
        s = bitget(value,n_max+2);
        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+1)*m(LSP(temp,1),LSP(temp,2))));
        end
    end
    
    n = n - 1;
end
up98

三、测试结果

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

fpga和matlab

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

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

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

打赏作者

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

抵扣说明:

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

余额充值