function make_bd_and_blobworld(...
    CC,path1,filename,Xdim,Ydim,size_thresh,con_s,aniso_s,pol_s)

RGB=double(imread(filename))/255;
[labL,labA,labB]=corelread(filename);
[x,y]=meshgrid(1:Xdim,Ydim:-1:1);

CCfinal=zeros(Ydim,Xdim);
ccnum=0;
for n=1:ceil(max(double(CC(:))))
   on1=(CC==n);
   on2=bwmorph(bwmorph(on1,'fill'),'erode'); % lose edge pixels
   if sum(on2(:))>(size_thresh*(Ydim*Xdim))
      index1=find(on1);
      index2=find(on2);
      ccnum=ccnum+1;
      CCfinal(index1)=ccnum;
      
      % get texture and texture bins
      bd(ccnum).con=mean(con_s(index2));
      bd(ccnum).aniso=mean(con_s(index2).*aniso_s(index2));
      con=bd(ccnum).con/.6;
      con=con*(con<1)+(con>=1);
      aniso=bd(ccnum).aniso/.8;
      %%%%%%%aniso=bd(ccnum).aniso*con/.8;
      aniso=aniso*(aniso<1)+(aniso>=1);
      cbin=ceil(con*5)+(con==0);
      abin=ceil(aniso*4)+(aniso==0);
      bd(ccnum).texbin=(cbin-1)*4+abin;
      
      % get location bins
      bd(ccnum).x=mean(x(index1));
      bd(ccnum).y=mean(y(index1));
      xbin=ceil(bd(ccnum).x*3)+(bd(ccnum).x==0); %%% these are wrong!!!
      ybin=ceil(bd(ccnum).y*3)+(bd(ccnum).y==0);
      bd(ccnum).locbin=(ybin-1)*3+xbin;

      % get shape descriptors (oriented & not) and bins
      bd(ccnum).shape_coeffs_saved=20;
      %bd(ccnum).shape_coeffs_used=10;
      [bd(ccnum).shape_oriented,...
	     bd(ccnum).shape_nonoriented,...
	     bd(ccnum).shape_oriented_size_inv,...
	     bd(ccnum).shape_nonoriented_size_inv,...
	     bd(ccnum).AB,bd(ccnum).B,bd(ccnum).fourier_mean]=...
	  shape(on1,bd(ccnum).shape_coeffs_saved);
      bd(ccnum).ABbin=ceil(5*(bd(ccnum).AB/(Xdim*Ydim)));
      %%bd(ccnum).ABbin=ceil(5*sqrt(bd(ccnum).AB/(Xdim*Ydim)));
      bd(ccnum).Bbin=ceil(5*bd(ccnum).B);
      
      % get pixel count
      bd(ccnum).pixels=sum(on1(:));
      %%bd(ccnum).pixbin=ceil(5*sqrt(sum(on1(:))/(Xdim*Ydim)));
      bd(ccnum).pixbin=ceil(5*(sum(on1(:))/(Xdim*Ydim)));
  
      % get average color (for BW)
      bd(ccnum).mean_color(1)=mean(labL(index2));
      bd(ccnum).mean_color(2)=mean(labA(index2));
      bd(ccnum).mean_color(3)=mean(labB(index2));
   end
end

% sort blobs by area, largest to smallest
sizes=[];
for n=1:ccnum
   sizes(n)=bd(n).pixels;
end
[junk,ind]=sort(-sizes);
bd=bd(ind);
CC=zeros(Ydim,Xdim);
for n=1:ccnum
   CC=CC+n*(CCfinal==ind(n));
end

% get color histogram
color_hist=get_lab_hist(CC,filename);
for n=1:ccnum
   bd(n).color_hist=color_hist(n,:);
end
whole_color_hist=color_hist(ccnum+1,:);

% get texture histogram for whole image
whole_con_hist=hist(con_s(:),0:0.05:1);
whole_con_hist=whole_con_hist/sum(whole_con_hist);
whole_aniso_hist=hist(con_s(:).*aniso_s(:),0:0.05:1);
whole_aniso_hist=whole_aniso_hist/sum(whole_aniso_hist);

% create blobworld.jpeg and support.map
[AVG,MASK,support,rings] = blobs2im(CC,RGB,bd,Xdim,Ydim);


% for n=1:ccnum
%    % create blobn.jpeg and blobnring.pbm
%    blobring=bwmorph(squeeze(rings(:,:,n)),'dilate');
%    
%    imwrite(blobring,[path1 '_blob' int2str(n) 'ring.tiff']);
%    % changed for windows -- Yi Li
%    %imwrite(blobring,['/tmp/tmp.tiff']);
%    %[junk1,junk2]=unix(...
%    %    ['tifftopnm /tmp/tmp.tiff >! ' path1 '_blob' int2str(n) 'ring.pbm']);
%    %if ~strncmp(junk2,'tifftopnm: writing PBM file',27)
%    %   errmsg=sprintf('Error: response from tifftopnm was |%s|',junk2);
%    %   error(errmsg);
%    %end
%    %unix('""rm /tmp/tmp.tiff');
%    RGBblob=reshape(AVG,Ydim*Xdim,3);
%    ind=find(blobring);
%    RGBblob(ind,1)=1;
%    RGBblob(ind,2)=0;
%    RGBblob(ind,3)=0;
%    imwrite(reshape(RGBblob,Ydim,Xdim,3),[path1 '_blob' int2str(n) '.jpeg']);
% end


write_support(path1,support,Xdim,Ydim)
write_feature_vector(path1,bd,ccnum)
% YI LI: unused output
% save([path1 '_bd.mat'], 'CC','bd','whole_color_hist','whole_con_hist',...
%        'whole_aniso_hist','Xdim','Ydim','filename','support');
imwrite(AVG,[path1 '_blobworld.jpeg']);
imwrite(MASK,[path1 '_mask.jpeg']);

return


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


function write_support(path1,support,Xdim,Ydim)

fid=fopen([path1 '_support.map'],'w');

fprintf(fid,'%i %i\n',Ydim,Xdim);

for i=1:Ydim
   fprintf(fid,'%i ',support(i,:));
   fprintf(fid,'\n');
end

fclose(fid);

return


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% YI LI: output the color and texture attribute of each region
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


function write_feature_vector(path1,bd,ccnum)

fid=fopen([path1 '_ft.txt'],'w');

fprintf(fid,'L A B contrast anisotropy count\n');

for i=1:ccnum
   fprintf(fid,'%f %f %f %f %f %i\n', bd(i).mean_color(1), bd(i).mean_color(2), bd(i).mean_color(3), bd(i).con, bd(i).aniso, bd(i).pixels);
end

fclose(fid);

return


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Copyright (c) 1997-1999. The Regents of the University
% of California (Regents). All Rights Reserved.
% 
% Permission to use, copy, modify, and distribute this software and its
% documentation for educational, research, and not-for-profit purposes,
% without fee and without a signed licensing agreement, is hereby
% granted, provided that the above copyright notice, this paragraph and
% the following two paragraphs appear in all copies, modifications, and
% distributions. Contact The Office of Technology Licensing, UC Berkeley,
% 2150 Shattuck Avenue, Suite 510, Berkeley, CA 94720-1620, (510)
% 643-7201, for commercial licensing opportunities. Created by
% Chad Carson, Serge Belongie, and Jitendra Malik, Department of Electrical
% Engineering and Computer Sciences, University of California, Berkeley.
% For technical information, contact carson@eecs.berkeley.edu.
% 
% IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
% SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
% ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
% REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
% 
% REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
% PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF
% ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION
% TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
% MODIFICATIONS.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

