📄 lzwcode2.m
字号:
function a=lzwcode2(img,max)
%lzwcode(img) used to make code of lzw .the parameter img is a matrix of n* m!
% and it return the lzw code matrix.
%进行LZW编码,输入2维图像数据,返回一个LZW编码一维的序列
size2=size(img);
num1=size2(2); %每行的像素数
num2=size2(1); %每列的像素数
curcode=[];
if nargin==1
max=255;
end
for f=0:max
dictcode(f+1,1)=f; %字典条目,注意实际数与下标相差一
dictcode(f+1,2)=-1;
end
n=max+1;
c=0; %编码输出下标
id=-1;
tt=0;
%开始编码
lastid=img(1,1);
for s=1:num2
for s2=1:num1
%查找字典条目 看是否有 含有当前像素值加在后面 组成的 一个字典条目 ,如果有就 1、取该条目为下一个当前识别的序列 , 其他什么也不做
%如果没有则 1、把当前像素作为下一个识别序列 2、编码输出当前识别序列在字典中的位置码字 3、向字典里写入一个以当前识别序列为头的拼接上当前像素值的条目
curcode2=curcode;
curcode=[curcode img(s,s2)]; %组成一个条目
if length(curcode)>1
%查找字典看是否有该条目
for d=(max+1):n
bb=find(dictcode(d,:)==-1)-1;
if length(curcode)==bb
for vv=1:bb
if dictcode(d,vv)~= curcode(vv)
tt=-1;
break;
end
end
if tt==-1
tt=0;
else
id=d-1;
break;
end
end
end
else
id=curcode;
end
if id==-1
%在字典中未找到对应的条目
n=n+1;
for mm=1:length(curcode)
dictcode(n,mm)=curcode(mm); %向字典增加条目
end
dictcode(n,mm+1)=-1; %加上最后一个是 -1 表示这是结束点
c=c+1;
if length(curcode2)==1
code(c)=curcode2; %编码输出
else
code(c)=lastid; %编码输出
end
curcode=img(s,s2);
else
lastid=id;
id=-1;
end
end
end
c=c+1;
code(c)=img(end);
a=code;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -