help-octave
[Top][All Lists]
Advanced

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

Re: How do I read an image into octave?


From: Clif Kussmaul
Subject: Re: How do I read an image into octave?
Date: Mon, 06 Dec 1999 08:20:27 -0500

Here's a modified "loadimage" that reads a variety of PPM/PGM/PBM
formats.  It's not perfect (for example, there are legal PPM comments
that will derail it), but it does seem to be able to read images
generated by xv, etc.  

Please share problems or suggestions...
Clif

Alasdair McAndrew wrote:
> I've just successfully compiled octave under linux; I'd like to use it
> to test some image processing algorithms.  But according to the info
> pages, images must be in octave's "img" format.  This is all very
> well, but how do I get a pgm/tif/gif/png image into that format?  Are
> there any file handling routines which will take, say, a 256 grey level
> pgm image and produce a matrix in octave?
> 
> In other word, is there an octave equivalent to MATLAB's "imread"
> command?

-- 
--------------------------------------------------------------------
Clif Kussmaul  mailto:address@hidden       ph:610-861-1570
 (Asst Prof)   http://www.cs.moravian.edu/~kussmaul fax:610-861-3979 
Computer Science, Moravian College, 1200 Main St, Bethlehem PA 18018
## Copyright (C) 1996 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.

## Load an image file.
##
## [img, map] = loadimage (img_file) 
## loads an image and its associated color map from file img_file, 
## which must be in octave's image format.
##
## [img, map] = loadimage (img_file, "img") 
## is the same as loadimage (img_file).
##
## [r, g, b]  = loadimage (img_file, "ppm") 
## loads an image from file img_file, 
## which must be in color ppm format (ascii or binary).
##
## [img, map]  = loadimage (img_file, "ppm") 
## loads an image from file img_file, 
## which may be in any ppm format (ascii or binary).
## If the image is in color (ppm, not pgm or pbm), 
## rgb2ind() is used to convert it to an indexed image.
##
## if no output arguments are specified, the image is displayed
##
## SEE ALSO: saveimage, load, save

## Author: Tony Richardson <address@hidden>
## Created: July 1994
## Adapted-By: jwe

## Updated Nov 1999 to include ppm,pgm.pbm image formats by 
## Clif Kussmaul <address@hidden>
## and Tricia Salomonsen <address@hidden>.

function [...] = loadimage2 (filename, img_form)
  
  ## process arguments
   
  if (nargin < 1 || nargin > 2)
    usage ("[img, map] = loadimage (filename, [format])");
  endif
  
  if (! isstr (filename))
    error ("loadimage: file name must be a string");
  endif

  if (nargin < 2)
    img_form = "img";
  endif
  
  ########################################
  ## load image in octave's image format
  
  if (strcmp (img_form, "img"))
    ## file is in octave's image format, 
    ## and is assumed to have variables img and map, or X and map.
    
    file = file_in_path (IMAGEPATH, filename);

    if (isempty (file))
       error ("loadimage: unable to find image file");
    endif

    eval (['load ', file]);

    if (exist ("img"))
      img_retval = img;
    elseif (exist ("X"))
      img_retval = X;
    else
      error ("loadimage: invalid image file found");
    endif

    if (exist ("map"))
      map_retval = map;
    else
      error ("loadimage: invalid image file found");
    endif
    
    if (nargout == 0) 
        imshow(img_retval, map_retval);
    else
        vr_val(img_retval);
        vr_val(map_retval);
    endif
    ## end of img format


  ########################################
  ## load image in PBM/PGM/PPM image format
  
  elseif (strcmp (img_form, "ppm") || 
          strcmp (img_form, "pgm") ||
          strcmp (img_form, "pbm")   )
        
    ## check magic number
    fp              = fopen(filename,"r");
    magic           = fscanf(fp,"%c",2);
    if (magic(1) != "P")
       error ("loadimage: invalid image file (bad magic number)");
    endif
    
    ## read 2 values (W,H) for pbm, 3 (W,H,M) for pgm or ppm
    if (magic(2) == "1" || magic(2) == "4")
      dneed = 2;
    else
      dneed = 3;
    endif
    while (dneed > 0)
      ## skip blank lines and comment lines
      while (1)
        data = fgetl(fp);
        if (length(data) > 0 && ! strcmp(data(1),"#")) break; endif
      endwhile
      ## try to read values
      [dvals, dcnt] = sscanf(data, "%d", dneed);
      if (dneed >= 3 && dneed - dcnt < 3)   dsize(3) = dvals(dneed - 2); endif
      if (dneed >= 2 && dneed - dcnt < 2)   dsize(2) = dvals(dneed - 1); endif
      if (dneed >= 1 && dneed - dcnt < 1)   dsize(1) = dvals(dneed - 0); endif
      dneed = dneed - dcnt;
    endwhile
    
    if     (magic(2) == "1" || magic(2) == "4")
      # read binary PBM in ascii ("1") or binary ("4") and transpose
      if (magic(2) == "1")
        data = fscanf(fp, "%d", [     dsize(2)    dsize(1)]         )';
      else
        idata = fread(fp,       [ceil(dsize(2)/8) dsize(1)], "uchar")';
        ## unpack bits from bytes
        data  = zeros(dsize(1),dsize(2));
        for i=8:-1:1
          data(:,i:8:size(data,2))  =   rem(idata,  2); 
          idata                     = floor(idata / 2);
        endfor
      endif
      
      # would like to make 2-entry colormap, but imshow crashes in saveimage.m
      data = (!data) * 255;
      map = gray(256);
      if (nargout == 0)
        imshow(data, map);
      else
        vr_val(data);
        vr_val(map);
      endif
      
    elseif (magic(2) == "2" || magic(2) == "5")
      ## read greyscale PGM in ascii ("2") or binary ("5") and transpose
      if (magic(2) == "2")
        data = fscanf(fp, "%d", [dsize(3)   dsize(2)]         )';  
      else
        data = fread( fp,       [dsize(3)   dsize(2)], "uchar")';
      endif
      
      map = gray(dsize(1));
      if (nargout == 0)
        imshow(data, map);
      else
        vr_val(data);
        vr_val(map);
      endif
      
    elseif (magic(2) == "3" || magic(2) == "6")
      ## read color PPM in ascii ("3") or binary ("6")
      if (magic(2) == "3")
        data = fscanf(fp, "%d", [dsize(3)*3 dsize(2)]         );
      else 
        data = fread( fp,       [dsize(3)*3 dsize(2)], "uchar");
      endif
        
      # extract R,G,B values between 0 and 1
      data = data / dsize(1);
      r = data(1:3:size(data,1),:)';
      g = data(2:3:size(data,1),:)';
      b = data(3:3:size(data,1),:)';

      if (nargout == 0)
        imshow(r,g,b);
      elseif (nargout < 3)
        [img_retval, map_retval] = rgb2ind(r, g, b);
        vr_val(img_retval);
        vr_val(map_retval);
      elseif (nargout == 3)
        vr_val(r);
        vr_val(g);
        vr_val(b);
      endif

    else
        error("loadimage: invalid image file (bad magic number)");
    endif
    
    fclose(fp);
    ## end of ppm/pgm/pbm format
        
  else
    error ("loadimage: image format not supported.");
  endif

endfunction

reply via email to

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