help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: image.m and scaling


From: Fredrik Lingvall
Subject: Re: image.m and scaling
Date: Sat, 04 Jun 2005 09:46:44 +0200
User-agent: Mozilla Thunderbird 1.0.2 (X11/20050523)

Brian Blais wrote:

> Hello,
>
> I was wondering what others thought of the following observation.  When
> I do an image command, or an imagesc command, the resulting image is
> increased in size (IMHO, good) and smoothed (IMHO, bad).  Usually when I
> am looking at a small matrix, I am looking at data and would like to see
> an honest representation of those data points, more similar to matlab's
> image command.  If one changes one word in image.m from:
>
> im_display = sprintf ("display -geometry %f%% \"%s\"", zoom*100,
> ppm_name);
>
> to:
>
> im_display = sprintf ("display -sample %f%% \"%s\"", zoom*100, ppm_name);
>
> using the -sample option, then you get the same increase in size, which
> is good for visibility, and the image is not smoothed.  Try it with:
>
> imagesc(rand(20));
>
> and you'll see the difference.  Is this something that others would
> like, or is the present behavior there for some reason?
>
>
>             thanks,
>
>                 Brian Blais
>
>
If you use the new image features (i.e., pm3d mode) of gnuplot 4.x you
don't have this
problem. I'll attach a version of image.m which use gnuplot 4.x instead
of ImageMagick.

Fredrik

PS. It falls back to ImageMagick if gnuplot 4 (or 3.8) in not available.
## Copyright (C) 1996, 1997 John W. Eaton
##
## This file is part of Octave.
##
## Octave is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2, or (at your option)
## any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING.  If not, write to the Free
## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
## 02111-1307, USA.

## -*- texinfo -*-
## @deftypefn {Function File} {} image (@var{x}, @var{zoom})
## @deftypefnx {Function File} {} image (@var{x}, @var{y}, @var{A}, @var{zoom})
## Display a matrix as a color image.  The elements of @var{x} are indices
## into the current colormap and should have values between 1 and the
## length of the colormap.  If @var{zoom} is omitted, the image will be
## scaled to fit within 600x350 (to a max of 4).
##
## It first tries to use @code{display} from @code{ImageMagick} then
## @code{xv} and then @code{xloadimage}.
##
## The axis values corresponding to the matrix elements are specified in
## @var{x} and @var{y}. At present they are ignored.
## @end deftypefn
##
## @seealso{imshow, imagesc, and colormap}

## Author: Tony Richardson <address@hidden>
## Created: July 1994
## Adapted-By: jwe
## Fredrik Lingvall 2005-03-08: Added gnuplot 4 stuff. 
## Fredrik Lingvall 2005-06-01: Changed to  "__gnuplot_set__" instead of "gset" 
etc.


function image (x, y, A, zoom)

