Tuesday, September 3, 2013

Activity 13 - Image Compression


Now we tackle how jpeg/JPG images are compressed, and why it's one of the more popular image file formats used everywhere!

So first we need to understand how images are compressed.
JPEG images are usually split into blocks of 8 pixels. [1] They undergo the Discrete Cosine Transform (DCT), which is essentially the Fourier Transform but without the sine counter part. (only real values)
The following equation show the Cosine Transform:


We will try to simulate the Fourier Transform, by obtaining the Principal Components (PC) of an image.
This is done using the code below:

[lambda,facpr,comprinc] = pca(x);

The main use of this is to split the image into its eigenvalues and eigenvectors. From there we can reconstruct the image using only certain components of the image, without having to use the entire picture.

So our 1st step is to split the image into 10 x 10 pixels. This is easily done with the code given by Mum Jing:

I = imread('C:\Users\Phil\Desktop\Academic Folder\Academic Folder 13-14 First Sem\AP 186\Activity13\SF3.jpg');
I = im2double((I(:,:,1)+I(:,:,2)+I(:,:,3))/3);
//scf(); imshow(I);
//chop image into 10x10 blocks and assemble in a matrix x
clear x;
k=0;
for r = 1:8 //rows divided by block length
for c = 1:6 //columns divided by block width
itemp = I(((10*r-9):(10*r)),((10*c-9):(10*c)));
xtemp = itemp(:);
k = c + (r-1)*6;
x(k,:) = xtemp';
end

end

The image I will be using is a cropped image of the Dota 2 Hero Shadow Fiend:



Courtesy of biggreenpepper from deviantart website: http://biggreenpepper.deviantart.com/art/dota2-Shadow-Fiend-363299573



I crop the image into 80 x 60 pixels. I then use subdivide it into smaller matrices of 10x10.
I then take the PCA of these images and obtain the components of the image.
After which I reconstruct the image in grayscale.
Here is the image in its component form (a simulation of the Cosine Transform):


The following images are reconstructed of Shadow Fiends mouth.


Finally Shadow Fiend reconstructed at 90% corresponding to 50 coefficients and 100% at 100 coefficients!


Code Snippet used all thanks to Mum Jing!

//program to use pca for images
stacksize(10000000);
//load image and convert to grayscale
I = imread('C:\Users\Phil\Desktop\Academic Folder\Academic Folder 13-14 First Sem\AP 186\Activity13\SF.jpg');
I = im2double((I(:,:,1)+I(:,:,2)+I(:,:,3))/3);
I2 = mat2gray(I);
imwrite(I2,'C:\Users\Phil\Desktop\Academic Folder\Academic Folder 13-14 First Sem\AP 186\Activity13\ORIGSF.jpg');
//chop image into 10x10 blocks and assemble in a matrix x
clear x;
k=0;
[S,T] = size(I);
for r = 1:(S/10) //rows divided by block length
for c = 1: (T/10)//columns divided by block width
itemp = I(((10*r-9):(10*r)),((10*c-9):(10*c)));
xtemp = itemp(:);
k = c + (r-1)*(T/10);
x(k,:) = xtemp';
end
end
// apply pc to set x
//clear lambda, facpr, comprinc
[lambda,facpr,comprinc] = pca(x);
//test reconstruction- display pc's

VARY = 30;
coef = comprinc(:,1:VARY)';
EI = facpr(:,1:VARY);
IM1 = EI*coef;
//imshow(IM1);
y = zeros(S,T);
k=0;
for r = 1:(S/10)
    for c = 1:(T/10)
        k = c + (r-1)*(T/10);
        xtemp=IM1(:,k);
        y(((10*r-9):(10*r)),((10*c-9):(10*c))) = matrix(xtemp,10,10);
    end
end
imshow(y);
imwrite(y,'C:\Users\Phil\Desktop\Academic Folder\Academic Folder 13-14 First Sem\AP 186\Activity13\NEWSF3.jpg');
//newcomprinc = zeros(100,T)
// Normalizes EigenVector
//for j = 1:T
//    newcomprinc(:,j) = comprinc(:,j)/sqrt(sum(comprinc(:,j)^2))
//end

//scf();
//imshow(Recon);
//

I would give myself a 12 for this activity for I was able to also generalize the code for all sizes of the image!!

References
[1] Soriano, Jing ACT 13 - Image Compression, 2013




No comments:

Post a Comment