params

load necessary218_full_U.mat

fidlist=fopen(CDLIST,'r');
fid_all=fopen([DATAPATH 'data35000_filter.txt'],'w');

n=0;

while 1
   if feof(fidlist), break, end
   cdname=fgetl(fidlist);    % this is, e.g., 1000
   if isempty(cdname), break, end
   fprintf(1,'%s\n',cdname);

   for i=0:99
      %%if n>4,break,end
      n=n+1;
      imgname=int2str(str2num(cdname)+i);
      dirpath=[DATAPATH cdname '/' imgname '/'];
      imgpath=[DATAPATH cdname '/' imgname '/' imgname];
   
      eval(['load ' imgpath '_bd.mat']);
      
      fid_img=fopen([imgpath '_data218_filter.txt'],'w');
   
      fprintf(fid_all,'%s\t',imgname);

      wich=whole_color_hist;
      if sum(wich)<0.97 | sum(wich)>1.03
	 fprintf(1,'imgnum=%i, sum(wich)=%.4f\n',imgnum,sum(wich));
	 keyboard
      end
      
      % write number of blobs in image
      fprintf(fid_all,'%i\n\n',length(bd));      
      fprintf(fid_img,'%i\n\n',length(bd));      

      for m=1:length(bd)
	 blob=bd(m);
      
	 % write texture and location values
	 true_texture=[blob.con blob.con*blob.aniso];
	 %% should be true_texture=[blob.con blob.aniso]; but
	 %% for 35,000 run, was true_texture=[blob.con blob.con*blob.aniso];
	 %% (double multiplying!)--need to do filter data the same way
	 
	 x=blob.x/Xdim;
	 y=blob.y/Ydim;
	 fprintf(fid_all, '%.4f %.4f\n%.4f %.4f\n', ...
	     true_texture(1), true_texture(2), x, y);
	 fprintf(fid_img, '%.4f %.4f\n%.4f %.4f\n', ...
	     true_texture(1), true_texture(2), x, y);
	 
	 % write shape values (just first coefficient; 10 are used for final
	 % match, but the first is the largest)
	 shape=blob.shape_oriented(:);
	 fprintf(fid_all, '%.4f ', shape(1:4));
	 fprintf(fid_all, '\n');
	 fprintf(fid_img, '%.4f ', shape(1:4));
	 fprintf(fid_img, '\n');
      
	 % find background histogram
	 area=blob.pixels/(Xdim*Ydim);
	 ch=blob.color_hist;
	 background=wich-area*ch;
	 background=background/sum(background);

	 % write first five coefficients of transformed color histogram
	 x = Up*ch(1:end-1)';
	 fprintf(fid_all, '%.4f\t', x(1:5));
	 fprintf(fid_all, '\n');
	 fprintf(fid_img, '%.4f\t', x(1:5));
	 fprintf(fid_img, '\n');
	 
	 % write first five coefficients of transformed background color hist.
	 x = Up*background(1:end-1)';
	 fprintf(fid_all, '%.4f\t', x(1:5));
	 fprintf(fid_all, '\n\n');
	 fprintf(fid_img, '%.4f\t', x(1:5));
	 fprintf(fid_img, '\n\n');
      end
      fclose(fid_img);
      unix(['chmod 664 ' imgpath '_data218_filter.txt']);
      
      fprintf(fid_all, '\n');
   end
end

fclose(fid_all);
fclose(fidlist);
disp([int2str(n) ' images.'])
