天天看點

【圖像壓縮】基于奇異值分解svd進行圖像壓縮matlab代碼

 1 簡介

根據奇異值分解的基本原理及其特點,給出了運用奇異值分解進行圖像壓縮的方法.通過簡單的例子說明了該方法進行圖像壓縮的基本過程,給出了壓縮流程.并通過MAT-LAB程式設計對實際圖像進行處理,表明了該方法的有效性.

【圖像壓縮】基于奇異值分解svd進行圖像壓縮matlab代碼

編輯

【圖像壓縮】基于奇異值分解svd進行圖像壓縮matlab代碼

編輯

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) < 0)
            NewImage_r(i,j) = 0;
        end
        if(NewImage_g(i,j) < 0)
            NewImage_g(i,j) = 0;
        end
        if(NewImage_b(i,j) < 0)
            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代碼

【圖像壓縮】基于奇異值分解svd進行圖像壓縮matlab代碼

編輯

4 參考文獻

部落客簡介:擅長智能優化算法、神經網絡預測、信号處理、元胞自動機、圖像處理、路徑規劃、無人機等多種領域的Matlab仿真,相關matlab代碼問題可私信交流。

繼續閱讀