octave-maintainers
[Top][All Lists]
Advanced

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

Re: usage of imread.m in octave


From: Trond Varslot
Subject: Re: usage of imread.m in octave
Date: Thu, 15 Jul 2004 21:05:26 +0200
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4.1) Gecko/20031114

I did a quick-and-dirty hack to read colour images once (attached). It worked quite nicely, but returns the red, green and blue component as separate matrixes since octave at that point did not support 3D tables. This could be changed now, though.

It requires X11 and Imlib, though. I was never big on graphics libraries, and was happy when I got something that worked:)


-Trond Varslot



Ole Jacob Hagen wrote:
Hi, Daniel.

I've tried that one (imshow(r,g,b)), but the image doesn't appear in colours, just greyscale.

So I guess a fix in imread.m is required, if colour images are impossible to visualise from Octave. Any volunteers?

Cheers,

Ole

Daniel J Sebald wrote:

From what I've seen, only gray scale and color palettes (i.e., "colormap") currently work in Octave. To show a color image, you need independent primary color components, e.g., RGB. So first, you can't use

X = imread('testimage.jpg');

because X can't have RGB. (Unless it were a 3 dimensional matrix, which I think it isn't.) So you need something like the RGB variant:

[r,g,b]= imread( fname );

Then you can do an

imshow(r,g,b);

But, currently here is what imshow() does for the RGB

elseif (nargin == 3)
[a1, a2] = rgb2ind (a1, a2, a3);
colormap (a2);
endif

i.e., converts to an indexed image. I believe in general that can only be an approximation.

Dan






// Kompiler med: mkoctfile -I/usr/X11R6/include -L/usr/X11R6/lib -lX11 -lImlib 
imageread.cc
#include <octave/oct.h>
#include <string.h>
#include <X11/Xlib.h>
#include <Imlib.h>

char *ToCString(std::string &s)
{
    char *cString = new char[s.length() + 1];
    s.copy(cString, std::string::npos); // copy s into cString
    cString[s.length()] = 0; // copy doesn't do null byte
    return cString;
}

DEFUN_DLD (imageread, args, ,"\
[r,g,b]=imageread('<filename>')\n\n\
Reads an image file, and returns the red, green and blue components\n\
in the matrices r,g and b respectively.\n\
\n\
This implementation does not behave well if the image file does not exist.\n")
{
  Display *disp;
  ImlibData *id;
  ImlibImage *im;
  int w,h;
  int i,j;//(tkv)
  int nargs=args.length();
  if(nargs<1){
    print_usage("imageread");
    return octave_value();
  }
  if (args(0).is_string()){//File name specified
    /* Connect to the default Xserver */
    disp=XOpenDisplay(NULL);
    /* Immediately afterwards Intitialise Imlib */
    id=Imlib_init(disp);
    
    /* Load the image specified as the first argument */
    std::string filnavnstr(args(0).string_value());
    char *filnavn=ToCString(filnavnstr);
    im=Imlib_load_image(id,filnavn);
    delete [] filnavn;
    /* Suck the image's original width and height out of the Image structure */
    w=im->rgb_width;h=im->rgb_height;
    
    Matrix R (h,w);
    Matrix B (h,w);
    Matrix G (h,w);
    
    for(j=0;j<h;j++)
      for(i=0;i<w;i++){ 
        R(j,i) = im->rgb_data[3*i+3*w*j+0]; //R
        G(j,i) = im->rgb_data[3*i+3*w*j+1]; //G
        B(j,i) = im->rgb_data[3*i+3*w*j+2]; //B
      }
    octave_value_list argument(R);
    argument.append(octave_value(G));
    argument.append(octave_value(B));
    return argument;
  }
  else{
    print_usage("imageread");
    return octave_value();
  }
}

reply via email to

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