# Set target region for image resampling

**URL:** <https://discourse.slicer.org/t/set-target-region-for-image-resampling/894>\
**Category:** Support\
**Tags:** registration, transforms, resample\
**Created:** [August 18, 2017, 2:03am UTC](https://discourse.slicer.org/t/set-target-region-for-image-resampling/894 "2017-08-18T02:03:26Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![jclauneuro](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jclauneuro/32/1821_2.png) [@jclauneuro](https://discourse.slicer.org/u/jclauneuro)\
**Post date:** [August 18, 2017, 2:03am UTC](https://discourse.slicer.org/t/set-target-region-for-image-resampling/894/1 "2017-08-18T02:03:26Z")

</div>

Operating system: Ubuntu 16.04 LTS, MacOS 10.12.3  
Slicer version: 4.6.2, also tested on 4.7.0 latest  
Expected behavior: preserved transformed state when file opened outside Slicer including the entire volume of interest  
Actual behavior: volume transformed but inappropriately cropped following resampling

Hi there,

This question builds on another thread I’ve found on the Slicer forum:

> [@Image with hardened transform returns to original when opened outside Slicer](https://discourse.slicer.org/t/image-with-hardened-transform-returns-to-original-when-opened-outside-slicer/431):
>
> Operating system: Windows 10 Slicer version: 4.6.2 Expected behaviour: After hardening transform, saved image volume will preserve transformed state when opened outside Slicer Actual behaviour: Image returns to original (un-transformed) orientation when opened outside Slicer Hello, I am trying to perform a simple rotation transformation to a .mha volume and then save the transformed result to be available outside of Slicer; however, when I open the saved result outside of Slicer, the image …

I have used the **ACPC Transform** module to align an image relative to the AC-PC plane in a brain volume (make it more symmetric). As a result, I have a transform matrix.

I first tried applying then hardening the transform and saving the volume (as .nii.gz format), but the transformation was not preserved (the output is identical to the input image). I followed the suggestion in the referenced thread and tried using the **Resample Image (BRAINS)** module with the following parameters:

#### Inputs:

- Image to Warp: original\_volume
- Reference Image: none

#### Outputs:

- Output Image: transformed\_volume
- Pixel Type: float

#### Warping Parameters:

- Displacement Field (deprecated): None
- Transform file: ACPC transform
- Interpolation Mode: NearestNeighbor

All other settings were by default. After clicking apply, the volume appears transformed but is inappropriately cropped. I believe this is a result of not specifying a Reference Image (help text: _“Reference image used only to define the output space. If not specified, the warping is done in the same space as the image to warp.”_). I don’t have a reference image that matches the dimensions to be expected post-transform.

Any thoughts on how to resolve this problem?

Thanks!  
jon

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [August 18, 2017, 3:06am UTC](https://discourse.slicer.org/t/set-target-region-for-image-resampling/894/2 "2017-08-18T03:06:32Z")

</div>

If the external program properly uses axis directions information in the image header then you don’t need resampling. If your program ignores axis directions and operates in voxel space then you need resampling.

To resample using an arbitrary region, you can use `Crop volume` module:

- Input volume: your transformed volume (no need to harden the transform)
- Input ROI: create a new RAS-axes-aligned region of interest by selecting `ROI` mouse mode on the toolbar, clicking in the middle of the image and a corner in the image, then adjust the size as needed.
- Click Apply

ROI place mode on the toolbar:  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/0/0/00d2c02ba3d9c1b200b853fa74d9bc95c36ddd2d.png)

---

<div class="post-metadata">

**Author:** ![jclauneuro](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jclauneuro/32/1821_2.png) [@jclauneuro](https://discourse.slicer.org/u/jclauneuro)\
**Post date:** [August 18, 2017, 4:17am UTC](https://discourse.slicer.org/t/set-target-region-for-image-resampling/894/3 "2017-08-18T04:17:02Z")

</div>

Appreciate your quick response!

I’ve attached a screenshot of my current workflow. It may be worth stepping back for a moment to better explain my objective, which is to correct a tilt in an image volume. I have used the **ACPC Transform** module, to reorient the image, and want to resample it then export as .nii format (input image was also .nii format). The goal is to have the AC-PC anatomical structures (labelled as 1 and 2 in the screenshot) aligned to be parallel to the anterior-posterior axis of the resampled volume.

I am following your suggestion to use the **Crop Volume** module. You mention ways to augment the size of the ROI box (white outline on screenshot). Is there a way to rotate/ the ROI box? I’m also wondering if there is a more automatic way to detect the optimal crop volume (looks like the magenta-coloured box in the 3D view would be the optimal ROI box so if somehow that could be fed in as the input ROI to **Crop Volume** …).

 ![Screenshot](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/6/66dbefc554ee9bd3128ada3290d471795f887c2e.jpeg)

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [August 18, 2017, 4:26am UTC](https://discourse.slicer.org/t/set-target-region-for-image-resampling/894/4 "2017-08-18T04:26:38Z")

</div>

Create a new RAS-axes-aligned region of interest by selecting ROI mouse mode on the toolbar, clicking in the middle of the image and a corner in the image, then adjust the size as needed. If you make the Crop Volume module create the ROI for you or you use Fit ROI button then the ROI will be aligned with your volume’s axes and not with RAS axes.

Of course, you can also rotate the ROI box by applying the transform to it. If you use it for ACPC alignment, then you need to invert the transform if you apply it to the ROI.

---

<div class="post-metadata">

**Author:** ![jclauneuro](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jclauneuro/32/1821_2.png) [@jclauneuro](https://discourse.slicer.org/u/jclauneuro)\
**Post date:** [August 18, 2017, 3:52pm UTC](https://discourse.slicer.org/t/set-target-region-for-image-resampling/894/5 "2017-08-18T15:52:14Z")

</div>

Thanks, this worked!

To recap, here is the workflow:

1. Import _original\_volume_ (.nii.gz) into 3D Slicer.
2. Compute the transformation using the **ACPC Transform** module.
3. Apply the transformation to the _original\_volume_ using the **Transforms** module.
4. Create a new RAS-axes aligned ROI (toolbar; see screenshot above from @lassoan) centered on the midpoint between AC and PC. Manually adjust the size of the box.
5. Use the **Crop Volume** module with the following settings, and hit Apply:
  - Input Volume: _original\_volume_
  - Input ROI: _R_ (default name for new RAS ROI)
  - Output Volume: _transformed\_volume_
  - Interpolated Cropping: On
  - Spacing scale: 1.00x
  - Isotropic spacing: On
  - Interpolator: Linear

6. Save the _transformed\_volume_ as .nii.gz

Comments: It is not immediately obvious that post-tranformation resampling would be handled by a Slicer module called **Crop Volume** (luckily there is great support here!). A nice-to-have down the road would be to have resampling as options in the **ACPC Transform** or better yet the **Transforms** module (perhaps as an option next to “hardening the transform”)? I understand that part of the problem stems from the export to .nii.gz format (which is not handling the axis information the way Slicer expects), but it is worth considering that this is one of the most common neuroimaging file formats in use these days.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [August 19, 2017, 4:30am UTC](https://discourse.slicer.org/t/set-target-region-for-image-resampling/894/6 "2017-08-19T04:30:31Z")

</div>

What software do you use for visualizing the saved nifti file? If that software does not ignore image axis directions then you don’t even need resampling.

We could add resampling to Transforms or ACPC modules, but as you can see, resampling is not trivial (has many options), so overall it’s cleaner to have it in a separate module instead of repeating it in many modules. But I agree that having automatic fitting of RAS-axis-aligned ROI would be useful and it would be also nice to consolidate all the cropping/resampling modules (there are 4 of them), and have better names. Maybe “Crop and resample volume” name would be more descriptive?

---

<div class="post-metadata">

**Author:** ![jclauneuro](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jclauneuro/32/1821_2.png) [@jclauneuro](https://discourse.slicer.org/u/jclauneuro)\
**Post date:** [August 21, 2017, 2:14pm UTC](https://discourse.slicer.org/t/set-target-region-for-image-resampling/894/7 "2017-08-21T14:14:18Z")

</div>

I use mainly fslview and ITK-snap for visualizing the saved nifti files. I’m not sure if nifti takes into account image axis directions.

I would put my vote in for a resampling option in Transforms with automatic fitting of RAS-axis aligned ROI. Consolidating the different options would also make sense. Is there a protocol I should follow for making these feature requests?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [August 22, 2017, 2:24am UTC](https://discourse.slicer.org/t/set-target-region-for-image-resampling/894/8 "2017-08-22T02:24:58Z")

</div>

Features requests can be entered at [issues.slicer.org](http://issues.slicer.org). However, this request is a typical example where we would need to choose between conflicting requirements of flexibility \<-\> ease of use of the software. Also, it is difficult to implement resampling of volume types in the core Transforms module for unknown volumes (that are defined in extensions, such as tensor volumes, 4D volumes, etc). In general, we try to keep core modules as simple as possible and not bundle multiple features, which are often used together in certain workflows, because in other workflows they are not, or they are combined with other modules.

Nowadays, we address conflicting flexibility \<-\> ease of use requirements and avoid complicated designs by implementing application-specific workflows by Python scripted modules. Very small and simple Python scripted modules can provide a streamlined user interface and internally use multiple core modules.

In your case, you could add a simple extension that would use ACPC, Transforms, and Resample modules to implement the complete processing workflow. If you think it would be too much work for you then what we can do is to add automatic ROI extension to one of the resample modules or maybe rename the Crop volume module to better reflect that it does resampling, too?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [August 22, 2017, 2:34am UTC](https://discourse.slicer.org/t/set-target-region-for-image-resampling/894/9 "2017-08-22T02:34:43Z")

</div>

> [@jclauneuro](#):
>
> I use mainly fslview and ITK-snap for visualizing the saved nifti files. I’m not sure if nifti takes into account image axis directions.

ITK-Snap correctly reads image header but it does not support editing in oblique planes, so you need to resample the input images manually.

Slicer’s recently introduced `Segment Editor` module (available in nightly builds) does not have this limitation: you can segment in arbitrarily oriented planes (if needed, image resampling is performed automatically in the background). Segment Editor has many more effects than ITK-Snap and has many unique features (allows overlapping segments, has sophisticated masking options, editing in 3D views, support of standard DICOM terminologies, etc). I would recommend to try Segment Editor and let us know if you find that compared to ITK-Snap anything is missing or you cannot figure out how to use. See [reference manual](http://slicer.readthedocs.io/en/latest/user_guide/module_segmenteditor.html) and [tutorials](https://www.slicer.org/wiki/Documentation/Nightly/Training#Segmentation_for_3D_printing).

---

<div class="post-metadata">

**Author:** ![jclauneuro](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jclauneuro/32/1821_2.png) [@jclauneuro](https://discourse.slicer.org/u/jclauneuro)\
**Post date:** [September 22, 2017, 2:43am UTC](https://discourse.slicer.org/t/set-target-region-for-image-resampling/894/10 "2017-09-22T02:43:31Z")

</div>

Sorry for the delay in responding. I did not receive an email notice.

I understand your concerns about feature modifications to core modules. However if transforming 4D or diffusion data works as expected in the Transforms module, I would imagine it would be possible to also handle resampling in the module as well (just apply the same resampling to each of the associated volumes in the N-D dataset)?

Regarding quicker fixes, one of the modules named “Resample …” should handle this functionality so either of the options you proposed would work. It’s just intuitively where any beginner-to-immediate user would look.

I’ve been meaning to give the Segment Editor a try. (please tell me there is an undo button)

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 22, 2017, 1:44pm UTC](https://discourse.slicer.org/t/set-target-region-for-image-resampling/894/11 "2017-09-22T13:44:56Z")

</div>

> [@jclauneuro](#):
>
> please tell me there is an undo button

There is.

Use the latest nightly version of Slicer.
