function n=neighbours8(X,v);
%n=neighbours8(X,v);
%
%Extracts the neighbours at location v=(x,y) in
%the image X starting from the right and going clockwise
%using 8-connectivity.
% 
%NON-TORUS; the neighbours in the output `n' which are 
%outside of the domain are represented with -1.
%
%Mats K 010206

[Ny,Nx]=size(X);
x=v(1);
y=v(2);

if x<Nx
    n(1)=X(y,x+1);
else
    n(1)=-1;
    n(2)=-1;
    n(8)=-1;
end

if x>1
    n(5)=X(y,x-1);
else
    n(5)=-1;
    n(4)=-1;
    n(6)=-1;
end

if y<Ny
    n(3)=X(y+1,x);
    if n(1)>0
	n(2)=X(y+1,x+1);
    end
    if n(5)>0
	n(4)=X(y+1,x-1);
    end
else
    n(3)=-1;
    n(2)=-1;
    n(4)=-1;
end

if y>1
    n(7)=X(y-1,x);
    if n(1)>0
       n(8)=X(y-1,x+1);
    end
    if n(5)>0
       n(6)=X(y-1,x-1);
    end
else
    n(7)=-1;
    n(6)=-1;
    n(8)=-1;
end

