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

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

✅作者简介:热爱科研的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电子书和数学建模资料


相关文章
|
3天前
|
数据安全/隐私保护
地震波功率谱密度函数、功率谱密度曲线,反应谱转功率谱,matlab代码
地震波格式转换、时程转换、峰值调整、规范反应谱、计算反应谱、计算持时、生成人工波、时频域转换、数据滤波、基线校正、Arias截波、傅里叶变换、耐震时程曲线、脉冲波合成与提取、三联反应谱、地震动参数、延性反应谱、地震波缩尺、功率谱密度
|
3天前
|
数据安全/隐私保护
耐震时程曲线,matlab代码,自定义反应谱与地震波,优化源代码,地震波耐震时程曲线
地震波格式转换、时程转换、峰值调整、规范反应谱、计算反应谱、计算持时、生成人工波、时频域转换、数据滤波、基线校正、Arias截波、傅里叶变换、耐震时程曲线、脉冲波合成与提取、三联反应谱、地震动参数、延性反应谱、地震波缩尺、功率谱密度
基于混合整数规划的微网储能电池容量规划(matlab代码)
基于混合整数规划的微网储能电池容量规划(matlab代码)
|
3天前
|
算法 调度
面向配电网韧性提升的移动储能预布局与动态调度策略(matlab代码)
面向配电网韧性提升的移动储能预布局与动态调度策略(matlab代码)
|
3天前
|
算法 调度
含多微网租赁共享储能的配电网博弈优化调度(含matlab代码)
含多微网租赁共享储能的配电网博弈优化调度(含matlab代码)
|
3天前
|
运维 算法
基于改进遗传算法的配电网故障定位(matlab代码)
基于改进遗传算法的配电网故障定位(matlab代码)
|
3天前
|
Serverless
基于Logistic函数的负荷需求响应(matlab代码)
基于Logistic函数的负荷需求响应(matlab代码)
|
3天前
|
供应链 算法
基于分布式优化的多产消者非合作博弈能量共享(Matlab代码)
基于分布式优化的多产消者非合作博弈能量共享(Matlab代码)
|
3天前
|
算法 调度
基于多目标粒子群算法冷热电联供综合能源系统运行优化(matlab代码)
基于多目标粒子群算法冷热电联供综合能源系统运行优化(matlab代码)
|
3天前
|
算法 调度 SoC
电动汽车充放电V2G模型(Matlab代码)
电动汽车充放电V2G模型(Matlab代码)

热门文章

最新文章