In: Computer Science
I want to create an image compression program with matlab use PCA. I have the code listed below. But this code is fail, the image is colorless, but still gray. Can you help me to fix my code.
clc
clear all
picture = im2double(imread('picture1.jpg'));
Red = picture(:,:,1);
premean = mean(Red(:));
premax = max(Red(:));
premin = min(Red(:));
x = size(Red,1);
y = size(Red,2);
Z = ones(x,y)*premean;
A = (Red - Z)*(1/premax - premin);
B = cov(A);
[veceig,eig,C] = pcacov(B);
NewRed = A*veceig(:,1:50);
ReturnRed = NewRed*veceig(:,1:50);
Return2Red = ((premax - premin)*ReturnRed)+ Z;
Green = picture(:,:,2);
premean_2 = mean(Green(:));
premax_2 = max(Green(:));
premin_2 = min(Green(:));
x2 = size(Green,1);
y2 = size(Green,2);
Z2 = ones(x2,y2)*premean_2;
A2 = (Green - Z2)*(1/premax_2 - premin_2);
D = cov(A2);
[veceig2,eig,E] = pcacov(D);
NewGreen = A2*veceig2(:,1:50);
ReturnGreen = NewGreen*veceig2(:,1:50);
Return2Green = ((premax_2 - Premin_2)*ReturnGreen)+ Z2;
Blue = picture(:,:,3);
premean_3 = mean(Blue(:));
premax_3 = max(Blue(:));
premin_3 = min(Blue(:));
x3 = size(Blue,1);
y3 = size(Blue,2);
Z3 = ones(x3,y3)*premean_3;
A3 = (Blue - Z3)*(1/premax_3 - premin_3);
F = cov(A3);
[veceig3,eig,G] = pcacov(F);
NewBlue = A3*veceig3(:,1:50);
ReturnBlue = NewBlue*veceig3(:,1:50);
Return2Blue = ((premax_3 - Premin_3)*Return2Blue)+ Z3;
figure,plot(cumsum(C)),cumsum(E),cumsum(G);
figure,imshow(A:A2:A2);
title('Original Image Normalization');
figure,imshow(ReturnRed:ReturnBlue:ReturnGreen);
title('Original Result of Decompression Normalization');
figure,imshow(Red:Green:Blue);
imshow(Red:Green:Blue);
Here I introduce a Matlab code for image compression using the very easy algorithm of PCA, in my code, I haven't used predefined functions for PCA but wrote the algorithm myself for a better understanding of PCA
clear; | |
clc; | |
% Start of PCA code, | |
Data = imread('/home/ammar/Desktop/PCA/Ammar3.png'); | |
Data_gray = rgb2gray(Data); | |
Data_grayD = im2double(Data_gray); | |
figure, | |
set(gcf,'numbertitle','off','name','Grayscale Image'), | |
imshow(Data_grayD) | |
Data_mean = mean(Data_grayD); | |
[a b] = size(Data_gray); | |
Data_meanNew = repmat(Data_mean,a,1); | |
DataAdjust = Data_grayD – Data_meanNew; | |
cov_data = cov(DataAdjust); | |
[V, D] = eig(cov_data); | |
V_trans = transpose(V); | |
DataAdjust_trans = transpose(DataAdjust); | |
FinalData = V_trans * DataAdjust_trans; | |
% End of PCA code | |
% Start of Inverse PCA code, | |
OriginalData_trans = inv(V_trans) * FinalData; | |
OriginalData = transpose(OriginalData_trans) + Data_meanNew; | |
figure, | |
set(gcf,'numbertitle','off','name','RecoveredImage'), | |
imshow(OriginalData) | |
% End of Inverse PCA code | |
% Image compression | |
PCs=input('Enter number of PC colomuns needed? '); | |
PCs = b - PCs; | |
Reduced_V = V; | |
for i = 1:PCs, | |
Reduced_V(:,1) =[]; | |
end | |
Y=Reduced_V'* DataAdjust_trans; | |
Compressed_Data=Reduced_V*Y; | |
Compressed_Data = Compressed_Data' + Data_meanNew; | |
figure, | |
set(gcf,'numbertitle','off','name','Compressed Image'), | |
imshow(Compressed_Data) | |
% End of image compression |
Results:
Original image |
After keeping only 5 eigenvectors |
After keeping only 10 eigenvectors |
After keeping only 20 eigenvectors |