% ========================================================================= % Math 336 - Image Processing - Laboratory 4 % % Image Restoration (Part 2): SVD and PCA. % Compute the SVD of Barbara.jpg. Plot the sigularity spectrum. Display the % reconstructions of Barbara using 5%, 10%, 15% and 20% of the singular % values. % % Marty Kandes % Computational Science Research Center / Department of Physics % San Diego State University % Fall 2008 % ------------------------------------------------------------------------- clear all; close all; clc; % Clear Matlab workspace. F = imread('barbara.jpg'); % Read in theimage. F = im2double(F); % Convert image to double. F = mat2gray(F); % Normalize grayscale image. [M,N] = size(F); % Get dimensions of image. [U,S,V] = svd(F,0); % Compute the SVD of the image. SIGMA = diag(S); % Compute the sigularity spectrum. figure; % Open new figure. semilogy(SIGMA); % Plot signularity spectrum. R = 51; % Set rank of PCA image. PCA = zeros(M,N); % Preallocate PCA image. for I = 1:R % Reconstruct the image via P = U(:,I); % PCA using the SVD components. Q = V(:,I); % PCA = PCA + SIGMA(I).*P*Q'; % end; % figure; % Open new figure. imshow(PCA); % Display PCA recontructed image. imwrite(PCA,'barbara20.jpg','jpg'); % Write image to file.