function CC=cleanup(CC,filename,Xdim,Ydim,NUM_STEPS,NUM_ERODES,SCORE_MULT)

%% parameters to tune postprocessing; in 10K run (Nov. '98) these were
%%NUM_STEPS=4;
%%NUM_ERODES=2;
%%SCORE_MULT=2;

%%% no size thresholding has been done yet

global actual_centers

RGB=imread(filename);
[X,RGBmap]=cmunique(RGB);
if min(X(:))~=1 | max(X(:))~=size(RGBmap,1)
   disp('cleanup: min(X(:))~=1 | max(X(:))~=size(RGBmap,1)')
%   keyboard
end

[X,RGBmap]=imapprox(X,RGBmap,min(2048,size(RGBmap,1)));
LABmap=rgb2lab(RGBmap);

if isa(X,'double') & min(X(:))>=1 & max(X(:))<=size(RGBmap,1)
   bin_image=get_bin_image(X,LABmap,actual_centers);
elseif isa(X,'uint8') & min(X(:))==0 & max(X(:))<=size(RGBmap,1)-1
   bin_image=get_bin_image(double(X)+1,LABmap,actual_centers);
elseif isa(X,'uint16') & min(X(:))==0 & max(X(:))<=size(RGBmap,1)-1
   bin_image=get_bin_image(double(X)+1,LABmap,actual_centers);
elseif isa(X,'uint16') & min(X(:))>=1 & max(X(:))<=size(RGBmap,1)
   bin_image=get_bin_image(double(X),LABmap,actual_centers);
else
   disp('neither case')
   keyboard
end

%%% this is an image/matrix containing the color bin (1-240) for each pixel
%%% (we could actually do this before any segmentation if we wanted)


K=max(bin_image(:));
[x,y]=meshgrid(1:Xdim,1:Ydim);

%fprintf(1,'a');

for step=1:NUM_STEPS
   CCnew=CC;
   N=max(CC(:));
   color_hist=zeros(N,K);
   for i=1:N
      interior=find(bwmorph(CC==i,'erode',NUM_ERODES));
      if length(interior)>0
	 color_hist(i,:)=hist(bin_image(interior),1:K);
	 color_hist(i,:)=color_hist(i,:)/sum(color_hist(i,:));
      end
   end
   
   CCmat=[];
   CCmat2=zeros(Ydim*Xdim,9);
   for r=-1:1
      for c=-1:1
	 CCmat=[CCmat ...
		reshape(CC(2+r:2+r+Ydim-3,2+c:2+c+Xdim-3), ...
		(Xdim-2)*(Ydim-2),1)];
      end
   end
   for i=1:9
      CCmat2(:,i)= ...
	  reshape(frame(reshape(CCmat(:,i),Ydim-2,Xdim-2),1),Ydim*Xdim,1);
   end
   clear CCmat
      
   for i=1:N
      bdy=find(bwmorph(CC==i,'remove')&x>9&x<Xdim-8&y>9&y<Ydim-8);
      %% this ANDing is to avoid the outer border (just leave as 0)
      %% (since we didn't group the outer 8 pixel border)
      
      if length(bdy)>0
	 scores=color_hist(:,bin_image(bdy));
	 near=CCmat2(bdy,:);
	 score=zeros(length(bdy),N);
	 for n=1:N
	    score(:,n)=(sum(near==n,2)>0).*scores(n,:)';
	 end
	 [sc,ind]=max(score,[],2);
	 CCnew(bdy)=ind.*(sc>SCORE_MULT*score(:,i)) + ...
	               i*(sc<=SCORE_MULT*score(:,i));
      end
   end
   CC=zeros(Ydim,Xdim);
   last_ccmax=0;
   for n=1:N
      CCtemp=CCnew==n;
      if max(CCtemp(:))>0
	 cc=cclabel(CCtemp)+last_ccmax*(CCtemp~=0);
	 CC=CC+cc;
	 last_ccmax=max(CC(:));
      end
   end
end

CCnew=maxvote(CC+1)-1; %% maybe leave this out
N=max(CCnew(:));
CC=zeros(Ydim,Xdim);
last_ccmax=0;
for n=1:N
   CCtemp=CCnew==n;
   if max(CCtemp(:))>0
      cc=cclabel(CCtemp)+last_ccmax*(CCtemp~=0);
      CC=CC+cc;
      last_ccmax=max(CC(:));
   end
end



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

