Question

In: Computer Science

I want to create an image compression program with matlab use PCA. I have the code...

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);

Solutions

Expert Solution

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

Related Solutions

I want to make picture compress program in matlab use eigenmatrix in RGB with PCA. This...
I want to make picture compress program in matlab use eigenmatrix in RGB with PCA. This code listed below. But this code is fail. I want to make the picture have a colour, but in my program the picture is still colorless. Can you 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 -...
can you please create the code program in PYTHON for me. i want to create array...
can you please create the code program in PYTHON for me. i want to create array matrix Nx1 (N is multiple of 4 and start from 16), and matrix has the value of elements like this: if N = 16, matrix is [ 4 4 4 4 -4 -4 -4 -4 4 4 4 4 -4 -4 -4 -4] if N = 64, matrix is [8 8 8 8 8 8 8 8 -8 -8 -8 -8 -8 -8 -8...
Could you create me a secant code in Matlab program? That's I can keep it and...
Could you create me a secant code in Matlab program? That's I can keep it and use it for any time?
Using Matlab 1. Create a color image of size 200 × 200. The image should have...
Using Matlab 1. Create a color image of size 200 × 200. The image should have a blue background. 2. Create 100 yellow regions within the image that have a variable size. More specifically, their width should be an odd number and vary between 3 and 7 and their height should also be an odd number and vary between 3 and 7. For example, you may get rectangular regions of size 3 × 3, 3 × 5, 5 × 3,...
I want the code for the 2D Ising model using Matlab
I want the code for the 2D Ising model using Matlab
In python using tkinter, I want to create a program. The program can have various classes...
In python using tkinter, I want to create a program. The program can have various classes and methods but I want it to have circles, triangles, and squares. Each shape movies in a certain way, like circles can move linearly, triangles can be affected by gravity, and squares can only go up and down. If the shapes get too close to one another then they disappear and a new shape appears somewhere else. I want this program to run continuously.
C Programming Run Length Encoder (Compression). I am writing a program that takes an image (from...
C Programming Run Length Encoder (Compression). I am writing a program that takes an image (from a text file) and looks for patterns of 2 or more duplicate values. The program should replace these values with the following pattern:  2 such characters followed by an Integer ( which represents number of occurrences of each character), followed by an asterisk. For example say the input of the non-compressed image was: ,,,,)H 7. i.e. it consists of four commas, one closed bracket, the...
Hi. I want matlab code that implements a mimo system that uses the mmse and zf...
Hi. I want matlab code that implements a mimo system that uses the mmse and zf methods with Rayleigh fading. Thank you.
So, I have this Matlab program that I have made for a lab, and despite having...
So, I have this Matlab program that I have made for a lab, and despite having different frequencies, when I go to plot them, the graphs look exactly the same. Can someone tell me why that is? I also need to know how to determine the digital frequencies(rad/sec) and how many samples per period of both of the waves? Thank you Code: N = 100; % Total number of time domain samples in simulation. A1 = 1; % Amplitude of...
MATLAB code: using Laplacian mask enhance for image "ngc6543.jpg".
MATLAB code: using Laplacian mask enhance for image "ngc6543.jpg".
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT