# vtkImageReslice and vtkImageExtractComponents

**URL:** <https://discourse.vtk.org/t/vtkimagereslice-and-vtkimageextractcomponents/4679>\
**Category:** Development\
**Created:** [November 17, 2020, 3:47pm UTC](https://discourse.vtk.org/t/vtkimagereslice-and-vtkimageextractcomponents/4679 "2020-11-17T15:47:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mogonen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mogonen/32/2626_2.png) [@mogonen](https://discourse.vtk.org/u/mogonen)\
**Post date:** [November 17, 2020, 3:47pm UTC](https://discourse.vtk.org/t/vtkimagereslice-and-vtkimageextractcomponents/4679/1 "2020-11-17T15:47:22Z")

</div>

Hi All, I have a method in application that uses ImageReslice and ExtractComponents together to etract the slice z from the 3d image m\_imageSrc and fill an imagebuffer using the transform xform as follows:

```
    m_slicer->SetInputConnection(m_imageSrc->GetOutputPort());
    m_slicer->SetResliceTransform(xform);
    m_slicer->SetOutputOrigin(0,0,0);

    m_slicer->SetOutputExtent(0, static_cast<int>(size.x) - 1,
                                                0, static_cast<int>(size.y) - 1,
                                                 z, z);

    const int extent[6] = { 0, static_cast<int>(size.x) - 1,
                                      0, static_cast<int>(size.y) - 1,
                                      z, z};

    m_extractor->SetInputConnection(m_slicer->GetOutputPort());
    m_extractor->UpdateExtent(extent);
    m_extractor->Update();

    im->SetDimensions(static_cast<int>(size.x), static_cast<int>(size.y), 1);
    im->AllocateScalars(m_extractor->GetOutput()->GetScalarType(), 1);
    im->DeepCopy(m_extractor->GetOutput());

```

and it works properly. Yet I needed to perform the same operation for a small region defined by origin point p and newsize, from the slice (p.z+z) and modified two lines above (parameter to the SetOutputextent and updateExtent) as follows:

```
    m_slicer->SetOutputExtent(p.x+0, p.x+static_cast<int>(newsize.x) - 1,
                                                p.y+0, p.y+static_cast<int>(newsize.y) - 1,
                                                p.z+z, p.z+z);

    const int extent[6] = {p.x+0, p.x+static_cast<int>(newsize.x) - 1,
                                      p.y+0, p.y+static_cast<int>(newsize.y) - 1,
                                      p.z+z, p.z+z};

```

It returns a small region in the end, yet it is not the proper part of the image defined by p0&newsize. I also used identity transform for testing to be safe. What am I doing wrong here? Thanks!

---

<div class="post-metadata">

**Author:** ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)\
**Post date:** [November 18, 2020, 10:08pm UTC](https://discourse.vtk.org/t/vtkimagereslice-and-vtkimageextractcomponents/4679/2 "2020-11-18T22:08:45Z")

</div>

Regarding the `SetDimension()` call, please note that

```auto
im->SetDimensions(static_cast<int>(newsize.x), static_cast<int>(newsize.y), 1);

```

is just a shortcut for

```auto
im->SetExtent(0, static_cast<int>(newsize.x) - 1, static_cast<int>(newsize.y) - 1, 0, 0);

```

But, anyway, for cropping an ROI you can try using SetOutputOrigin() instead.

```auto
m_slicer->SetOutputOrigin(p.x, p.y, p.z);
m_slicer->SetOutputExtent(static_cast<int>(newsize.x) - 1, static_cast<int>(newsize.y) - 1, z, z);

```

---

<div class="post-metadata">

**Author:** ![mogonen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mogonen/32/2626_2.png) [@mogonen](https://discourse.vtk.org/u/mogonen)\
**Post date:** [November 19, 2020, 12:46am UTC](https://discourse.vtk.org/t/vtkimagereslice-and-vtkimageextractcomponents/4679/3 "2020-11-19T00:46:52Z")

</div>

Thank you so much for your reply, David. In this case, how should the extent parameter

const int extent[6] = {p.x+0, p.x+static\_cast(newsize.x) - 1,  
p.y+0, p.y+static\_cast(newsize.y) - 1,  
p.z+z, p.z+z};

that goes to the vtkImageExtractComponents’s updateExtent be modified in regards to slicer call above?

---

<div class="post-metadata">

**Author:** ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)\
**Post date:** [November 19, 2020, 1:01am UTC](https://discourse.vtk.org/t/vtkimagereslice-and-vtkimageextractcomponents/4679/4 "2020-11-19T01:01:41Z")

</div>

If used, it should be identical to the OutputExtent of vtkImageReslice.

However, it shouldn’t be necessary at all. Just call Update().
