1.哈夫曼编码
已知某信源发出的8个消息,其信源概率分布是不均匀的,分别为{0.1,0.18,0.4,0.05,0.06,0.1,0.07,0.04},请对信源进行哈夫曼编码,并求出三个参数:平均码长、熵及编码效率。分析编码结果和书上不一致的原因。
2.算术编码和解码
假设信源符号为{a,b,c,d},假设这些符号出现的概率分别为{0.2,0.2,0.4,0.2} ,写出对信源符号“bdadcadc”进行算术编码和解码的过程。
3.车牌号码识别
主要步骤:图像预处理、车牌区域定位、车牌字符分割、车牌字符识别。
4.图像拼接
编写程序,将两幅相邻图像(图1和图2)拼接成一幅全景图像(图3)。
图1:
huffman.m文:
function CODE = huffman(p)
%HUFFMAN Builds a variable-length Huffman code for a symbol source.
% CODE = HUFFMAN(P) returns a Huffman code as binary strings in
% cell array CODE for input symbol probability vector P. Each word
% in CODE corresponds to a symbol whose probability is at the
% corresponding index of P.
%
% Based on huffman5 by Sean Danaher, University of Northumbria,
% Newcastle UK. Available at the MATLAB Central File Exchange:
% Category General DSP in Signal Processing and Communications.
% Copyright 2002-2004 R. C. Gonzalez, R. E. Woods, & S. L. Eddins
% Digital Image Processing Using MATLAB, Prentice-Hall, 2004
% $Revision: 1.5 $ $Date: 2003/10/26 18:37:16 $
% Check the input arguments for reasonableness.
error(nargchk(1, 1, nargin));
if (ndims(p) ~= 2) | (min(size(p)) > 1) | ~isreal(p) | ~isnumeric(p)
error('P must be a real numeric vector.');
end
% Global variable surviving all recursions of function 'makecode'
global CODE
CODE = cell(length(p), 1); % Init the global cell array
if length(p) > 1 % When more than one symbol ...
p = p / sum(p); % Normalize the input probabilities
s = reduce(p); % Do Huffman source symbol reductions
makecode(s, []); % Recursively generate the code
else
CODE = {'1'}; % Else, trivial one symbol case!
end;
%-------------------------------------------------------------------%
function s = reduce(p);
% Create a Huffman source reduction tree in a MATLAB cell structure
% by performing source symbol reductions until there are only two
% reduced symbols remaining
s = cell(length(p), 1);
% Generate a starting tree with symbol nodes 1, 2, 3, ... to
% reference the symbol probabilities.
for i = 1:length(p)
s{i} = i;
end
while numel(s) > 2
[p, i] = sort(p); % Sort the symbol probabilities
p(2) = p(1) + p(2); % Merge the 2 lowest probabilities
p(1) = []; % and prune the lowest one
s = s(i); % Reorder tree for new probabilities
s{2} = {s{1}, s{2}}; % and merge & prune its nodes
s(1) = []; % to match the probabilities
end
%-------------------------------------------------------------------%
function makecode(sc, codeword)
% Scan the nodes of a Huffman source reduction tree recursively to
% generate the indicated variable length code words.
% Global variable surviving all recursive calls
global CODE
if isa(sc, 'cell') % For cell array nodes,
makecode(sc{1}, [codeword 0]); % add a 0 if the 1st element
makecode(sc{2}, [codeword 1]); % or a 1 if the 2nd
else % For leaf (numeric) nodes,
CODE{sc} = char('0' + codeword); % create a char code string
end
调用函数
% function huffmanBIANMA
p=[0.1 0.18 0.4 0.05 0.06 0.1 0.07 0.04];
%p=[0.12 0.16 0.3 0.05 0.06 0.2 0.07 0.04];
code=huffman(p)
算数编码函数:
%算数编码函数
function acode = suanshubianma(symbol,ps,inseq)
high_range=[];
for k=1:length(ps)
high_range=[high_range sum(ps(1:k))];
end
low_range=[0 high_range(1:length(ps)-1)];
sbidx=zeros(size(inseq));
for i=1:length(inseq)
sbidx(i)=find(symbol==inseq(i));
end
low=0;
high=1;
for i=1:length(inseq)
range=high-low;
high=low+range*high_range(sbidx(i));
low=low+range*low_range(sbidx(i));
end
acode=low;
end
算术解码函数
%算术解码函数
function symbos=suanshujiema(symbol,ps,codeword,symlen)
high_range=[];
for k=1:length(ps)
high_range=[high_range sum(ps(1:k))];
end
low_range=[0 high_range(1:length(ps)-1)];
psmin=min(ps);
symbos=[];
for i=1:symlen
idx=max(find(low_range<=codeword));
codeword=codeword-low_range(idx);
if abs(codeword-ps(idx))<0.01*psmin
idx=idx+1;
codeword=0;
end
symbos=[symbos symbol(idx)];
codeword=codeword/ps(idx);
if abs(codeword)<0.01*psmin
i=symlen+1;
end
end
主函数
%主函数
clc
clear
symbol=['abcd'];
ps=[0.2 0.2 0.4 0.2];
inseq='bdadc';
format long e
codeword=suanshubianma(symbol,ps,inseq);
outseq=suanshujiema(symbol,ps,codeword,length(inseq));
由于代码较长,这里只展示图片
有需要,请自取:
链接:https://pan.baidu.com/s/17hQBKmhN_mFwL6P4yH5x5w
提取码:sxqy
由于代码较长,这里只展示图片
有需要,请自取:
链接:https://pan.baidu.com/s/1XMlyH1fMehi4z1QZB0642g
提取码:sxqy