if (nargin == 0)
    ## Load Bobbie Jo Richardson (Born 3/16/94)
    A = loadimage ("default.img");
    x = y = [];
    zoom = 2;
  elseif (nargin == 1)
    A = x;
    zoom = [];
    x = y = [];
  elseif (nargin == 2)
    A = x;
    zoom = y;
    x = y = [];
  elseif (nargin == 3)
    zoom = [];
  elseif (nargin > 4)
    usage ("image (matrix, zoom) or image (x, y, matrix, zoom)");
  end

  # Get version of gnuplot.
  [gnuplot_version,s] = system('gnuplot --version');
  
  # Check for gnuplot 4.x
  if (~isempty(findstr(gnuplot_version,'gnuplot 4')) | ...
        ~isempty(findstr(gnuplot_version,'gnuplot 3.8')))
      
    #
    # Use gnuplot 4 (using similar code as in mesh.m)
    #
    
    if (isempty(x) && isempty(y) && ~isempty(A))
      if (ismatrix (A))
        
        __gnuplot_set__ hidden3d;
        __gnuplot_set__ data style lines;
        __gnuplot_set__ surface;
        __gnuplot_set__ nocontour;
        __gnuplot_set__ noparametric;
        __gnuplot_set__ nologscale;
        
        __gnuplot_set__ pm3d map; # Set 2D (image) mode.
        __gnuplot_set__ pm3d corners2color c1; # Use only one corner for images.

        #__gnuplot_set__ palette gray;
        #__gnuplot_set__ palette color; # Matlab default.
        #__gnuplot_set__ palette negative; # More Matlab-like.

        __gnuplot_set__ size ratio -1;
        __gnuplot_set__ cbrange [0:64]; # Set color range as in Matlab.

        axis([0 columns(A) 0 rows(A)]);
        axis('xy');
        
        # Hack to force gnuplot to show the whole image.
        # See manual on pm3d mode on this issue.
        A = [A A(:,size(A,2))];
        At = A';
        A = [At(:,1) At]';
        A = A';        

        __gnuplot_splot__(A);
      
      else
        error ("image: argument must be a matrix");
      end
    
    elseif (~isempty(x) && ~isempty(y) && ~isempty(A))

      # Hack to force gnuplot to show the whole image (works 
      # for linearly spaced x and y).
      # See gnuplot manual on pm3d mode on this issue.
      A = [A A(:,size(A,2))];
      At = A';
      A = [At At(:,size(At,2))]';
      x = [x(:)' x(length(x))+x(length(x))-x(length(x)-1)]; 
      y = [y(:)' y(length(y))+y(length(y))-y(length(y)-1)]; 

      if (isvector (x) && isvector (y) && ismatrix (A))
        xlen = length (x);
        ylen = length (y);
        
        if (xlen == columns (A) && ylen == rows (A))
          if (rows (y) == 1)
            y = y';
          end

          len = 3 * xlen;
          zz = zeros (ylen, len);
          k = 1;
          for i = 1:3:len
            zz(:,i)   = x(k) * ones (ylen, 1);
            zz(:,i+1) = y;
            zz(:,i+2) = A(:,k);
            k++;
          end

          __gnuplot_set__ hidden3d;
          __gnuplot_set__ data style lines;
          __gnuplot_set__ surface;
          __gnuplot_set__ noparametric;
          __gnuplot_set__ nocontour;
          __gnuplot_set__ nologscale;
          __gnuplot_set__ pm3d map; # Set 2D (image) mode.
          __gnuplot_set__ pm3d corners2color c1; # Use only one corner for 
images.

          #__gnuplot_set__ palette gray;
          #__gnuplot_set__ palette color; # Matlab default.
          #__gnuplot_set__ palette negative; # More Matlab-like.

          __gnuplot_set__ size ratio -1;
          __gnuplot_set__ cbrange [0:64]; # Set color range as in Matlab.

          axis([min(x) max(x) min(y) max(y)]);
          axis('xy');
          
          __gnuplot_set__ parametric;
          __gnuplot_splot__ (zz);
          __gnuplot_set__ noparametric;

        else
          msg = "image: rows (A) must be the same as length (y) and";
          msg = sprintf ("%s\ncolumns (A) must be the same as length (x)", msg);
          error (msg);
        end
      else
        error ("image: x and y must be vectors and A must be a matrix");
      end
    else
      usage ("image (A)");
    end
    
  else 
    
    #
    # Fallback to old the methods.
    #

    if isempty(zoom)
      ## Find an integer scale factor which sets the image to
      ## approximately the size of the screen.
      zoom = min ([350/rows(A), 600/columns(A), 4]);
      if (zoom >= 1)
        zoom = floor (zoom);
      else
        zoom = 1 / ceil (1/zoom);
      end
    end
    ppm_name = tmpnam ();
    
    saveimage (ppm_name, A, "ppm");
    
    ## Start the viewer.  Try display, xv, then xloadimage.
    
    xv = sprintf ("xv -raw -expand %f \"%s\"", zoom, ppm_name);
    
    xloadimage = sprintf ("xloadimage -zoom %f \"%s\"", zoom*100, ppm_name);
    
    ## ImageMagick:
    im_display = sprintf ("display -geometry %f%% \"%s\"", zoom*100, ppm_name);
    
    rm = sprintf ("rm -f \"%s\"", ppm_name);
    
    ## Need to let the shell clean up the tmp file because we are putting
    ## the viewer in the background.
    
    system (sprintf ("( %s || %s || %s && %s ) > /dev/null 2>&1 &",
                   im_display, xv, xloadimage, rm));
  end
endfunction

reply via email to

[Prev in Thread] Current Thread [Next in Thread]