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;


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



G2=erode(G1,ones(1,20));
%figure; imagesc(G2); colormap(gray); title('G2'); grid on
G3=dilate(G2,ones(1,20));
figure; imagesc(G3); colormap(gray); title('G3'); grid on

G4=erode(G1,ones(20,1));
figure; imagesc(G4); colormap(gray); title('G4'); grid on
G5=dilate(G4,ones(20,1));
figure; imagesc(G5); colormap(gray); title('G5'); grid on

Gv=zeros(size(G3));
Gv(:,:)=G3(:,:);
Gh=zeros(size(G5));
Gh(:,:)=G5(:,:);


G6=Gh+Gv;
figure; imagesc(G6); colormap(gray); title('G6'); grid on



Gv2=zeros(size(G2));
Gv2(:,:)=G2(:,:);
Gh2=zeros(size(G4));
Gh2(:,:)=G4(:,:);

G7=Gh2+Gv2;
figure; imagesc(G7); colormap(gray); title('G7'); grid on
c=find(G7<300);
G7(c)=0;
G8=dilate(G7,ones(20,20));
figure; imagesc(G8); colormap(gray); title('G8'); 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
