From c40932c3967b18ebf7b18622774547b825e354d6 Mon Sep 17 00:00:00 2001 From: sonnenberg Date: Mon, 20 Jan 2020 16:30:00 +0100 Subject: [PATCH] added PCA solution --- .../solution/ConeResponse.m | 31 +++++++++ .../solution/gauss.m | 3 + .../solution/main.m | 65 +++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 projects/project_pca_natural_images/solution/ConeResponse.m create mode 100644 projects/project_pca_natural_images/solution/gauss.m create mode 100644 projects/project_pca_natural_images/solution/main.m diff --git a/projects/project_pca_natural_images/solution/ConeResponse.m b/projects/project_pca_natural_images/solution/ConeResponse.m new file mode 100644 index 0000000..cc85ab8 --- /dev/null +++ b/projects/project_pca_natural_images/solution/ConeResponse.m @@ -0,0 +1,31 @@ +function Y = ConeResponse(X) + +wl_r = 700; +wl_g = 510; +wl_b = 440; +wl = [wl_r; wl_g; wl_b]; + +mu_s = 445; +mu_m = 545; +mu_l = 575; + +sig_s = 20; +sig_m = 40; +sig_l = 45; + +s = gauss(wl,mu_s,sig_s)/gauss(mu_s,mu_s,sig_s); +m = gauss(wl,mu_m,sig_m)/gauss(mu_m,mu_m,sig_m); +l = gauss(wl,mu_l,sig_l)/gauss(mu_l,mu_l,sig_l); + +S = X'*s; +M = X'*m; +L = X'*l; + +Y = [L, M, S]'; +% +% close all +% hold on +% plot(lam,s,'b') +% plot(lam,m,'g') +% plot(lam,l,'r') +% hold off diff --git a/projects/project_pca_natural_images/solution/gauss.m b/projects/project_pca_natural_images/solution/gauss.m new file mode 100644 index 0000000..a327ee1 --- /dev/null +++ b/projects/project_pca_natural_images/solution/gauss.m @@ -0,0 +1,3 @@ +function Y = gauss(X,mu,sigma) +Y = exp(-0.5*((X-mu)/sigma).^2)./sqrt(2*pi*sigma^2); +% Y = Y/exp(-0.5*(mu/sigma).^2)./sqrt(2*pi*sigma^2); diff --git a/projects/project_pca_natural_images/solution/main.m b/projects/project_pca_natural_images/solution/main.m new file mode 100644 index 0000000..7d411bc --- /dev/null +++ b/projects/project_pca_natural_images/solution/main.m @@ -0,0 +1,65 @@ +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 \ No newline at end of file