1、一、 实验目的实验六 图像压缩 (同预 习)1、 理解有损压缩和无损压缩的概念;2、 理解图像压缩的主要原则和目的;3、 利用 MATLAB 程序 进 行图像压缩。二、 实验内容1、Huf fman 编码使用 mat2huff, huff2mat 实现 Huffman 编解码 , 并应用 imratio 计算编码后信号的压缩率。2、编写无损预测编解码 ( lossless predictive coding) 程序, 使 p313 Figure 8.7 (c) 图具有更低的熵 ( 5.4436。 显示预测误差(p rediction error)图 。 用解码 程序解 码,并验证解码是否正确(
2、 compare3、参考p319- 323,实现 16 16分块离散余弦( DCT)编码,用z igzag方式 保留每一 块的前10个和前 21个系数,再进行进行DCT解码。编写程序并分别显示解码 后的两个图。实验程序:实验1:i=imread(Fig0804(a)(Tracy).tif);i1=mat2huff(i);cr1=imratio(i,i1);%cr1,压缩率i2=huff2mat(i1);rmse1=compare(i,i2);figure,imshow(i);figure,imshow(i2);实验2:子程序1:function y = mat2lpc2(x, f)%本程序改自
3、课本上那个程序,可实现更高压缩率!error(nargchk(1, 2, nargin); % Check input argumentsif nargin 2 % Set default filter if omittedf = 1; endx = double(x); % Ensure double for computationsm, n = size(x); % Get dimensions of input matrixp = zeros(m, n); % Init linear prediction to 0xs = x; zc = zeros(1,n); % Prepare fo
4、r input shift and padfor j = 1:length(f) % For each filter coefficient .xs = zc; xs(1:end - 1,:); % Shift and zero pad xga改加一行p = p + f(j) * xs; % Form partial prediction sumsendy = x - round(p); % Compute the prediction error子程序2:function x = lpc2mat2(y, f)% 本程序改自课本上那个程序,可以解压error(nargchk(1, 2, nar
5、gin); % Check input argumentsif nargin 2 % Set default filter if omittedf = 1; endy=y;f = f(end:-1:1); % Reverse the filter coefficientsm, n = size(y); % Get dimensions of output matrixorder = length(f); % Get order of linear predictorf = repmat(f, m, 1); % Duplicate filter for vectorizingx = zeros(
6、m, n + order); % Pad for 1st order column decodes% Decode the output one column at a time. Compute a prediction based% on the order previous elements and add it to the prediction% error. The result is appended to the output matrix being built.for j = 1:njj = j + order;x(:, jj) = y(:, j) + round(sum(
7、f(:, order:-1:1) .* .x(:, (jj - 1):-1:(jj - order), 2);endx = x(:, order + 1:end); % Remove left paddingx=x;总程序:f=imread(Fig0807(c)(Aligned).tif);e=mat2lpc2(f);figure,imshow(mat2gray(e);h=entropy(e);c=lpc2mat2(e);figure,imshow(c,);rmse=compare(f,c);实验三:i=imread(Fig0804(a)(Tracy).tif);i=im2double(i);
8、mask1=1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
9、0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ;mask2=1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0
10、 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0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