function [c,h] = imcontour(x,y,a,n)
%IMCONTOUR Image contour.
%	IMCONTOUR(...) is the same as CONTOUR but computes contours
%	suitable for images.  The difference is a coordinate system
%	shift.  IMCONTOUR also displays the data with AXIS IJ.
%	IMCONTOUR(A,N) displays N equally spaced contours of the
%	image A.
%	IMCONTOUR(X,Y,A,N) uses the vectors X,Y to define the axes
%	for the image A.
%	IMCONTOUR(X,Y,A,V) or CONTOUR(A,V) displays LENGTH(V) contour
%	lines at the values of vector V.
%	IMCONTOUR(X,Y,A) or IMCONTOUR(A) displays 10 contour lines.
%
%	When invoked with lefthand arguments, 
%	   [C,H] = IMCONTOUR(...)
%	returns a contour matrix C with format described by CONTOURC
%	and a column vector H containing handles to line objects,
%	one handle per contour line.
%
%	See also CONTOUR, CONTOUR3, CONTOURC, CLABEL.

%	Clay M. Thompson 10-6-92
%	Copyright (c) 1992 by The MathWorks, Inc.
%	$Revision: 1.9 $  $Date: 1993/09/24 22:03:14 $

% Determine which IMAGE coordinate system is being used.
s = [version '    ']; k = find(s<46 & s>58);
if ~isempty(k), s = s(1:min(k)); end
[ver,count,msg,next] = sscanf(s,'%f',1);
if ver > 4.1,
  useNew = 1;
elseif ver < 4.1,
  useNew = 0;
else
  if s(next)>='a', useNew = 1; else useNew = 0; end
end
  
if nargin==1, % imcontour(a)
  a = x;
  x = 1:size(a,2); y = 1:size(a,1);

elseif nargin==2, % imcontour(a,n) or imcontour(a,v)
  a = x;
  n = y;
  x = 1:size(a,2); y = 1:size(a,1);
end

% Make sure x,y are vectors.
if all(size(x)==size(a)), x = x(1,:); end
if all(size(y)==size(a)), y = y(:,1); end

if ~useNew,
  dx = x(2)-x(1); dy = y(2)-y(1);
  x = x + dx/2; y = y + dy/2;
end

if nargout==0,
  if nargin==1 | nargin==3,
    contour(x,y,a)
  else
    contour(x,y,a,n)
  end
else
  if nargin==1 | nargin==3,
    [c,h] = contour(x,y,a);
  else
    [c,h] = contour(x,y,a,n);
  end
end

if ~ishold,
  axis ij
  axis image
end
