addpath([pwd '/../core/minmax']);

%% load image
src_file='C:\Users\jml1g18\OneDrive - University of Southampton\Soton\CameraArray\photos\16.jpg';
im		= imread(src_file);
im_orig = double(im(1:3040, 1:4056,:));

%% process blue channel using principal component analysis
sz		= size(im_orig);
n_px	= sz(1)*sz(2);
[u,s,v] = svds(reshape(im_orig, [n_px 3]), 2);

%% extract pca image
im_pca	= reshape(u(:,2), sz(1:2));
im_pca	=-im_pca;
im_pca	= im_pca ./ max(im_pca(:));
im_pca	= 1-im_pca;

%% local background subtraction
c		= 2;
im_comp = double(im(:,:,c));
im_bg	= minfiltnd(single(im_comp), [11 11]);
G		= fspecial('gaussian', [16 16]);
G		= G/sum(G(:));
im_bg	= convn(im_bg, G, 'same');
im_filt = im_comp - im_bg;

%% display
figure(1);
clf;
imagesc(im_filt);
axis equal tight ij;
xlim([2600 3000]);
ylim([900 1200]);

%cmap=viridis;
%cmap(1,:)=0;
cmap = gray;
cmap(:,setdiff(1:3,c))=0;
colormap(cmap);
cb=colorbar;
caxis([0 64]);
%caxis([0.0 0.5])
%imwrite(im_pca,'test.tif');