Re: [ITK-users] [ITK] vnl matrix to numpy array gives random numbers

"Lowekamp, Bradley (NIH/NLM/LHC) [C]" <blowekamp-2loH/[email protected]>
Newsgroups gmane.comp.lib.itk.user
Message-ID <[email protected]>
Hello,

Is there a method named something like “_GetArrayFromVnlMatrix”? This should perform a copy of the data, as opposed to just a “view”.

Brad


From: Samuel Gerber <[email protected]>
Date: Tuesday, August 8, 2017 at 7:57 AM
To: Nick Cullen <[email protected]>
Cc: "[email protected]" <[email protected]>
Subject: Re: [ITK] [ITK-users] vnl matrix to numpy array gives random numbers

I'd guess there is something funky going with vnl_matrix_fixed. From the docs ( https://github.com/vxl/vxl/blob/master/core/vnl/vnl_matrix_fixed.h ) it says that the data resides on the stack. From your code snippet it is not exactly clear to me who owns the data in fixed_matrix, probably image. Might it be that image gets removed before you use the array view?

On Mon, Aug 7, 2017 at 10:27 PM, Nick Cullen via Insight-users <[email protected]<mailto:[email protected]>> wrote:
Hi Friends !!

I'm trying to use itkPyVnl directly in ITK, but I must be getting something wrong with pointers... The following just gives me random numbers:

```
    typedef typename ImageType::DirectionType ImageDirectionType;
    ImageDirectionType * direction = const_cast<ImageDirectionType *>( &image->GetDirection() );

    typedef typename ImageDirectionType::InternalMatrixType DirectionInternalMatrixType;
    DirectionInternalMatrixType * fixed_matrix = const_cast<DirectionInternalMatrixType *>( &direction->GetVnlMatrix() );

    const vnl_matrix<double> vnlmat1 = fixed_matrix->as_matrix();
    vnl_matrix<double> * vnlmat2 = const_cast<vnl_matrix<double> *>( &vnlmat1 );

    typedef itk::PyVnl<double> PyVnlType;
    PyObject * mymatrix = PyVnlType::_GetArrayViewFromVnlMatrix( vnlmat2 );
```

I've tried a million different things and have problems either 1) due to the constness of ->as_matrix() and others or 2) due to "taking the address of a temporary object" with ->as_matrix() ... -__-


I know that this works:

```
    vnl_matrix<double> * vnlmat = new vnl_matrix<double>;
    vnlmat->set_size(2,3);
    vnlmat->fill(0);
    vnlmat->put(1,2,1.3);
    vnlmat->put(1,0,2);
    std::cout << vnlmat << std::endl;

    typedef itk::PyVnl<double> PyVnlType;
    PyObject * mymatrix = PyVnlType::_GetArrayViewFromVnlMatrix( vnlmat );
```

Any help above is MUCH appreciated !!!!

_____________________________________
Powered by www.kitware.com<http://www.kitware.com>

Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html

Kitware offers ITK Training Courses, for more information visit:
http://www.kitware.com/products/protraining.php

Please keep messages on-topic and check the ITK FAQ at:
http://www.itk.org/Wiki/ITK_FAQ

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/insight-users



--
Samuel Gerber
R&D Engineer
Kitware, Inc.

_____________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html

Kitware offers ITK Training Courses, for more information visit:
http://www.kitware.com/products/protraining.php

Please keep messages on-topic and check the ITK FAQ at:
http://www.itk.org/Wiki/ITK_FAQ

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/insight-users
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.