
clear
%close all

% ask user for image 
in1 = input('Input image:\n','s');
in2 = input('Background number:\n   1. Savanna (Dirt)\n   2. Desert (Carpet)\n   3. Streets (Brick)\n','s');


in_im=['proj2_imag/', in1];
im=imread(in_im);
bkg_num=str2num(in2);


final_imag=zeros(size(im));  % allocate for the final image
final_imag2=im;
figure; imagesc(im); colormap(gray); title('Original Image')


% threshold
upper=256;
if( bkg_num==1 )
  lower=240; % for Dirt images
else
  lower=150; % for Duncan and Brick images 
end
y = im<=upper & im>=lower;
figure; imagesc(y); colormap(gray); title('Threshold')


% clean the image
SE2=ones(4,2);
ero=erode(im,SE2);
SE=ones(4,4);
dil=dilate(ero, SE);
y=dil<=upper & dil>=lower;


% convert it to a format matlab can deal with
img=zeros(size(y));
img(:,:)=y(:,:);
G1=img*200;


% clean up the background
if( bkg_num==3 )
  G2=erode(G1,ones(1,20));
  %figure; imagesc(G2); colormap(gray); title('G2')
  G2a=dilate(G2,ones(1,20));
  %figure; imagesc(G2a); colormap(gray); title('G2a')

  G2b=erode(G1,ones(20,1));
  %figure; imagesc(G2b); colormap(gray); title('G2b')
  G2c=dilate(G2b,ones(20,1));
  %figure; imagesc(G2c); colormap(gray); title('G2c')

  Gv=zeros(size(G2a));
  Gv(:,:)=G2a(:,:);
  Gh=zeros(size(G2c));
  Gh(:,:)=G2c(:,:);
  G2d=Gh+Gv;
  c=find( G2d<300 );
  G2d(c)=0;
  G2=zeros(size(G1));
  G2(66:414,66:575)=G2d(66:414,66:575);

  % get rid of cracks
  Dr=dilate(G2,ones(5,5));
  Er=erode(Dr,ones(5,5));
  G2(:,:)=Er(:,:);
  c=find(G2>100); G2(c)=400;

  figure; imagesc(G2); colormap(gray); title('Clean up the background')

else
  % remove things that are too small
  G2=zeros(size(G1));
  [row,col]=find(G1 > 50);
  for( i=1:length(row) )
    r=row(i); c=col(i);
    if( (r>65)&(c>65)&(r<415)&(c<575) )   
      M=G1(r-65:r+65,c-65:c+65);
      l=length(find(M > 50));
      if( (l>930) )
        G2(r,c)=G1(r,c);
      end
    end
  end
  figure; imagesc(G2); colormap(gray); title('Clean up the background')
end % if


% find each object in the image
G3=G2;
while( sum(sum(G2)) ~= 0 )
  % column
  ri=sum(G2);
  f1=find(ri ~= 0);
  f2=find(ri == 0);
  c1=f1(1)-1;
  ctmp=find(f2>c1);
  c2=f2(ctmp(1));
  M1=G2(:,c1:c2);
  % row
  RI=sum(M1');
  F1=find(RI ~= 0);
  F2=find(RI == 0);
  C1=F1(1)-1;
  CTMP=find(F2>C1);
  C2=F2(CTMP(1));
  M2=M1(C1:C2,:);

  % get rid of extra space
  rri=sum(M2);
  ff1=find(rri ~= 0);
  ff2=find(rri == 0);
  cc1=ff1(1)-1;
  cctmp=find(ff2>cc1);
  cc2=ff2(cctmp(1));
  MM1=M2(:,cc1:cc2);
  RRI=sum(MM1');
  FF1=find(RRI ~= 0);
  FF2=find(RRI == 0);
  CC1=FF1(1)-1;
  CCTMP=find(FF2>CC1); 
  CC2=FF2(CCTMP(1));
  MM2=MM1(CC1:CC2,:);

  figure; imagesc(MM2); colormap(gray); title('Break up each object')

  % check size
  sizeM2=size(MM2);
  if( (sizeM2(1)>23) & (sizeM2(1)<120) & (sizeM2(2)>23) & (sizeM2(2)<120) ) 
    s=(sum(sum(MM2)))/200;
    if( (s<2000) & (s>950) )
      % determine if the object is a rectangle

      % take a chunk to see if the object is indeed a rectangle
      b=0;
      ss=size(MM2);
      rr=round(.4*ss(1));
      cc=round(.4*ss(2));
      chunk=MM2(1:rr,1:cc);
      if( sum(sum(chunk)) < 200*10 )
	b=1; end
      chunk2=MM2(ss(1)-rr:ss(1), ss(2)-cc:ss(2));
      if( sum(sum(chunk2)) < 200*10 )
        b=1; end
      chunk3=MM2(1:rr, ss(2)-cc:ss(2));
      if( sum(sum(chunk3)) < 200*10 )
        b=1; end
      chunk4=MM2(ss(1)-rr:ss(1), 1:cc);
      if( sum(sum(chunk4)) < 200*10 )
        b=1; end

      if(b==0)
        isTank = rect_test(MM2);
        if( isTank == 1 )
          %figure; imagesc(MM2); title('MM2'); colormap(gray);
	  % get the coordinates
          r=round((2*C1+CC1+CC2)/2);
          c=round((2*c1+cc1+cc2)/2);
	  %G3(r-15:r+15,c-3:c+3)=150;
  	  %G3(r-3:r+3,c-15:c+15)=150;
          final_imag2(r-15:r+15,c-3:c+3)=0;
          final_imag2(r-3:r+3,c-15:c+15)=0;
	end % 4th if
      end % 3rd if
    end % 2nd if
  end % 1st if
  G2(C1:C2,c1:c2)=0;

end % while


%figure; imagesc(G3); title('G3'); colormap(gray);
figure; imagesc(final_imag2); title('Result'); colormap(gray);
