# Vector Image To Image of Vectors

**URL:** <https://discourse.itk.org/t/vector-image-to-image-of-vectors/4385>\
**Category:** Engineering\
**Tags:** python\
**Created:** [August 30, 2021, 9:15pm UTC](https://discourse.itk.org/t/vector-image-to-image-of-vectors/4385 "2021-08-30T21:15:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tom\_Birdsong](https://discourse.itk.org/user_avatar/discourse.itk.org/tom_birdsong/32/1894_2.png) [@Tom\_Birdsong](https://discourse.itk.org/u/Tom_Birdsong)\
**Post date:** [August 30, 2021, 9:15pm UTC](https://discourse.itk.org/t/vector-image-to-image-of-vectors/4385/1 "2021-08-30T21:15:14Z")

</div>

Hi all,

In discussion on the [ITKStrain](https://github.com/KitwareMedical/ITKStrain/issues/44) remote module we recently encountered a question of handling vector-valued image data in the ITK Python wrapping. I’ve worked with `itk.VectorImage` and `itk.Image[itk.Vector[t,d],d]` types separately, but I’m hoping someone can clarify the best way to convert between the two.

In my experience so far it seems that the Python wrapping favors importing vector-valued data from outside sources into `itk.VectorImage` objects given their ability to accommodate `itk.VariableLengthVector`s of arbitrary length, as opposed to greatly increasing the wrapping coverage for `itk.Vector`. However, wrappings for ITK filter classes typically favor `itk.Image`s with fixed-length `itk.Vector` pixel types, such as in the case of the `itk.StrainImageFilter` under discussion in the original thread. The ITK Software Guide also favors the `itk.Image` type with little discussion of `itk.VectorImage`s. The [`itk.VectorImage`](https://itk.org/Doxygen/html/classitk_1_1VectorImage.html) documentation emphasizes that the formats differ in their memory organization, so they are not directly interchangeable. As expected, attempting to cast directly between an `itk.Image` and `itk.VectorImage` fails.

The brute force solution this problem is to allocate and iteratively fill a new object, but that seems poorly optimized. Is there a recommended method for moving vector data between an `itk.Image` and `itk.VectorImage` of the same size and vector length, both in c++ and Python?

Thank you!

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [August 30, 2021, 9:21pm UTC](https://discourse.itk.org/t/vector-image-to-image-of-vectors/4385/2 "2021-08-30T21:21:46Z")

</div>

[Duplicator](https://itk.org/ITKExamples/src/Core/Common/DuplicateAnImage/Documentation.html) probably will not work for conversion. But [Paste](https://itk.org/ITKExamples/src/Filtering/ImageGrid/PasteImageIntoAnotherOne/Documentation.html) and [RegionOfInterest](https://itk.org/Doxygen/html/classitk_1_1RegionOfInterestImageFilter.html) might.

---

<div class="post-metadata">

**Author:** ![Tom\_Birdsong](https://discourse.itk.org/user_avatar/discourse.itk.org/tom_birdsong/32/1894_2.png) [@Tom\_Birdsong](https://discourse.itk.org/u/Tom_Birdsong)\
**Post date:** [August 30, 2021, 9:42pm UTC](https://discourse.itk.org/t/vector-image-to-image-of-vectors/4385/3 "2021-08-30T21:42:49Z")

</div>

[PasteImageFilter](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Filtering/ImageGrid/wrapping/itkPasteImageFilter.wrap) and [RegionOfInterestImageFilter](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Filtering/ImageGrid/wrapping/itkRegionOfInterestImageFilter.wrap) are not currently wrapped for `itk.VectorImage` to/from `itk.Image[itk.Vector]` types, we can add that wrapping if one of these would be the best route.

I noticed that [`itkImageToImageFilterA.wrap`](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Core/Common/wrapping/itkImageToImageFilterA.wrap#L41) contains a basis for `VectorImage` \<-\> `Vector` transformation, I’m not sure if there is a class already inheriting from this that could provide conversion.

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [August 30, 2021, 9:44pm UTC](https://discourse.itk.org/t/vector-image-to-image-of-vectors/4385/4 "2021-08-30T21:44:52Z")

</div>

Ah, `CastImageFilter`. It is probably better than `Paste` or `RoI` filters.

---

<div class="post-metadata">

**Author:** ![Tom\_Birdsong](https://discourse.itk.org/user_avatar/discourse.itk.org/tom_birdsong/32/1894_2.png) [@Tom\_Birdsong](https://discourse.itk.org/u/Tom_Birdsong)\
**Post date:** [August 30, 2021, 9:51pm UTC](https://discourse.itk.org/t/vector-image-to-image-of-vectors/4385/5 "2021-08-30T21:51:09Z")

</div>

Perfect, `cast_image_filter` is already wrapped and ready to go. Thanks @dzenanz.
