IM=imread('object8.jpg');
IM=double(IM);
IM=(IM(:,:,1)+IM(:,:,2)+IM(:,:,3))/3;
colormap(gray(256));
image(IM);
title('Imagen en escala de gris');disp('press key');pause

E=[1 2 1;
   2 4 2;
   1 2 1]/16;
E=ones(5);
size(IM)
I2=filter2(E,IM);
imshow(I2,[]);
title('Imagen suave');disp('press key');pause


 
I2=filter2(E,I2);
I2=sqrt(1+abs(I2));
size(I2)

imshow(I2,[]);title('Imagen borde');disp('press key');pause

I2=255*(I2/max(max(I2)));
I2=I2>30;
imshow(I2,[]);title('Imagen borde');disp('press key');pause

%CLOSE

title('Imagen erosionada');disp('press key');pause

%OPEN
E=ones(6);
I2=erode(I2,E);
imshow(I2,[]);title('Imagen erosionada');disp('press key');pause
E=ones(6);
I2=dilate(I2,E);
imshow(I2,[]);title('Imagen dilatada');disp('press key');pause


%BORDES
E=ones(3);
aux=erode(I2,E);
I2=double(I2)-double(aux);
imshow(I2,[]);title('Borde');disp('press key');pause

Ic=1-I2;
imshow(Ic,[]);title('Complemento');disp('press key');pause


[x y]=ginput(1);
x=round(x);y=round(y);

E=[0 1 0;1 1 1;0 1 0];
Ix=I2*0;
Ix(y,x)=1;
while 1==1
   Iold=Ix;
   Ix=dilate(Ix,E);
   Ix=Ix & Ic;
   if sum(sum(double(Ix)-double(Iold)))==0, break;end;
end;

imshow(Ix,[]);title('Ix');pause(0)
   


