PCA图像压缩的matlab实现

PCA

function pca_test
clear;close all;

img=imread('lena.png');           
figure(1),subplot(121),imshow(img,[]);title('Original Image');
[M N] = size(img);
f = double(img);
bs = 16;   %图像块尺寸
p = 30;    %保留的维数

% PCA图像压缩
g = im2col(f, [bs bs], 'distinct'); %将图像块转换成列矢量表示
g_m = mean(g')'*ones(1,size(g,2));   %计算每个块的灰度均值
g = g - g_m;
[E D]=fun_pca(g);
E_proj = E(:,1:p);   %取最大的p个特征值所对应的特征矢量进行降维
g_proj = g'*E_proj;  %从bs*bs维映射到p维

% 恢复图像
g_rec = g_proj*E_proj';
s = g_rec' + g_m;
s = col2im(s, [bs bs], [M N], 'distinct');
figure(1),subplot(122),imshow(s,[]);title('Recovered Image');

function [E,D] = fun_pca(X)
% do PCA on image patches
% INPUT variables:
% X                  matrix with image patches as columns
% OUTPUT variables:
% E                  特征矢量(第一列对应最大特征值)
% D                  特征值(按下降顺序排列)
% Calculate the eigenvalues and eigenvectors
covarianceMatrix = X*X'/size(X,2);
[E, D] = eig(covarianceMatrix);
% Sort the eigenvalues and recompute matrices
[d_out,order] = sort(diag(D),'descend');
E = E(:,order);
d = diag(D); 
D = diag(d(order));
  • 1
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值