%close all
clear

im=imread('proj2_imag/Duncan2.jpg');
%im=imread('proj2_imag/Dirt4.jpg');
%im=imread('proj2_imag/Pebble1.jpg');

%figure;imagesc(im); colormap(gray);
tmp = dilate(im,ones(7,7));
tmp2 = erode(im,ones(7,7));
%figure; imagesc(tmp2); colormap(gray);

img=zeros(size(im));
img(:,:)=tmp2(:,:);



% zoom in to where the tank is
%img=img(200:300,240:340);


%%% gradient

[gx,gy]=gradient(img); 
gx=abs(gx); gy=abs(gy);

coef=find(gx > 23);
gx(coef)=gx(coef)+200;
%figure; imagesc(gx); colormap(gray); title('gx')

coeff=find(gy > 23);
gy(coeff)=gy(coeff)+200;
%figure; imagesc(gy); colormap(gray); title('gy')

g=gx+gy;
%figure; imagesc(g); colormap(gray); title('g')


% LP filter
x=ones(3,3);
G=filter2(x,g);

% normalize and threshold
m=max(max(G)); G=G./m*100; 
%figure; imagesc(G); colormap(gray); title('G')
c=find( G > 40 ); 
G2 = zeros(size(img));
G2(c)=200;
%figure; imagesc(G2); colormap(gray); title('G2')


G3=zeros(size(G2));
[row,col]=find(G2 > 50);
for( i=1:length(row) )
  r=row(i); c=col(i);
  if( (r>65)&(c>65)&(r<415)&(c<575) )   
    M=G2(r-65:r+65,c-65:c+65);
    l=length(find(M > 50));
    if( (l>360) )
      G3(r,c)=G2(r,c);
    end
  end
end
figure; imagesc(G3); colormap(gray); title('G3'); grid on
