clear
%close all

im=imread('proj2_imag/Brick1.jpg');
upper=256;
lower=150;
figure; imagesc(im); colormap(gray)

y = im<=upper & im>=lower;
%figure; imagesc(y); colormap(gray)


SE2=ones(4,2);
ero=erode(im,SE2);
SE=ones(4,4);
dil=dilate(ero, SE);


%[i,j]=find(y==1);
%all the row and column indices on places that are white
%k=y(J(1));
%l=size(k);
%for(h=1:l(2))
%	maxcount=count=0;
%	for(q=
%	if (k(i+1)==1)
%		n=n+1;
%		i=i+1;
	

img=zeros(size(y));
img(:,:)=y(:,:);
img=img*200;

%[gx,gy]=gradient(img); 
%gx=abs(gx); gy=abs(gy);
%g=gx+gy;
%g=abs(g);
%figure; imagesc(g); colormap(gray); title('g')

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

G1=img;
figure; imagesc(G1); colormap(gray); title('G1'); grid on


s = sum(sum(G1))/200

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('G2'); grid on
