# Trim background in slicer

**URL:** <https://discourse.slicer.org/t/trim-background-in-slicer/1119>\
**Category:** Feature requests\
**Created:** [September 26, 2017, 3:27am UTC](https://discourse.slicer.org/t/trim-background-in-slicer/1119 "2017-09-26T03:27:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [September 26, 2017, 3:27am UTC](https://discourse.slicer.org/t/trim-background-in-slicer/1119/1 "2017-09-26T03:27:22Z")

</div>

Hi  
convert3d (c3d) tool has this nice feature called trim, which finds the smallest rectangular region of non-background voxels. I use quite a lot with masked images. In Slicer, Crop Volume gives a similar functionality, but in a more manual/visual kind of way. Given c3d is based on ITK, I thought maybe Slicer has a similar option too, but couldn’t find it in Simple Filters. Is this feature available?

---

<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 26, 2017, 4:13am UTC](https://discourse.slicer.org/t/trim-background-in-slicer/1119/2 "2017-09-26T04:13:10Z")

</div>

It is really easy to do this, but I guess it hasn’t come up as a feature request, as it is quite rare that images contain large irrelevant background regions that could be removed by simple thresholding. What kind of images do you have?

Anyway, it’s really simple to do, just copy-paste this into the Python console (image is cropped to region with voxel intensity \> 0):

```
volumeNode = getNode('MRHead')
import vtkSegmentationCorePython as seg
img = slicer.modules.segmentations.logic().CreateOrientedImageDataFromVolumeNode(volumeNode)
img.UnRegister(None)
extent=[0,0,0,0,0,0]
seg.vtkOrientedImageDataResample.CalculateEffectiveExtent(img, extent)
croppedImg = seg.vtkOrientedImageData()
seg.vtkOrientedImageDataResample.CopyImage(img, croppedImg, extent) 
slicer.modules.segmentations.logic().CopyOrientedImageDataToVolumeNode(croppedImg, volumeNode)

```

If you need to set a custom threshold value for cropping then download Slicer nightly build on Wednesday or later and use this code:

```
volumeNode = getNode('MRHead')
threshold = 20
import vtkSegmentationCorePython as seg
img = slicer.modules.segmentations.logic().CreateOrientedImageDataFromVolumeNode(volumeNode)
img.UnRegister(None)
extent=[0,0,0,0,0,0]
seg.vtkOrientedImageDataResample.CalculateEffectiveExtent(img, extent, threshold)
croppedImg = seg.vtkOrientedImageData()
seg.vtkOrientedImageDataResample.CopyImage(img, croppedImg, extent) 
slicer.modules.segmentations.logic().CopyOrientedImageDataToVolumeNode(croppedImg, volumeNode)
```

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [September 26, 2017, 6:37pm UTC](https://discourse.slicer.org/t/trim-background-in-slicer/1119/3 "2017-09-26T18:37:03Z")

</div>

Hi Andras,

It worked great, thank you! Once trimmed, may I ask how to pad images as well? Perhaps, add a buffer of few voxels on each side.

I use this on head microCT of mice. I segment all non-cranial elements (there would be limbs, shoulder blade, mandible etc) and usually left with a volume with 50% (if not more) background voxels.

---

<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 26, 2017, 6:43pm UTC](https://discourse.slicer.org/t/trim-background-in-slicer/1119/4 "2017-09-26T18:43:32Z")

</div>

To pad images, adjust the extent after calling `CalculateEffectiveExtent`:

```
volumeNode = getNode('MRHead')
threshold = 20
margin=5
import vtkSegmentationCorePython as seg
img = slicer.modules.segmentations.logic().CreateOrientedImageDataFromVolumeNode(volumeNode)
img.UnRegister(None)
extent=[0,0,0,0,0,0]
seg.vtkOrientedImageDataResample.CalculateEffectiveExtent(img, extent, threshold)
extent=[extent[0]-margin,extent[1]+margin,extent[2]-margin,extent[3]+margin,extent[4]-margin,extent[5]+margin]
croppedImg = seg.vtkOrientedImageData()
seg.vtkOrientedImageDataResample.CopyImage(img, croppedImg, extent) 
slicer.modules.segmentations.logic().CopyOrientedImageDataToVolumeNode(croppedImg, volumeNode)
```

---

<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:** [March 21, 2023, 2:50am UTC](https://discourse.slicer.org/t/trim-background-in-slicer/1119/5 "2023-03-21T02:50:13Z")

</div>

This feature is available in “Split volume” effect (provided by SegmentEditorExtraEffects extension).

---

<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:** [March 21, 2023, 2:50am UTC](https://discourse.slicer.org/t/trim-background-in-slicer/1119/6 "2023-03-21T02:50:18Z")

</div>


