%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  CreateTMI.m
%
%  Copyright (c) 2012 Homayoun Bagherinia 
%
% This program is free software: you can redistribute it and%or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program.  If not, see <http:%%www.gnu.org%licenses/>.
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%
function [C CC] = CreateTMI(out)

% function [C CC] = CreateTMI(out)
% 
% Interactive utility to collect training color data from images of a color marker.
% 
% Example of use:
%   [C CC] = createTMI('tst');
% or
%   createTMI('tst');
%
% input/output:
%   'tst': prefix of .mat file where results are stored
%   C:  cell array. Each cell is a matrix of 12x25 (4 colors and 25 samples per each color).
%   CC: matrix containing all training data from selected training
%       images
%   
%   C and CC are stored in tst_n.mat, where n is the number of images
%   considered
%


C = []; CC = [];
imList = {};
k = 0;
suf = findstr(out,'.mat');
if(isempty(suf))
    out = [out,'.mat'];
end
if exist(out)==2
    disp(['Output file ',out,' already exists.']);
    req = input(['Do you want to overwrite it (1) or to append data to it(2)? ']);
    if (req==2)
        disp(['Appending data to ',out]);
        load(out);
        k = size(C,1);
    else
        disp(['Overwriting ',out]);
    end
end
while 1
    [fn,fp] = uigetfile('*.*','Select training color marker image');
    if fn == 0
        break;
    end;
    
    disp(fn);
    
    imin = imread([fp filesep fn]);
    imagesc(imin);
    set(gca,'DataAspectRatio',[1 1 1]);
    axis off
    
    h = text(0,0,'Select marker area');
    set(h,'FontSize',15,'color',[1 0 0],'BackgroundColor',[0.5 0.5 0.5]);
    
    rect = round(getrect);
    xmin = rect(1);     ymin = rect(2);
    w = rect(3);        h = rect(4);

    k = k+1;
    imList{k} = [fp filesep fn];
    C{k,1} = roicolorpatch(imin(ymin:ymin+h,xmin:xmin+w,:),h);
    
    % RM 11/11/12
    CC = [];
    for i=1:size(C,1)
        CC = [CC C{i,1}];
    end

    if nargin==1
        if (exist(out)==2)
            disp(['Appending training data to file ',out])
            save(out, 'C', 'CC', 'imList', '-append');
        else
            disp(['Saving training data to file ',out])
            save(out, 'C', 'CC','imList');
        end            
    end

end;
close;

% CC = [];
% for i=1:size(C,1)
%     CC = [CC C{i,1}];
% end
% 
% close;
% if nargin==1
%     disp(['Saving training data in file ',out,'_',num2str(size(C,1)),'.mat'])
%     save(strcat(out,'_',num2str(size(C,1)),'.mat'), 'C', 'CC');
% end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% select regions for a target image
function C12 = roicolorpatch(I,hf)

imagesc(I);

N = 25;
C12 = zeros(N,12);

hf = text('string','Select green area','color',[0 1 0],'Position',[0 0],'BackgroundColor',[.5 .5 .5],'FontSize',15);
axis off
rect = round(getrect);
xmin = rect(1); ymin = rect(2);
w = rect(3);    h = rect(4);
I1 = I(ymin:ymin+h,xmin:xmin+w,:);
sizI1 = size(I1);
idx = [1:sizI1(1)*sizI1(2)];
idx1 = randsample(length(idx),N);
idx = idx(idx1);
I2 = I1(:,:,1);
C12(:,1) = I2(idx);
I2 = I1(:,:,2);
C12(:,2) = I2(idx);
I2 = I1(:,:,3);
C12(:,3) = I2(idx);

set(hf,'string','Select white area','color',[1 1 1],'Position',[0 0],'BackgroundColor',[.5 .5 .5],'FontSize',15);
rect = round(getrect);
xmin = rect(1); ymin = rect(2);
w = rect(3);    h = rect(4);
I1 = I(ymin:ymin+h,xmin:xmin+w,:);
sizI1 = size(I1);
idx = [1:sizI1(1)*sizI1(2)];
idx1 = randsample(length(idx),N);
idx = idx(idx1);
I2 = I1(:,:,1);
C12(:,4) = I2(idx);
I2 = I1(:,:,2);
C12(:,5) = I2(idx);
I2 = I1(:,:,3);
C12(:,6) = I2(idx);

set(hf,'string','Select orange area','color',[255/255 142/255 47/255],'Position',[0 0],'BackgroundColor',[.5 .5 .5],'FontSize',15);
rect = round(getrect);
xmin = rect(1); ymin = rect(2);
w = rect(3);    h = rect(4);
I1 = I(ymin:ymin+h,xmin:xmin+w,:);
sizI1 = size(I1);
idx = [1:sizI1(1)*sizI1(2)];
idx1 = randsample(length(idx),N);
idx = idx(idx1);
I2 = I1(:,:,1);
C12(:,7) = I2(idx);
I2 = I1(:,:,2);
C12(:,8) = I2(idx);
I2 = I1(:,:,3);
C12(:,9) = I2(idx);

set(hf,'string','Select black area','color',[0 0 0],'Position',[0 0],'BackgroundColor',[.5 .5 .5],'FontSize',15);
rect = round(getrect);
xmin = rect(1); ymin = rect(2);
w = rect(3);    h = rect(4);
I1 = I(ymin:ymin+h,xmin:xmin+w,:);
sizI1 = size(I1);
idx = [1:sizI1(1)*sizI1(2)];
idx1 = randsample(length(idx),N);
idx = idx(idx1);
I2 = I1(:,:,1);
C12(:,10) = I2(idx);
I2 = I1(:,:,2);
C12(:,11) = I2(idx);
I2 = I1(:,:,3);
C12(:,12) = I2(idx);

C12 = C12';
