help-octave
[Top][All Lists]
Advanced

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

Re: To many questions


From: Paul Kienzle
Subject: Re: To many questions
Date: Sun, 8 Aug 2004 12:40:34 -0400


On Aug 8, 2004, at 8:14 AM, Soren Hauberg wrote:

Hi Everybody
I've been using matlab for image processing for a couple of years now. But since I'm a huge fan of the GPL I would love to switch to octave. Ofcourse I have the usual problem, "Octave dosn't have function XXX". I would love to have the morphological operations the matlab provide.

A number of these are available in octave-forge (http://octave.sf.net). If you mention specific additional functions, other image processing users may tell you what they are using instead.

Since I'm to lazy to implement these operations myself I was thinking about using the implementations from the Computer Vision library OpenCV (it's using a BSD-style license). In order to this I would however need to convert an octave matrix to an IPL image (that's the type OpenCV uses for images). So here comes the questions:

Can I (in C++) acces the array behind the octave matrix, and if so how is this array organized?

See Da Coda al Fine on the octave wiki:

        http://wiki.octave.org/wiki.pl?CodaTutorial

Given a Matrix m, you can access the data for reading using m.data(), or for updating using m.fortran_vec(). Storage is column major as in fortran.

If you need row major, then you can transpose the matrix first and transpose it again when you are done. Octave matrix operations are not in place unfortunately. I have some code to perform in-place transpose, but it is concise rather than optimal:

/* From Robin Becker <address@hidden>
 * He does not remember who is the original author.
 */
typedef unsigned int mxtype;
void mx_transpose(int n, int m, mxtype *a, mxtype *b)
{
  int size = m*n;
  if(b!=a){ /* out of place transpose */
    mxtype *bmn, *aij, *anm;
    bmn = b + size; /*b+n*m*/
    anm = a + size;
    while(b<bmn) for(aij=a++;aij<anm; aij+=n ) *b++ = *aij;
  }
  else if(n!=1 && m!=1){ /* in place transpose */
    /* PAK: changed test from size != 3 so vectors aren't transposed */
    int i,row,column,current;
    for(i=1, size -= 2;i<size;i++){
      current = i;
      do {
        /*current = row+n*column*/
        column = current/m;
        row = current%m;
        current = n*row + column;
      } while(current < i);

      if (current>i) {
        mxtype temp = a[i];
        a[i] = a[current];
        a[current] = temp;
      }
    }
  }
}

What kind of types does octave use as matrix values? Matlab supports several different types, e.g. double, uint8, uint16, and so on.

The CVS version of octave has started adding restricted types such as uint8.

If I have a standard C array, can I create an octave matrix containing the values of this array?

Short answer no. So far I've avoided it by letting octave preallocate the array using e.g. Matrix m(r,c), then passing that array to a library for initialization using m.fortran_vec().

It would be possible to extend liboctave/Array.h to allow this feature. You will need direct access to ::Array<T>::ArrayRep in Array.h. In your code would would have to be able to create and hold on to an internal representation (::Array<T>::ArrayRep) initialized with T*v, n and a reference count of 1. You would then need to have a public Array constructor which accepts a representation and increments its reference count, so that it is now two. You would need similar constructors for Array2<T>, MArray<T>, MArray2<T>, Matrix, ...

This will ensure that octave won't ever try to delete the data, and any assignments to the array will force a private copy first. You won't be allowed to modify or delete the data while the reference count is greater than 1, or the results in octave will be unpredictable.

So how do you free the private data? This is more complicated. Ideally, when octave no longer holds any pointers to the representation (i.e., when the reference count is 1 again), your own private destructor would be called. The address of this destructor would have to go into the ArrayRep class, along with some user data (e.g., the PIL or BLT_vector or numarray internal rep. or VTK class, etc. which is responsible for the data). Subclassing ArrayRep with e.g. ExternalRep and making the ArrayRep destructor virtual is an obvious way to implement this.

I'm not too keen on adding extra overhead to the array class for such a rarely used feature. Perhaps somebody else has a better suggestion for how to deal with private destructors?

- Paul



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

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