With PCA, how much of the photo did i compress?
조회 수: 1(최근 30일)
Here my own PCA algorithm code that i create by myself by inspiring Prof. Andrew Ng's ML lectures.
It works very well.
The problem that I do not know how much of the original photo i compressed and how to press it on the title of the photo.
In title i pressen the number of colums for original and number of principal component for reversed (reconstracted) photo, is that a true way to specify how much i compressed?
SO, could you please check and edit the title(sprintf('....')) parts?
% imshow(X)=imshow(b) ile aynıdır
% imagesc(X)= imagesc(b) ikisi renkli
% K = 20; % or find K with below algorithm
if sum(ss2)/ss >=0.99
Z = projectData(Xn, U, K);
Xappx= recoverData(Z, U, K);
subplot(1, 2, 1);
title(sprintf('Original: %d features', size(X,2)));
subplot(1, 2, 2);
title(sprintf('Recovered: with top %d principal component', K));
function [U, S, X] = pca(X)
% U = zeros(n);
% S = zeros(n);
sigma = (1/m)*(X'*X);
[U, S , ~] = svd(sigma);
function Z = projectData(X, U, K)
% Z = zeros(size(X, 1), K);
U_reduce = U(:,(1:K)); % n x K
Z = X * U_reduce; % m x k
function X_rec = recoverData(Z, U, K)
% X_rec = zeros(size(Z, 1), size(U, 1));
% m * n
X_rec = Z * U(:,1:K)'; %=m*n
William Rose 2022년 7월 15일
The percent compression is (Au-Ac)/Au, where Au= the amount of information needed to generate the uncompressed image, and Ac= the amount of information needed to generate the compressed image.
Au: Image ben.png is 1408x1849 pixels. The monochrome image (for example, the green channel of the image, which you chose) has 1 byte per pixel, as represented in Matab. Therefore Au=2,603,392 bytes. (A monochrome PNG file on disk will probably be smaller, because PNG uses a lossless compression algorithm.)
Ac: To reconstruct an image that has been compressed with PCA, you need the basis set images and the weighting factors which tell you how much of each basis image to use in the reconstruction. Therefore PCA actually requires MORE infrmation that the original image, if you are only compressing one image: You have to supply each of the basis images, plus the weighting factors. If you are compressing a large set of images, then PCA can produce good compression, because you use a common basis set for all the images, and a small set of wegihting factors for each images. For example, supose you had 1000 images with the same size as "ben". The raw monocrome images require 1408x1849x1000 bytes, i.e. Au=2.603 x 10^9 bytes. If you recontsruct the images using the first 20 principal components, you would need 1408x1849x20 for the basis images, plus 20x1000 for the weighting factors, i.e Ac=5.209 x 10^6 bytes.
The compression percentage, if you had 1000 images and reconstructed them using 20 principal components (i.e. a basis set of 20 images), would be (Au-Ac)/Au= 98%. The compression ratio for one image with PCA is a negative number, bcause you need more informatin to reconstruct it with PCA than the original image.
See this article for more.
For a set of RGB images, you can do PCA on each color independently.
William Rose 2022년 7월 19일
@ali yaman, The attached script applies PCA image compression to image ben.jpg. It reconstructs the image with 10 principal components and with 20 PCs. The compressed files are saved as ben10.jpg and ben20.jpg. The red, blue, and green components are compressed separately and are combined after compression.
From the comments in the script:
%Demonstrate the use of PCA for color image compression.
%An image is read from disk. It is split into R, G, B components.
%Each color is compressed with PCA. The compressed color slices are combined
%to reconstruct color images. Reconstructed color images are saved to disk.
%The color and R,G,B channels of the original and reconstructed images
%are displayed in low resolution as an array of images.
%To compress a different file, change the value of imagefile.
%To compress with different numbers of PCs, change the value of numpc,
%for example, numpc=15 or [10,20] or [6,12,24] or [10,20,50,1000].
%To see the true effects of compression, the user should view the original
%and reconstructed images at full resolution.