%
% This script shows how to use the LabelMe toolbox to read the annotations.
% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Object Recognition by Scene Alignment
% B. C. Russell, A. Torralba, C. Liu, R. Fergus, W. T. Freeman
% Advances in Neural Information Processing Systems, 2007.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% First download the image and annotation tarballs and extract them to:
%   yourpath/Images/
%   yourpath/Annotations/
%
% Download also the LabelMe toolbox and add it to the Matlab path:
%   http://labelme.csail.mit.edu/LabelMeToolbox/index.html
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% put here the name of the images and annotations paths:
HOMEIMAGES = 'yourpath/Images';
HOMEANNOTATIONS = 'yourpath/Annotations';

% You can also use the images online but it will work faster if you
% download your own copy of the images and annotations.
% HOMEIMAGES = 'http://labelme.csail.mit.edu/Images';
% HOMEANNOTATIONS = 'http://labelme.csail.mit.edu/Annotations';


% Create the LabelMe index structure:
DB = LMdatabase(HOMEANNOTATIONS);

% Replace "name" with "finaltag":
for ii = 1:length(DB)
  display(ii);
  for jj = 1:length(DB(ii).annotation.object)
    DB(ii).annotation.object(jj).name = DB(ii).annotation.object(jj).finaltag;
  end
end

% Get training/test set indices (jtrain,jtest):
trainNames = importdata('train.txt');
testNames = importdata('test.txt');
[D,jtrain] = LMquery(DB,'folder',lower([sprintf('%s,',trainNames{1:end-1}) trainNames{end}]),'exact');
[D,jtest] = LMquery(DB,'folder',lower([sprintf('%s,',testNames{1:end-1}) testNames{end}]),'exact');

% You can visualize the images:
LMdbshowscenes(DB(1:36), HOMEIMAGES);

% To see the object names and the number of instances:
[names, counts] = LMobjectnames(DB);

% The functions in the toolbox will allow you to search for objects and
% extract segmentation masks.
