This repository has been archived on 2021-05-17. You can view files and clone it, but cannot push or open issues or pull requests.
scientificComputing/projects/project_pca_natural_images/solution/main.m
2020-01-20 16:30:00 +01:00

65 lines
1.3 KiB
Matlab

clear all
close all
pic = imread('D:\Dokumente\MATLAB\Matlabkurs2018\pca\natimg.jpg');
pic = pic(end:-1:1,:,:);
red = double(pic(:,:,1));
green = double(pic(:,:,2));
blue = double(pic(:,:,3));
% mat = [red(:),green(:),blue(:)];
mat = reshape(double(pic),size(pic,1)*size(pic,2),size(pic,3))';
cones = ConeResponse(mat);
% [coeff, score, latent] = pca(mat,'Centered',false);
% [coeff, score, latent] = pca(cones,'Centered',false);
% n = 10000;
% cones = cones(:,randi([1,size(cones,2)],1,n));
cones = mat;
cv = cov(cones');
[ev, ew] = eig(cv);
[ew, ew_idx] = sort(diag(ew), 'descend');
coeff = cones'*ev(:,ew_idx);
% coeff = [coeff'; zeros(size(pic,1)*size(pic,2)-size(coeff,2),3)];
c1 = reshape(coeff(:,1),size(pic,1),size(pic,2));
c2 = reshape(coeff(:,2),size(pic,1),size(pic,2));
c3 = reshape(coeff(:,3),size(pic,1),size(pic,2));
% score
figure
subplot(221)
contourf(red)
axis('square')
subplot(222)
contourf(green)
axis('square')
subplot(223)
contourf(blue)
axis('square')
colormap('gray')
subplot(224)
imshow('D:\Dokumente\MATLAB\Matlabkurs2018\pca\natimg.jpg')
axis('square')
figure
subplot(221)
contourf(c1)
axis('square')
subplot(222)
contourf(c2)
axis('square')
colormap('gray')
subplot(223)
contourf(c3)
axis('square')
colormap('gray')
% score