📄 examp_compression.m
字号:
%EXAMP_COMPRESSION Image compression example%% This examples shows how to do image compression using a% two-dimensional Wilson transform.%% The image is transformed using an orthonormal DWILT2 transform% followed by N-term approximation and quantization. Then the image is% reconstructed and compared with the original.%% This example demonstrates both lossless and lossy compression. An image% compression is lossless if the truncation error is below the quantization% error, as the truncation error is not visible in that case.%% FIGURE 1 The cameraman.%% This figure to the left shows lossless compression of the% cameraman. The figure to the right shows a lossy compression.%% This program is free software: you can redistribute it and/or modify% it under the terms of the GNU General Public License as published by% the Free Software Foundation, either version 3 of the License, or% (at your option) any later version.% % This program is distributed in the hope that it will be useful,% but WITHOUT ANY WARRANTY; without even the implied warranty of% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the% GNU General Public License for more details.% % You should have received a copy of the GNU General Public License% along with this program. If not, see <http://www.gnu.org/licenses/>.disp('Type "help examp_compression" to see a description of how this example works.');% -------- defining constants -------------% Image has dimension 256x256.L=256;% The original signal uses 8 bits:origbits=8;% Number of channels.M=32;% Bitratenbits=8;% Number of coefficients to keep for lossy compression.Nkeep=2000;% Constant to use for mulaw encoding / decoding.mu=2;% -------- initial setup ------------------% Load the cameraman picturef=cameraman;f = double(f);% Create window for Wilson orhonormal basis.g=wilorth(M,L);% Get coefficients.c=dwilt2(f,g,M);disp('----- Lossless compression -----');% Find max-value of imagemax_c=max(abs(f(:)));% Determine a thresholdthr_val=.5*max_c/(2^origbits);% Do threshholding[cN,N_thresh]=thresh('hard',c,thr_val);% Do quantization.cQ=uquant('unsigned',cN,nbits);% Reconstruct.f_lossless=idwilt2(cQ,g);% Display itfigure(1);subplot(1,2,1);colormap(gray);imagesc(f_lossless);axis('image');disp('');disp('Compression ratio:');nbits*N_thresh/(origbits*L*L)disp('')disp('relative l^2 error:'); norm(f_lossless(:)-double(f(:)))/norm(double(f(:)))disp('----- Lossy compression ------');% Do N-term approximation.cN=largestn(c,Nkeep);% Do mulaw encoding[c_mulaw, sigweight] = mulawencode(cN,mu);% Do quantization.c_mulaw_Q=uquant('unsigned',c_mulaw,nbits);% Do mulaw decondingc_Q=mulawdecode(c_mulaw_Q,mu,sigweight);% Reconstruct.f_lossy=idwilt2(c_Q,g);% Display itsubplot(1,2,2);colormap(gray);imagesc(f_lossy);axis('image');disp('');disp('Compression ratio:');nbits*Nkeep/(origbits*L*L)disp('')disp('relative l^2 error:'); norm(f_lossy(:)-double(f(:)))/norm(double(f(:)))% Scale it nicely%if ~isoctave% set(gcf,'Units','Normalized');% set(gcf,'Position',[0,0,1,.5]);%end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -