# how to get the 0-th component for vector image

**URL:** <https://discourse.itk.org/t/how-to-get-the-0-th-component-for-vector-image/2759>\
**Category:** Beginner Questions\
**Created:** [February 24, 2020, 9:30am UTC](https://discourse.itk.org/t/how-to-get-the-0-th-component-for-vector-image/2759 "2020-02-24T09:30:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhang-qiang-github](https://discourse.itk.org/user_avatar/discourse.itk.org/zhang-qiang-github/32/1183_2.png) [@zhang-qiang-github](https://discourse.itk.org/u/zhang-qiang-github)\
**Post date:** [February 24, 2020, 9:30am UTC](https://discourse.itk.org/t/how-to-get-the-0-th-component-for-vector-image/2759/1 "2020-02-24T09:30:56Z")

</div>

Let’s I have a vector image:

```auto
using VectorPixelType = itk::Vector< float, 3>;
using DisplacementFieldType = itk::Image< VectorPixelType, 3>;
DisplacementFieldType ::Pointer displacementField = DisplacementFieldType ::New();

```

The value displacementField is a 3-dimension image, which has 3 component. Then, I want to multiply the x-component by 2, multiply the y-component by 3, and multiply the z-component by 4.

I know the image iterator is a helpful class for this function, just like:

```auto
using ImageIterator = itk::ImageRegionIterator<DisplacementFieldType>;
ImageIterator it(displacementField, displacementField->GetRequestedRegion());
while(!it.IsAtEnd())
{
    it.Set(it.Get()*2);
}

```

However, this code would multiply x-, y-, and z-component by 2. I want to seperately multiply the x-, y- and z- component by a factor. How can I do this?

---

<div class="post-metadata">

**Author:** ![dchen](https://discourse.itk.org/user_avatar/discourse.itk.org/dchen/32/34_2.png) [@dchen](https://discourse.itk.org/u/dchen)\
**Post date:** [February 24, 2020, 2:05pm UTC](https://discourse.itk.org/t/how-to-get-the-0-th-component-for-vector-image/2759/2 "2020-02-24T14:05:09Z")

</div>

You could use VectorIndexSelectionCastImageFilter to split the multicomponent image into separate images. Then you could multiply each component image by whatever factor you’d like. Finally you’d use the ComposeImageFilter to combine the separated component images back together into one vector image.

Here are the docs of the two filters:  
[https://itk.org/Doxygen/html/classitk\_1\_1VectorIndexSelectionCastImageFilter.html](https://itk.org/Doxygen/html/classitk_1_1VectorIndexSelectionCastImageFilter.html)  
[https://itk.org/Doxygen/html/classitk\_1\_1ComposeImageFilter.html](https://itk.org/Doxygen/html/classitk_1_1ComposeImageFilter.html)

A similar question came up earlier (except using SimpleITK and Python). You can see my example reply here:

> [@add alpha channel to an image in simpleitk](https://discourse.itk.org/t/add-alpha-channel-to-an-image-in-simpleitk/2665/2):
>
> You can use the VectorIndexSelectionCast function to split an RGB image into separate components. Then create an alpha image of matching dimensions and use the Compose function to combine all four channels into an RGBA image. Here’s an example rgb = sitk.Image(100,100,sitk.sitkVectorUInt8) r = sitk.VectorIndexSelectionCast(rgb, 0) g = sitk.VectorIndexSelectionCast(rgb, 1) b = sitk.VectorIndexSelectionCast(rgb, 2) a = sitk.Image(100,100,sitk.sitkUInt8) rgba = sitk.Compose(r, g, b, a)

---

<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:** [February 24, 2020, 4:31pm UTC](https://discourse.itk.org/t/how-to-get-the-0-th-component-for-vector-image/2759/3 "2020-02-24T16:31:33Z")

</div>

You should be able to modify the pixels of a vector image using subscript operator `[]`, e.g. `pixel[0]*=2; // x`. Or to modify your iterator fragment:

```auto
using ImageIterator = itk::ImageRegionIterator<DisplacementFieldType>;
ImageIterator it(displacementField, displacementField->GetRequestedRegion());
while(!it.IsAtEnd())
{
    auto p=it.Get();
    p[0]*=2;
    p[1]*=3;
    p[2]*=4;
    it.Set(p);
}

```

---

<div class="post-metadata">

**Author:** ![zhang-qiang-github](https://discourse.itk.org/user_avatar/discourse.itk.org/zhang-qiang-github/32/1183_2.png) [@zhang-qiang-github](https://discourse.itk.org/u/zhang-qiang-github)\
**Post date:** [February 24, 2020, 11:57pm UTC](https://discourse.itk.org/t/how-to-get-the-0-th-component-for-vector-image/2759/4 "2020-02-24T23:57:32Z")

</div>

Thank you very much. It is a very concise method.

In addition, I have another question. The displacementField is a displacement field from a 3D image registration. The 0-th component is x-component, 1-th component is y-component, and 2-th component is z-component? Am I right?

Or 0-th component is z-component, 1-th component is x-component, 2-th component is z-component?

---

<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:** [February 25, 2020, 2:42pm UTC](https://discourse.itk.org/t/how-to-get-the-0-th-component-for-vector-image/2759/5 "2020-02-25T14:42:15Z")

</div>

0 should be `x`.

---

<div class="post-metadata">

**Author:** ![mihail.isakov](https://discourse.itk.org/letter_avatar_proxy/v4/letter/m/c4cdca/32.png) [@mihail.isakov](https://discourse.itk.org/u/mihail.isakov)\
**Post date:** [February 25, 2020, 3:55pm UTC](https://discourse.itk.org/t/how-to-get-the-0-th-component-for-vector-image/2759/6 "2020-02-25T15:55:58Z")

</div>

no _++it_ in the _while_ loop above, probably typo

---

<div class="post-metadata">

**Author:** ![zhang-qiang-github](https://discourse.itk.org/user_avatar/discourse.itk.org/zhang-qiang-github/32/1183_2.png) [@zhang-qiang-github](https://discourse.itk.org/u/zhang-qiang-github)\
**Post date:** [February 26, 2020, 1:08am UTC](https://discourse.itk.org/t/how-to-get-the-0-th-component-for-vector-image/2759/7 "2020-02-26T01:08:41Z")

</div>

Thank you. I have added it.
