Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %% Compress colored image with PCA algorithm
- clear
- close all
- clc
- A = double(imread('blue_bird.jpg'));
- % Display original image
- subplot(1, 2, 1);
- imagesc(A/255)
- title('Original image')
- % Convert image to 2d
- img_size = size(A);
- X = reshape(A, img_size(1) * img_size(2), 3);
- % Normalize data before performing PCA
- [X_norm, mu, sigma] = featureNormalize(X);
- % Run PCA
- [U, S] = pca(X_norm);
- % Reduce data to the first 2 eigenvectors
- K = 2;
- Z = projectData(X_norm, U, K);
- % Convert back to 3d and display compressed image
- Q = reshape((X_norm+mu).*sigma, img_size(1),img_size(2), 3);
- subplot(1, 2, 2);
- imagesc(Q/255);
- title('Compressed image')
- % Create the new compressed image
- imwrite(Q,'blue_bird_compressed.jpg');
- % Find out how much space we saved
- original = dir('blue_bird.jpg');
- compressed = dir('blue_bird_compressed.jpg');
- oSize = original.bytes;
- cSize = compressed.bytes;
- saved = (1-(cSize/oSize))*100;
- disp(['Before: ', num2str(oSize), ' bytes']);
- disp(['After: ', num2str(cSize), ' bytes']);
- disp([num2str(saved), '% space saved']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement