[ITK-users] Multiply VariableSizeMatrix by VariableLengthVector
Francois Budin
francois.budin at kitware.com
Mon Sep 19 16:32:52 EDT 2016
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
>>
>> 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
>>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/insight-users/attachments/20160919/840daa01/attachment.html>
More information about the Insight-users
mailing list