
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%% Demo code %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% Spectral Total-Variation Local Scale Signatures %%%%%%%%
%%%%%%%% for Image Manipulation and Fusion %%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% Ester Hait and Guy Gilboa %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% EE department, Technion - Israel Institue of Technology%

% If you use this code, please cite our paper:
% Hait, Ester, and Guy Gilboa. "Spectral Total-Variation 
% Local Scale Signatures for Image Manipulation and Fusion." 
% IEEE Transactions on Image Processing 28.2 (2019): 880 - 895.
% https://ieeexplore.ieee.org/document/8476587

% Structure extraction and image manipulation 
% of an image with repetitive structures - leaf image %

clc,clear,close all

%%%%%%%%%%%%% Input image %%%%%%%%%%%%%%%%%%%%%%%%5
rgb_img=imread('leaf_veins.jpg');
rgb_img=im2double(imresize(rgb_img,1/2));
figure; imshow(rgb_img);title('Input RGB image');

% input for spectral TV decomposition: gryascale version
gray_img=rgb2gray(rgb_img);
figure; imshow(gray_img);title('Input grayscale image');

%%%%%%%%%%%%% Specral TV decomposition, time=scale %%%%%%%%%%%%%
% output = comprehensive scale and location information, Phi
addpath('specTV')
dt=1; % time step
dtf=100; % number of iterations
Max_time=dt*dtf; % total time
tic;[S,T,Phi,f_r,u2] = specTV_evolve_sym(gray_img, Max_time, dt);toc;
figure, imshow(f_r,[]); title('Residual image')
figure, plot((1:dtf)*dt, S,'LineWidth',4); xlabel('\bf t'); title('\bf Global spectrum','FontSize',11)

%%%%%%%%%%%%% K-means clustering %%%%%%%%%%%%%
[M,N,z]=size(Phi);
X=reshape(Phi,M*N,z);
K=4; % # of clusters
[idx,C] = kmeans(X,K,'replicates',10);
clust=reshape(idx,M,N);
figure;imagesc(clust);colormap('jet');axis off; title('Clustering result');

%%%%%%%%%%%%% Extracting interesting structures = veins %%%%%%%%%%%%%
for i=1:K
    vec_mean_clust(i)=mean(mean((clust==i).*gray_img));
end
[~,ind_clust]=sort(vec_mean_clust);
map=or(clust==ind_clust(1),clust==ind_clust(2));
figure;imshow(map); title('Map of interesting structures - leaf veins');

%%%%%%%%%%%%%% Image manipulation %%%%%%%%%%%%%
manip=1.5*rgb_img.*map+rgb_img.*(1-map);
figure;imshow(manip); title('Image manipulation example #1');
manip=1.5*rgb_img.*map+0.5*rgb_img.*(1-map);
figure;imshow(manip); title('Image manipulation example #2');

