[ITK-users] [ITK] vnl matrix to numpy array gives random numbers
Lowekamp, Bradley (NIH/NLM/LHC) [C]
blowekamp at mail.nih.gov
Tue Aug 8 08:51:35 EDT 2017
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 <samuel.gerber at kitware.com>
Date: Tuesday, August 8, 2017 at 7:57 AM
To: Nick Cullen <nickmarch31 at yahoo.com>
Cc: "insight-users at itk.org" <insight-users at itk.org>
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 <insight-users at itk.org<mailto:insight-users at itk.org>> 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/insight-users/attachments/20170808/cea47e13/attachment.html>
More information about the Insight-users
mailing list