📄 encode_spiht.m
字号:
function [mylineout,Linelength,Tout,ALR]=encode_SPIHT(A,Ncode)
%mylineout为输出码流
%Linelength为每个码流位数
%Tout为每次权值
%spiht encode
%A is the input matrix
%*****************************************************************************
%define some global variable
%global A %A is the processing matrix (A是输入的小波系数矩阵)
global LIP %LIP list of insignificant pixels
global LIS %LIS list of insignificant set
global LSP %LSP list of insignificant pixels
global LSPflag %the breakpoint of the LSP refinement (作为LSP细化时的处理分界点)
global LIStype %LIS have two type:D and L (作为D型与L型的区分bit)
global ALR %the line_number of A
global Alength %A to vector's length
global X %Mapping result (扫描时根据它的顺序)
global Index %保存索引
global output %output string:you can easily change it into what type you want
%*******************************************************************************
%A=A1;
LSPflag=0; %because the LSP initial to []
if nargin==0
A=[63 -34 49 10 7 13 -12 7;
-31 23 14 -13 3 4 6 -1;
15 14 3 -12 5 -7 3 9;
-9 -7 -14 8 4 -2 3 2;
-5 9 -1 47 4 6 -2 2;
3 0 -3 2 3 -2 0 4;
2 -3 6 -4 3 6 3 6;
5 11 5 6 0 3 -4 4];
Ncode=200;
end
A=A(1:end); %将A整形成1维向量(第二列接在第一列的后面)
Alength=length(A); %A矢量长度
ALR=sqrt(Alength); %原来A矩阵的边长
X=mapping(ALR); %找出扫描顺序存在X矩阵中
%以下将扫描顺序索引置于NN中
X=X(1:end); %对应于A作整形
[XX,Index]=sort(X); %XX是1:end的向量而Index则保存了索引:A(Index(i))则找到第i个被扫描的A阵的元素
%initial the LIP,LSP and LIS
%初始化LIP,LSP及LIS
LIP=[1 2 3 4]; %LIP初始化为H及其三个子女
LIS=[2 3 4]; %LIS一开始是三个D型根节点,即作为H的子女O
LIStype=[1 1 1]; %D为类型1,0为类型L
LSP=[]; %置空
T=Weight(A); %T是权值
Tout=T; %将权值输出
mylineout=''; %输出码流
Linelength=[]; %输出码长
%开始处理小波系数
Ntotal=0;
i=1;
while(Ntotal < Ncode)
%for i=1:ratio
if T < 2
display('权值太小!!!')
return
end
output='';
LSPflag=length(LSP); %确定细分LSP的分界点
%******扫描LIP******************************************
LIPScan(T,A);
%******扫描LIS*******************************************
LISScan(T,A);
%******细分LSP*********************************
LSPRefine(T,A);
%*****************************************
mylineout=strvcat(mylineout,output);
Linelength=[Linelength,length(output)];
T=T/2; %权值减半
%display(LSPflag);
%display(output);
Ntotal=Ntotal+length(output);
i=i+1;
end
%-----------截断码流处理------------
%dalta=Ntotal-Ncode;
%output=output(1:dalta);
%mylineout=mylineout(1:i-1);
%mylineout=strvcat(mylineout,output);
%Linelength=[Linelength,length(output)];
%-------------------------------------------------------------------------
%计算权值的函数
function T=Weight(Ain)
T=1;
while T <= max(Ain)/2
T=2*T;
end
%------------------------------------------------------------------------
%------------------------------------------------------------------------
% LIP扫描
function LIPScan( T ,A)
global LIP
global LIS
global LSP
global Index
global output
i=1;
while (i<= length( LIP )) & ( i > 0 ) %判断LIP扫描到最后一个没有
Aindex=Index( LIP(i) ); %LIP中对应的坐标映射到A向量的坐标-->值
%判断Sn(i,j)=1还是=0
if abs( A(Aindex) ) >= T %Sn(i,j)=1时
output=[output,'1']; %输出1
if A(Aindex) > 0 %判断符号位
output=[output,'0'];
else
output=[output,'1'];
end
LSP=[LSP,LIP(i)]; %把LIP(i)移到LSP的末尾
LIP=[LIP(1:i-1),LIP(i+1:end)]; %把LIP(i)从LIP中删除
i=i-1; %把计数(索引)减一以对应于更新的LIP向量
else
output=[output,'0']; %Sn(i,j)=0
end
i=i+1; %计数(索引)加1
end
%------------------------------------------------------------
%--------------------------------------------------------------
% LIS扫描:高志斌
function LISScan(T,A)
global LIP
global LIStype
global LIS
global LSP
global Index
global output
i=1;
while( i<= length(LIS) ) & ( i > 0 ) %LIS的扫描结束否?
Ki=LIS(i);
Oindex=MyChilds(Ki);
for count=1:4
result=Dcheck( Oindex(count),A,T,0); %把所有儿子的h后代先找出来
if result
break
end
end
if LIStype(i) %若集合D型
if result==1 || max( abs( [A(Index(Oindex(1))),A(Index(Oindex(2))),A(Index(Oindex(3))),A(Index(Oindex(4)))] ) ) >= T %若Sn(D(i,j))=1
output=[output,'1'];
for count=1:4 %对O(i,j)逐个扫描
if abs( A(Index(Oindex(count))) ) >= T
output=[output,'1'];
LSP=[LSP, Oindex(count)]; %若Sn=1,把(i,j)放到LSP末尾
if A(Index(Oindex(count)))>0 %判断符号
output=[output,'0'];
else
output=[output,'1'];
end
else
output=[output,'0'];
LIP=[LIP,Oindex(count)]; %若Sn=0,把(i,j)放到LIP末尾
end %这里对应于if abs(A(indexx,indexy))>=T
end %对应于for count=1:4 %对O(i,j)逐个扫描
LIS=[LIS(1:i-1),LIS(i+1:end)]; %把D从LIS中删除
LIStype=[LIStype(1:i-1),LIStype(i+1:end)]; %同时移走标志--标志与LIS各位置一一对应
i=i-1;
%判断L(i,j)是否存在
if (DDCheck(Ki,A)) %该节点还有孙节点(及二级后代),置为类型L
LIS=[LIS,Ki];
LIStype=[LIStype,0];
end
else
output=[output,'0'];
end
else %类型L的处理 相应于if (LIStype(i)==1) %类型D
if result==1 %若Sn(D(i,j))=1
output=[output,'1'];
LIS=[LIS(1:i-1),LIS(i+1:end)]; %把L从LIS中提出
LIStype=[LIStype(1:i-1),LIStype(i+1:end)]; %同时移走标志
i=i-1;
LIS=[LIS,Oindex(1),Oindex(2),Oindex(3),Oindex(4)];
LIStype=[LIStype,1,1,1,1]; %移入LIS,并标志为D
else
output=[output,'0'];
end
end
i=i+1;
end %对应于while结束
%------------------------------------------------------------
%LSP细分
function LSPRefine(T,A)
%define some global variable
global LSP %LSP list of insignificant pixels
global LSPflag %the breakpoint of the LSP refinement
global Index
global output %output string:you can easily change it into what type you want
%**************************************
for i=1:LSPflag
Aindex=Index(LSP(i));
P=dec2bin(abs(A(Aindex)));
P=P(end:-1:1);
output=[output,P(double(log2(T))+1)];
end
%-----------------------------------------------------------
function O=MyChilds(i)
% 找儿子-----------------------
O= [4*i-3 : 4*i];
function result=Dcheck(i,A,T,result)
%result初始化为0,其值表示A中有否大于T
global Index
t=1;
while ChildCheck(t*i,A)
for k= 4*t*i-(4*t-1) : 4*t*i
if abs(A(Index(k))) >= T
result=1;
return
end
end
t=t*4;
end
function X = mapping(n)
%this function find the scan order of the matrix
%"Z" type
if n == 2
X = [1 2; 3 4];
else
B = mapping(n/2);
X = [B B+(n/2)^2; B+(n/2)^2*2 B+(n/2)^2*3];
end
function Dcheck=DDCheck(i,A)
%是否有儿子——————————
Dcheck=0;
if (i~=1) & ( 16*i<= length(A) )
Dcheck=1;
end
function Ccheck=ChildCheck(i,A)
Ccheck=0;
if (i~=1) & (4*i<length(A))
Ccheck=1;
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -