[ITK-users] Multiply VariableSizeMatrix by VariableLengthVector

Cyril Mory cyril.mory at creatis.insa-lyon.fr
Tue Sep 20 07:11:34 EDT 2016


OK, great, I'll do as you suggest and create a vnl_vector from the 
VariableLenghtVector's data pointer.
Thank you for your answer and the example code.

Cyril

On 2016-09-19 22:32, Francois Budin wrote:
> Hello Cyril,
> 
> The solution I provided works indeed only if you have an itk::Matrix
> and an itk::Vector, compute the product, and then convert it to
> variable size containers.
> It seems that what you want to do is the reverse.
> One solution is to work directly with vnl_vector and vnl_matrix:
> //Input data
> itk::VariableLengthVector<float> v_vec;
> itk::VariableSizeMatrix<float> v_mat;
> v_vec.SetSize(3);
> v_vec.Fill(1.7);
> v_mat.Fill(1.3);
> 
> // Converting data to vnl vector and matrix
> 
> vnl_vector<float> vnl_vec(v_vec.GetDataPointer(),v_vec.GetSize());
> vnl_matrix<float> vnl_mat=v_mat.GetVnlMatrix();
> vnl_vector<float> vnl_vec_res=vnl_mat*vnl_vec;
> 
> // Converting result back
> 
> v_vec.SetData(vnl_vec_res.data_block(),3,false);
> 
> You could also work with itk::Vector and itk::Matrix if you know the
> size is within a certain range (i.e. dimensions 2 and 3). You could
> use
> 
> templates over this range.
> 
> Hope this helps
> 
> Francois
> 
> On Mon, Sep 19, 2016 at 4:02 PM, Cyril Mory
> <cyril.mory at creatis.insa-lyon.fr> wrote:
> 
>> Thanks for this reply, but part of it is not what I was looking for.
>> In my understanding, the solution you suggested allows to multiply a
>> VariableSizeMatrix with a Vector, and then store the result into a
>> VariableLenghtVector. But my input is also a VariableLengthVector,
>> not a Vector.
>> 
>> So let me be clearer: I want to multiply a VariableSizeMatrix with a
>> VariableLengthVector. Is there a way to extract from this
>> VariableLengthVector something that can be multiplied with
>> VariableSizeMatrix.GetVnlMatrix() ? Maybe I should copy my
>> VariableLengthVector into a vnl_vector ?
>> 
>> I do not know the size of that VariableLengthVector at compile time,
>> so I cannot create an itk::Vector of the correct size.
>> Best,
>> Cyril
>> 
>> On 09/19/2016 06:12 PM, Francois Budin wrote:
>> 
>> Hello Cyril,
>> 
>> For the matrix conversion, you can use itk::Matrix::GetVnlMatrix()
>> to extract the matrix from the itk::Matrix object, and use assign
>> that vnl matrix to your itk::VariableLengthVector using
>> operator=(const vnl_matrix< T > &matrix).
>> 
>> itk::Matrix<float,3,3> mat;
>> itk::VariableSizeMatrix<float> v_mat;
>> v_mat = mat.GetVnlMatrix();
>> 
>> For the vectors, you could get access to the data from itk::Vector
>> and pass the pointer to the data to your itk::VariableLengthVector.
>> itk::Vector<float> vec;
>> itk::VariableLengthVector<float> v_vec;
>> v_vec.SetSize(3);
>> v_vec.SetData(vec.GetDataPointer(),false);
>> 
>> Be careful with the memory management for the vector as the data is
>> still managed by the itk::Vector in this example, so the data will
>> be destroyed if the vector is destroyed (if you go out of scope).
>> 
>> On Mon, Sep 19, 2016 at 11:05 AM, Cyril Mory
>> <cyril.mory at creatis.insa-lyon.fr> wrote:
>> Hi ITK users,
>> 
>> I have some working ITK code with itk::Matrix by itk::Vector
>> multiplications, and I need to change to itk::VariableSizeMatrix and
>> itk::VariableLengthVector.
>> 
>> Simply writing
>> 
>> "myResultVector = myMatrix * myInputVector"
>> 
>> works with itk::Matrix and itk::Vector objects, but not with their
>> VariableSize/Length counterparts. Is there a way to do it, other
>> than writing the matrix by vector multiplication for-loops myself ?
>> 
>> Regards,
>> 
>> Cyril
>> 
>> _____________________________________
>> Powered by www.kitware.com [1]
>> 
>> Visit other Kitware open-source projects at
>> http://www.kitware.com/opensource/opensource.html [2]
>> 
>> Kitware offers ITK Training Courses, for more information visit:
>> http://www.kitware.com/products/protraining.php [3]
>> 
>> Please keep messages on-topic and check the ITK FAQ at:
>> http://www.itk.org/Wiki/ITK_FAQ [4]
>> 
>> Follow this link to subscribe/unsubscribe:
>> http://public.kitware.com/mailman/listinfo/insight-users [5]
> 
> 
> 
> Links:
> ------
> [1] http://www.kitware.com
> [2] http://www.kitware.com/opensource/opensource.html
> [3] http://www.kitware.com/products/protraining.php
> [4] http://www.itk.org/Wiki/ITK_FAQ
> [5] http://public.kitware.com/mailman/listinfo/insight-users


More information about the Insight-users mailing list