📄 lzwcode.m
字号:
function a=lzwcode(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.
size2=size(img);
num1=size2(2); %每行的像素数
num2=size2(1); %每列的像素数
curcode=[];
if nargin==1
max=255;
end
for f=0:max
dictcode(f+1)={[f]}; %字典条目,注意实际数与下标相差一
end
n=max+1;
c=0;
id=-1;
%要用 到的一些介绍
%int8() uint8() double() str2num()字符转为数字 num2str() func2str() str2func() 函数与字符串的转换
%利用元胞类型放字典条目curcode={''}; diccode()
%开始编码
for s=1:num2
for s2=1:num1
if isempty(curcode)
curcode=img(1,1); %当前识别序列
else
%查找字典条目 看是否有 含有当前像素值加在后面 组成的 一个字典条目 ,如果有就 1、取该条目为下一个当前识别的序列 , 其他什么也不做
%如果没有则 1、把当前像素作为下一个识别序列 2、编码输出当前识别序列在字典中的位置码字 3、向字典里写入一个以当前识别序列为头的拼接上当前像素值的条目
curcode2=curcode;
curcode=[curcode,img(s,s2)]; %组成一个条目
ln=length(dictcode);
%查找字典看是否有该条目
for d=1:ln
if length(curcode)==length(dictcode{d}) & curcode==dictcode{d}
id=d-1;
end
end
if id==-1
%在字典中未找到对应的条目
n=n+1;
dictcode(n)={curcode}; %向字典增加条目
c=c+1;
if length(curcode2)==1
code(c)=curcode2; %编码输出
else
code(c)=lastid; %编码输出
end
curcode=img(s,s2);
else
lastid=id;
end
id=-1;
end
end
end
c=c+1;
code(c)=img(end);
a=code;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -