[ITK-users] [ITK] vnl matrix to numpy array gives random numbers
Nick Cullen
nickmarch31 at yahoo.com
Tue Aug 8 09:46:11 EDT 2017
Re: Brad No unfortunately there's only GetArrayView.. the copy happens at the python level in the wrapping.
Re: Samuel, i think you're probably right.
I dont think it's a direct issue with the function though, as shown by the second example.
Here is something that 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);
typedef itk::PyVnl<double> PyVnlType;PyObject * mymatrix = PyVnlType::_GetArrayViewFromVnlMatrix( vnlmat );
Here is something that returns random initialization values (e.g. 1.0232e-307)
vnl_matrix<double> vnlmat;vnlmat.set_size(2,3);vnlmat.fill(0);vnlmat.put(1,2,1.3);vnlmat.put(1,0,2);
typedef itk::PyVnl<double> PyVnlType;PyObject * mymatrix = PyVnlType::_GetArrayViewFromVnlMatrix( vnlmat );
that's why i say it's a pointer issue . I guess i could just copy the matrix into a new one and use that . i will figure it out. thanks for the help :)
On Tuesday, August 8, 2017 8:51 AM, "Lowekamp, Bradley (NIH/NLM/LHC) [C]" <blowekamp at mail.nih.gov> wrote:
#yiv5516187396 #yiv5516187396 -- _filtered #yiv5516187396 {panose-1:2 4 5 3 5 4 6 3 2 4;} _filtered #yiv5516187396 {font-family:Calibri;panose-1:2 15 5 2 2 2 4 3 2 4;} _filtered #yiv5516187396 {panose-1:2 0 5 3 0 0 0 2 0 4;}#yiv5516187396 #yiv5516187396 p.yiv5516187396MsoNormal, #yiv5516187396 li.yiv5516187396MsoNormal, #yiv5516187396 div.yiv5516187396MsoNormal {margin:0in;margin-bottom:.0001pt;font-size:11.0pt;}#yiv5516187396 a:link, #yiv5516187396 span.yiv5516187396MsoHyperlink {color:blue;text-decoration:underline;}#yiv5516187396 a:visited, #yiv5516187396 span.yiv5516187396MsoHyperlinkFollowed {color:purple;text-decoration:underline;}#yiv5516187396 span.yiv5516187396EmailStyle17 {color:windowtext;}#yiv5516187396 span.yiv5516187396msoIns {text-decoration:underline;color:teal;}#yiv5516187396 .yiv5516187396MsoChpDefault {font-size:10.0pt;} _filtered #yiv5516187396 {margin:1.0in 1.0in 1.0in 1.0in;}#yiv5516187396 div.yiv5516187396WordSection1 {}#yiv5516187396 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> 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
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/639b9652/attachment.html>
More information about the Insight-users
mailing list