# Cropping an image in Jupyter notebook fails

**URL:** <https://discourse.slicer.org/t/cropping-an-image-in-jupyter-notebook-fails/9741>\
**Category:** Support\
**Created:** [January 8, 2020, 9:06pm UTC](https://discourse.slicer.org/t/cropping-an-image-in-jupyter-notebook-fails/9741 "2020-01-08T21:06:44Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![myousefi2016](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/myousefi2016/32/7602_2.png) [@myousefi2016](https://discourse.slicer.org/u/myousefi2016)\
**Post date:** [January 8, 2020, 9:06pm UTC](https://discourse.slicer.org/t/cropping-an-image-in-jupyter-notebook-fails/9741/1 "2020-01-08T21:06:44Z")

</div>

I’m trying to crop an image in Jupyter notebook by using this code:

```auto
import SimpleITK as sitk

inputVolumeFnm = "./test.nrrd"  
croppedImageFnm = "./testCropped.nrrd"

inIm = sitk.ReadImage(inputVolumeFnm)

cropper = sitk.CropImageFilter()
croppingBounds = [[117, 194, 95],[227, 351, 125]]
cropper.SetLowerBoundaryCropSize(croppingBounds[0])
cropper.SetUpperBoundaryCropSize(croppingBounds[1])
croppedImage = cropper.Execute(inIm) 

outIm = crop.Execute(inIm)
outIm.SetOrigin(inIm.GetOrigin())
outIm.SetDirection(inIm.GetDirection())
sitk.WriteImage(outIm, croppedImageFnm, True)

```

But, I’m getting this error:

```auto
Traceback (most recent call last):
  File "<string>", line 12, in <module>
  File "/users/yousefi/Downloads/Slicer-4.11.0-2020-01-07-linux-amd64/lib/Python/lib/python3.6/site-packages/SimpleITK-1.3.0rc2.dev260-py3.6-linux-x86_64.egg/SimpleITK/SimpleITK.py", line 22692, in Execute
    return _SimpleITK.CropImageFilter_Execute(self, *args)
RuntimeError: Exception thrown in SimpleITK CropImageFilter_Execute: /work/Preview/Slicer-0-build/ITK/Modules/Core/Common/src/itkPoolMultiThreader.cxx:254:
itk::ERROR: Split count is greater than number of work units!

```

I’m using the latest Slicer nightly build and using Python3. Any idea what’s the reason for that error?

---

<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:** [January 8, 2020, 9:10pm UTC](https://discourse.slicer.org/t/cropping-an-image-in-jupyter-notebook-fails/9741/2 "2020-01-08T21:10:41Z")

</div>

Are you sure the file has been loaded successfully? Have you tried to load the image by specifying full path?

It seems like an error in ITK or how ITK is used. @blowekamp @thewtex do you have any clues?

We’ll upgrade ITK and SimpleITK in Slicer to latest releases within a few days, which may fix this issue.

---

<div class="post-metadata">

**Author:** ![myousefi2016](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/myousefi2016/32/7602_2.png) [@myousefi2016](https://discourse.slicer.org/u/myousefi2016)\
**Post date:** [January 8, 2020, 9:26pm UTC](https://discourse.slicer.org/t/cropping-an-image-in-jupyter-notebook-fails/9741/3 "2020-01-08T21:26:52Z")

</div>

Yes, I believe it’s loaded successfully cause this print:

```auto
print(inIm.GetOrigin())

```

Shows me this:

```auto
(-177.32197570800784, -64.95606994628905, 1008.1570434570312)

```

One question: The crop bounds should be in extent format or range format? Extent means just integer values that corresponds to the structured points in the volumetric image but range means the 3D coordinate of points.

---

<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:** [January 8, 2020, 9:48pm UTC](https://discourse.slicer.org/t/cropping-an-image-in-jupyter-notebook-fails/9741/4 "2020-01-08T21:48:12Z")

</div>

Extents are integer values, referring to voxel indices. See more information in [this ITK discussion](https://discourse.itk.org/t/how-to-crop-a-3d-image-with-a-specified-size/715/10).

---

<div class="post-metadata">

**Author:** ![blowekamp](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/blowekamp/32/1386_2.png) [@blowekamp](https://discourse.slicer.org/u/blowekamp)\
**Post date:** [January 9, 2020, 8:56pm UTC](https://discourse.slicer.org/t/cropping-an-image-in-jupyter-notebook-fails/9741/5 "2020-01-09T20:56:59Z")

</div>

Hello,

The usage looks correct, and it certainly should produce that error.

This seems likely that it is a problem inside ITK. I’m note sure what version of ITK or SimpleITK is being used here.

You have a nice stand alone test case. Can you please try with two different stand alone versions of SimpleITK

- v1.2.4 - This is the versions that should be pip install able.
- [latest nightly pre-release binary](https://github.com/SimpleITK/SimpleITK/releases) - This is build against ITKv5.1rc1

Please report back how these versions of SimpleITK/ITK work for this test case.

---

<div class="post-metadata">

**Author:** ![myousefi2016](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/myousefi2016/32/7602_2.png) [@myousefi2016](https://discourse.slicer.org/u/myousefi2016)\
**Post date:** [January 9, 2020, 9:12pm UTC](https://discourse.slicer.org/t/cropping-an-image-in-jupyter-notebook-fails/9741/6 "2020-01-09T21:12:27Z")

</div>

Hi @blowekamp, I just found the solution. There was two problems in my code. First I executed the crop filter twice and also I didn’t know that instead of giving the upper bound of cropped image, I need to have the upper bound of original image - upper bound of cropped image. Thank you!

---

<div class="post-metadata">

**Author:** ![blowekamp](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/blowekamp/32/1386_2.png) [@blowekamp](https://discourse.slicer.org/u/blowekamp)\
**Post date:** [January 9, 2020, 9:16pm UTC](https://discourse.slicer.org/t/cropping-an-image-in-jupyter-notebook-fails/9741/7 "2020-01-09T21:16:36Z")

</div>

What is the size in pixels of “test.nrrd”?

---

<div class="post-metadata">

**Author:** ![myousefi2016](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/myousefi2016/32/7602_2.png) [@myousefi2016](https://discourse.slicer.org/u/myousefi2016)\
**Post date:** [January 9, 2020, 9:31pm UTC](https://discourse.slicer.org/t/cropping-an-image-in-jupyter-notebook-fails/9741/8 "2020-01-09T21:31:55Z")

</div>

`test.nrrd` size is (512,512,144) or in terms of extent:

```auto
X extent: [0,511] (delta: 512)
Y extent: [0,511] (delta: 512)
Z extent: [0,143] (delta: 144)

```

and I wanted to crop this region based on its actual lower and upper bounds:

```auto
Lower bounds: [117,194,95]
Upper bounds: [227,351,125]

```

But I realized that crop filter take this as lower and upper bounds:

```auto
Lower bounds: [117,194,95]
Upper bounds: [511-227,511-351,143-125] = [284, 160, 18]

```

So, it just solved my problem and I don’t get that error anymore with same code.

---

<div class="post-metadata">

**Author:** ![blowekamp](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/blowekamp/32/1386_2.png) [@blowekamp](https://discourse.slicer.org/u/blowekamp)\
**Post date:** [January 9, 2020, 9:52pm UTC](https://discourse.slicer.org/t/cropping-an-image-in-jupyter-notebook-fails/9741/9 "2020-01-09T21:52:31Z")

</div>

I ran the following locally with the “latest” binary:

```auto
In [1]: import SimpleITK as sitk                                                                                                                        

In [2]: inIm = sitk.Image( (512,512,144), sitk.sitkUInt8)                                                                                               

In [3]:  
   ...: cropper = sitk.CropImageFilter() 
   ...: croppingBounds = [[117, 194, 95],[227, 351, 125]] 
   ...: cropper.SetLowerBoundaryCropSize(croppingBounds[0]) 
   ...: cropper.SetUpperBoundaryCropSize(croppingBounds[1]) 
   ...: croppedImage = cropper.Execute(inIm)  
   ...:                                                                                                                                                 

In [4]: print(croppedImage)                                                                                                                             
Image (0x7fbed5538090)
  RTTI typeinfo: itk::Image<unsigned char, 3u>
  Reference Count: 1
  Modified Time: 813
  Debug: Off
  Object Name: 
  Observers: 
    none
  Source: (none)
  Source output name: (none)
  Release Data: Off
  Data Released: False
  Global Release Data: Off
  PipelineMTime: 799
  UpdateMTime: 809
  RealTimeStamp: 0 seconds 
  LargestPossibleRegion: 
    Dimension: 3
    Index: [0, 0, 0]
    Size: [168, 18446744073709551583, 18446744073709551540]
  BufferedRegion: 
    Dimension: 3
    Index: [0, 0, 0]
    Size: [168, 18446744073709551583, 18446744073709551540]
  RequestedRegion: 
    Dimension: 3
    Index: [0, 0, 0]
    Size: [168, 18446744073709551583, 18446744073709551540]

```

It looks like there are some signed integer and/or underflow issue going on here. SimpleITK/ITK need to be more robust with the improper parameters.

---

<div class="post-metadata">

**Author:** ![blowekamp](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/blowekamp/32/1386_2.png) [@blowekamp](https://discourse.slicer.org/u/blowekamp)\
**Post date:** [January 9, 2020, 10:20pm UTC](https://discourse.slicer.org/t/cropping-an-image-in-jupyter-notebook-fails/9741/10 "2020-01-09T22:20:27Z")

</div>

Here is a pull request to fix the issue in ITK:

> <https://github.com/InsightSoftwareConsortium/ITK/pull/1551>
