📄 demo_pcacomp.m
字号:
% DEMO_PCACOMP Demo on image compression using PCA%% Description:% The PCA model is trained to model rectangular non-overlapping % patches of a specified input image. The compression phase % involves representation of the image patches in the lower % dimesional space trained by the PCA. The decompression consists % of reconstruction of the image patches from their low dimensional % representation.%% Note: This demo requires Images toolbox.%% See also % PCA, LINPROJ, PCAREC.%% About: Statistical Pattern Recognition Toolbox% (C) 1999-2003, Written by Vojtech Franc and Vaclav Hlavac% <a href="http://www.cvut.cz">Czech Technical University Prague</a>% <a href="http://www.feld.cvut.cz">Faculty of Electrical Engineering</a>% <a href="http://cmp.felk.cvut.cz">Center for Machine Perception</a>% Modifications:% 06-jun-2004, VFhelp demo_pcacomp;% Setting%==========================================img_file = 'lena.jpg';patch_size = 16;fprintf('Input dimension: %d\n', patch_size^2);new_dim=input('Output dimesion:');if isempty(new_dim), new_dim = 20; end% load imagefprintf('Image: %s\n', img_file);img = im2double( imread( img_file ));if isrgb(img), img = rgb2grey(img); endfprintf('Image size: [%d x %d]\n', size(img));% image -> patches stored as column vectorsX = im2col( img, patch_size*[1 1], 'distinct');% train PCAfprintf('Training PCA...');pca_model = pca(X,new_dim );fprintf('done\n');% reconstructionfprintf('Reconstructing image patches...');Y = pcarec( X, pca_model );fprintf('done\n');% plot reconstruction error w.r.t. to dimensionfigure; hold on; title('Reconstruction error'); xlabel('output dimesion'); ylabel('MsErr');plot(pca_model.MsErr);plot(pca_model.new_dim, pca_model.mse, 'or');% patches -> imagecompressed_img = col2im(Y,patch_size*[1 1],size(img),'distinct');% display original and compresed imagefigure; imshow(img);hold on; title('Original image');% display original and compresed imagefigure; imshow(img);hold on; title('Splitting to patches');[h,w]=size(img);for x=1:16:w, plot([x x],[1 h],'b'); endfor y=1:16:w, plot([1 w],[y y],'b'); endfigure; imshow(compressed_img);hold on; title('Compressed image');% display statistics[dim,num_data]=size(X);new_dim=pca_model.new_dim;cr = ((new_dim*num_data)+(new_dim*dim))/(dim*num_data);fprintf('new_dim=%d\n',new_dim);fprintf('Compression ratio (compressed_size/original_size)= %f%%\n',cr*100);fprintf('MsErr=%f\n', pca_model.mse);%EOF
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -