% read the raw images, their masks and their labels:
dpath='/Volumes/mm/PRLab/Data/icpr2012_continued/data/Main_Dataset/';
N = 28;

X = cell(1,1);
k=0;
for i=1:N
   i
   % first read the label file
   dinfo = importdata(sprintf('%s/Images/%02d/%02d.csv',dpath,i,i));
   dat = dinfo.textdata(2:end,:);
   %cellid = str2num(cell2mat(dat(:,2)));
   cellid = str2num(strvcat(dat(:,2)));
   pattlab = dat(:,6);
   % then read the images 
   fnames = dir(sprintf('%s/Cells/Cell_Images/%02d/*.png',dpath,i));
   M = size(fnames,1);
   for j=1:M
      k = k+1;
      nr = str2num(fnames(j).name(1:3));
      fullfname = sprintf('%s/Cells/Cell_Images/%02d/%s',dpath,i,fnames(j).name);
      X{k,1} = importdata(fullfname);
      fullfname = sprintf('%s/Cells/Cell_Masks/%02d/%s',dpath,i,fnames(j).name);
      bigmask = importdata(fullfname);
      X{k,2} = (bigmask(dinfo.data(nr,2):dinfo.data(nr,4)-1,...
                       dinfo.data(nr,1):dinfo.data(nr,3)-1) > 0);
      X{k,3} = i;
      X{k,4} = pattlab(nr,:);
      X{k,5} = nr;

   end
end


Itrain = [ 1 2 3 5 6 7 8 9 11 14 18 20 25 26];
Itest = [ 4 10 12 13 15 16 17 19 21 22 23 24 27 28];
I = cell2mat(X(:,3));
j = [];
for i=1:length(Itrain)
   j = [j; find(I==Itrain(i))];
end
x = X(j,:); 
j = [];
for i=1:length(Itest)
   j = [j; find(I==Itest(i))];
end
z = X(j,:); 
save cells x z;

