
clear

% ask user for image 
in1 = input('Input image:\n','s');
in2 = input('Background number:\n   1. Savanna (Dirt)\n   2. Desert (Carpet)\n   3. Grid (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)

% 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)

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


% edge detection
[gx,gy]=gradient(img); 
gx=abs(gx); gy=abs(gy);
g=gx+gy;
g=abs(g);
% make it 200x200
img=zeros(200);
s=size(g);
img(1:s(1),1:s(2))=g;

