所有分类
  • 所有分类
  • 其它

【图像压缩】基于奇异值分解svd进行图像压缩matlab代码

 1 简介

根据奇异值分解的基本原理及其特点,给出了运用奇异值分解进行图像压缩的方法.通过简单的例子说明了该方法进行图像压缩的基本过程,给出了压缩流程.并通过MAT-LAB编程对实际图像进行处理,表明了该方法的有效性.

【图像压缩】基于奇异值分解svd进行图像压缩matlab代码_奇异值分解

【图像压缩】基于奇异值分解svd进行图像压缩matlab代码_参考文献_02编辑

【图像压缩】基于奇异值分解svd进行图像压缩matlab代码_奇异值分解_03

【图像压缩】基于奇异值分解svd进行图像压缩matlab代码_参考文献_04编辑

2 完整代码

% Read the image into A as a matrix of uint8
clc
clear all
close all
[X,map] = imread('witchhead.jpg');
Im = X;
% Convert image from uint8 to doubles for svd
X = im2double(X);
% Seperate
[U_r,S_r,V_r] = svd(X(:,:,1));
[U_g,S_g,V_g] = svd(X(:,:,2));
[U_b,S_b,V_b] = svd(X(:,:,3));
%=============================
% Test Scripts
%=============================
%confirmation checking for red
out_red = U_r*S_r*V_r';
red = X(:,:,1);
%-----------------------------
% S_r has size 640x1138, thus 640 diagonal values
size(S_r)
% Find the largest k singular values
k = 30;
redk = zeros(k,1);
greenk = zeros(k,1);
bluek = zeros(k,1);
% Discovered that the diagonal of the sum matrix is in order
for i = 1:k
redk(i) = S_r(i,i);
greenk(i) = S_g(i,i);
bluek(i) = S_b(i,i);
end
%-----------------------------
% Storage Analysis
initialStorage = 640*1138;
currentStorage = (640+1138)*k+k;
%-----------------------------
% Error Analysis
sume = 0;
for i = 1:640
sume = sume + S_r(i,i) + S_g(i,i) + S_b(i,i);
end
error = sum(redk+greenk+bluek) / sume;
%-----------------------------
NewImage_r = zeros(640,1138);
NewImage_g = zeros(640,1138);
NewImage_b = zeros(640,1138);
for i = 1:k
NewImage_r = NewImage_r + redk(i)* U_r(:,i)*V_r(:,i)';
NewImage_g = NewImage_g + greenk(i) * U_g(:,i)*V_g(:,i)';
NewImage_b = NewImage_b + bluek(i) * U_b(:,i)*V_b(:,i)';
end
%-----------------------------
% Normalize the matrices to fit the rgb format
for i = 1:640
for j = 1:1138
if(NewImage_r(i,j) NewImage_r(i,j) = 0;
end
if(NewImage_g(i,j) NewImage_g(i,j) = 0;
end
if(NewImage_b(i,j) NewImage_b(i,j) = 0;
end
end
end
%-----------------------------
rgbImage = cat(3, NewImage_r, NewImage_g, NewImage_b);
% image(rgbImage);
% image(X);
difference = rgbImasge-X
figure
subplot(121)
imshow(X,[]);title('原图')
subplot(122)
imshow(NewImage_b,[]);title('压缩后的图')
%-----------------------------

3 仿真结果

【图像压缩】基于奇异值分解svd进行图像压缩matlab代码_图像压缩_05

【图像压缩】基于奇异值分解svd进行图像压缩matlab代码_奇异值分解_06编辑

4 参考文献

[1]胡乡峰, 卫金茂. 基于奇异值分解(SVD)的图像压缩[J]. 东北师大学报:自然科学版, 2006, 38(3):4.

博主简介:擅长智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、路径规划、无人机等多种领域的Matlab仿真,相关matlab代码问题可私信交流。

部分理论引用网络文献,若有侵权联系博主删除。

社交账号快速登录