%close all
clear

%im=imread('proj2_imag/Duncan3.jpg');
im=imread('Duncan1.jpg');
%im=imread('proj2_imag/Pebble1.jpg');

%imagesc(im); colormap(gray);
tmp = dilate(im,ones(4,4));
tmp2 = erode(im,ones(4,4));
%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')



% make the lines thicker
G3=dilate(G2,ones(3,3));
figure; imagesc(G3); colormap(gray); title('G3');grid on

G4=zeros(size(G3));
[row,col]=find(G3 > 100);
for( i=1:length(row) )
  r=row(i); c=col(i);
  if( (r>12)&(c>12)&(r<468)&(c<628) )   
    M=G3(r-5:r+5,c-5:c+5);
    M2=G3(r-12:r+12,c-12:c+12);
    l=length(find(M > 100));
    l2=length(find(M2 > 100));
    if( (l>30)&(l2<200) )
      G4(r-5:r+5,c-5:c+5)=M;
    end
  end
end
figure; imagesc(G4); colormap(gray); title('G4'); grid on

% try to move those that are not lines...
G5=blkproc(G4, [20 20], 'rm_dots');
figure; imagesc(G5); colormap(gray); title('G5'); grid on
