You are on page 1of 6

Lab Experiment-3 Karhunen-Loeve Transform

Aim: To apply Karhunen-Loeve transformation to given image and also show that the image can be reconstructed with fewer number of eigen values, eigen vectors. Theory: Karhunen-Loeve transformation is a transformation in which the transform kernal is not fixed.In KLT the transform kernal is dependent on the data or we can say the kernal is derived from data . The data is represented in the form of vectors. we define the transformation as. Y=A(Xi-x) Y is the transformed matrix, A is the eigen vector matrix of the covariance matrix of data arranged according to desending order of its eigen vectors, x is the mean value (row sum) vector of the input data The covariance matrix is obtained from the given equation Cx=E { (Xi-x) x (Xi-x)T } We can easly reconstruct our image back by applying inverse operation Xi=ATY+ x KLT closely related to the Principal Component Analysis (PCA) and widely used in data analysis in many fields

Karhunen- Loeve Transform Matlab code


% To implement Karhunen Loeve Transform clc clear X=imread('lena_128.jpg'); X=double(X); [m,n]=size(X); sum_cov_mat=zeros(m,m); for i=1:m r(i)=mean(X(i,:)); end mean_vec=r'; for i=1:n diff_mean_vec(:,i)=X(:,i)-mean_vec; end for i=1:n temp=diff_mean_vec(:,i); temp_cov_mat=temp*temp'; sum_cov_mat=sum_cov_mat+temp_cov_mat; end avg_cov_mat=(1/n).*sum_cov_mat; eig_vec=eig(avg_cov_mat); [eigen_vec,eigen_values]=eig(avg_cov_mat); eigen_vec=fliplr(eigen_vec);%arranging the eigen vectors as columns corresponding to %decreasig order of magnitude of eigen values eigen_vec=eigen_vec'; A=eigen_vec; %-------------------------------------------------------------------------%% Uncomment this part and comment the next part to see perfect reconstruction %%start of perfect reconstruction procedure % for i=1:length(mean_vec) % Y(:,i)=A*(X(:,i)-mean_vec); % end % % for i=1:length(mean_vec) % Reconstructed_image(:,i)=A'*Y(:,i)+mean_vec; % end

% Reconstructed_image=uint8(Reconstructed_image); % imshow(Reconstructed_image) %%End of Perfect reconstruction procedure %-------------------------------------------------------------------------% In the above part,we have acheived Perfect reconstruction since we have % considered all the eigen values %-------------------------------------------------------------------------%Now we are going for approximate reconstruction so that we will get %compression n_values=input ('Enter the no of largest eigen values to be considered: ') for i=1:n_values A_k(i,:)=A(i,:); end for i=1:length(A_k) Y_k(:,i)=A_k*(X(:,i)-mean_vec); end for i=1:length(A_k) Reconstructed_image(:,i)=A_k'*Y_k(:,i)+mean_vec; end %-------------------------------------------------------------------------%Calculating the mean square error %%WORK IN PROGRESS % for i=1:n_values % mod_eig(i)=eig_vec(i); % end for i=1:m for j=1:n error_reconstruction(i,j)=X(i,j)-Reconstructed_image(i,j); square_error(i,j)=(error_reconstruction(i,j))^2; end end disp('The mean square error between original image and reconstructed image is: ') MSE=sum(sum(square_error))/(m*n) disp('PSNR in dB') PSNR=20*log10(max(max(X))/sqrt(MSE)) %%------------------------------------------------------------------------Reconstructed_image=uint8(Reconstructed_image); figure imshow(Reconstructed_image)

title('Reconstructed image') figure imshow(uint8(X)) title('Original image')

Results Test 1 Enter the no of largest eigen values to be considered: 10 n_values =10 The mean square error between original image and reconstructed image is: MSE =346.6516 PSNR in dB PSNR =22.1326

Reconstructed image

Original image

Test 2 Enter the no of largest eigen values to be considered: n_values =30 The mean square error between Original image and reconstructed image is: MSE =65.8073

PSNR in dB PSNR =29.3488


Reconstructed image

Original image

Test3 Enter the no of largest eigen values to be considered: 50 n_values =50 The mean square error between original image and reconstructed image is: MSE =17.1151 PSNR in dB PSNR =35.1978

Reconstructed image

Original image

Discussion We have sucessfully implemented the program for finding KLT of the given image and we have also studied the diffrence in the represantation of the image with different eigen values of the covariance matrix as the number of eigen vectors increases up to certain amount we will get perfect reconstruction of image. This technique can be effectively used for compression of image.

You might also like