%% SIFT_avg.m
%% 11/8/2006
%% Compute SIFT visualization by averaging SIFT patch regions.

% $$$ CODE_DIR = '/afs/csail.mit.edu/u/b/brussell/research/TopLearn/words_code/Release/words-code';
% $$$ CLUSTER_DIR = '/afs/csail.mit.edu/u/b/brussell/research/TopLearn/words_code/Release/clusters';
% $$$ IMAGE_DIR = '/afs/csail.mit.edu/u/b/brussell/research/TopLearn/words_code/Release/Datasets/LabelMe/images';
% $$$ OUT_DIR = '/afs/csail.mit.edu/u/b/brussell/research/TopLearn/words_code/Release/Datasets/LabelMe';

% $$$ CODE_DIR = '/afs/csail.mit.edu/u/b/brussell/research/TopLearn/ProjectRelease/words-code';
% $$$ CLUSTER_DIR = '/afs/csail.mit.edu/u/b/brussell/research/HierDisc/SpatialWords/out/LabelMe_dense/clusters';
% $$$ IMAGE_DIR = '/afs/csail.mit.edu/u/b/brussell/research/HierDisc/SpatialWords/images/LabelMe';
% $$$ OUT_DIR = '/afs/csail.mit.edu/u/b/brussell/research/HierDisc/SpatialWords/out/LabelMe_dense';

% $$$ CODE_DIR = '/afs/csail.mit.edu/u/b/brussell/research/TopLearn/ProjectRelease/words-code';
% $$$ CLUSTER_DIR = '';
% $$$ IMAGE_DIR = '/afs/csail.mit.edu/u/b/brussell/research/HierDisc/SpatialWords/images/LabelMe';
% $$$ OUT_DIR = '/afs/csail.mit.edu/u/b/brussell/research/HierDisc/SpatialWords/LabelMe_visual_words/LabelMe_dense_V10';

CODE_DIR = '/afs/csail.mit.edu/u/b/brussell/research/TopLearn/ProjectRelease/words-code';
CLUSTER_DIR = '';
IMAGE_DIR = '/afs/csail.mit.edu/u/b/brussell/research/HierDisc/SpatialWords/images/LabelMe';
OUT_DIR = '/afs/csail.mit.edu/u/b/brussell/research/HierDisc/SpatialWords/LabelMe_visual_words/LabelMe_dense_V100';

addpath(CODE_DIR);
add_matlab_paths(CODE_DIR);

disc_T = 20;
% $$$ P = set_params(CODE_DIR,IMAGE_DIR,CLUSTER_DIR,OUT_DIR,'disc.T',disc_T,'words.doplot',0);
% $$$ P = set_params(CODE_DIR,IMAGE_DIR,CLUSTER_DIR,OUT_DIR,'disc.T',disc_T,'words.doplot',0,'words.cluster_fnames',{fullfile(CLUSTER_DIR,'d101-tp101357_n1_sift3_s1.0_pp90_K1.0_t0.VQadam')});
% $$$ P = set_params(CODE_DIR,IMAGE_DIR,CLUSTER_DIR,OUT_DIR,'disc.T',disc_T,'words.doplot',0);
P = set_params(CODE_DIR,IMAGE_DIR,CLUSTER_DIR,OUT_DIR,'disc.T',disc_T,'words.doplot',0);

P = load_word_mat(P);
P.params.disc.V = 1001; %% fix josef bug
P = form_word_doc_mat(P);

ff = figure;

num_retrieve = 64;

word_doc_cts = col_sum(P.disc.f>0);
words = find(word_doc_cts>=num_retrieve);
avg_vwords = zeros(128,128,cols(P.disc.f));
for jj = 1:length(words)
  ww = words(jj);
  disp(sprintf('%d out of %d\r',ww,words(end)));
% $$$   title(sprintf('%d out of %d',ww,words(end)));
  accum = zeros(128,128);
  im_ind = find(P.disc.f(:,ww)>0);
  im_ind = im_ind(randperm(length(im_ind)));
  for ii = 1:num_retrieve
    im = rgb2gray(read_im(P,im_ind(ii)));
    findex = get_findex(P,im_ind(ii));

    idx_found_word = find(findex.word_label==ww);
    idx_found_word = idx_found_word(randperm(length(idx_found_word)));

    fts = findex.fts(:,idx_found_word(1));
    minx = floor(max(1,fts(1)-fts(3)));
    maxx = ceil(min(size(im,2),fts(1)+fts(3)));
    miny = floor(max(1,fts(2)-fts(5)));
    maxy = ceil(min(size(im,1),fts(2)+fts(5)));
    patch = im(miny:maxy,minx:maxx,:);
    accum = accum + imresize(double(patch)/255,[128 128],'bicubic');
  end

  accum = accum/num_retrieve;
  avg_vwords(:,:,ww) = accum;

  clf(ff);
  imshow(accum);
  drawnow;
  
  if mod(jj,100)==0
    disp('saving...');
    save avg_vwords_1001.mat avg_vwords;
    disp('done');
  end
end

% $$$ save avg_vwords_v6.mat avg_vwords;
% $$$ save avg_vwords_1001_v6.mat avg_vwords;
% $$$ save avg_vwords_11_v6.mat avg_vwords;
save avg_vwords_101_v6.mat avg_vwords;
