# Crop patch out of an image

**URL:** https://discourse.itk.org/t/crop-patch-out-of-an-image/5784
**Category:** Beginner Questions
**Tags:** itk, python
**Created:** [March 6, 2023, 4:08pm UTC](https://discourse.itk.org/t/crop-patch-out-of-an-image/5784 "2023-03-06T16:08:23Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![User0](https://discourse.itk.org/letter_avatar_proxy/v4/letter/u/94ad74/32.png) [@User0](https://discourse.itk.org/u/User0)
#### Post date: [March 6, 2023, 4:08pm UTC](https://discourse.itk.org/t/crop-patch-out-of-an-image/5784/1 "2023-03-06T16:08:23Z")

</div>

I’m trying to use itk (not sitk) to crop out a patch of another image. I have two images A and B that are overlapping, but don’t share the same size, spacing or image origin.

Image A is basically a 224x224 square patch. I’d like to crop out the same patch of image B. I’ll then resample the patch from image B to also make it 224x224.

I tried to do it like that, but it seems like I have misunderstood the transformation to physical points:

```auto
def crop_xray(xray, target):
    target_region = target.GetLargestPossibleRegion()
    physical_size = target.TransformIndexToPhysicalPoint(
        target_region.GetSize()
    )
    physcial_index = target.TransformIndexToPhysicalPoint(
        target_region.GetIndex()
    )

    xray_size = xray.TransformPhysicalPointToIndex(physical_size)
    xray_index = xray.TransformPhysicalPointToIndex(physcial_index)

    # Extract the patch from image A
    xray_region = itk.ImageRegion[3](xray_index, xray_size)
    extractFilter = itk.ExtractImageFilter.New(Input=xray)
    extractFilter.SetExtractionRegion(xray_region)
    extractedPatch = extractFilter.GetOutput()
    extractedPatch.Update()
    return extractedPatch

```

---

<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: [March 6, 2023, 4:20pm UTC](https://discourse.itk.org/t/crop-patch-out-of-an-image/5784/2 "2023-03-06T16:20:02Z")

</div>

You should divide `target_region.GetSize()` by `target.GetSpacing()` to get `physical_size`. Then multiply by `xray.GetSpacing()` to get `xray_size`.

The rest of the code look OK. Why not use RegionOfInterest filter?

---

<div class="post-metadata">

### Author: ![User0](https://discourse.itk.org/letter_avatar_proxy/v4/letter/u/94ad74/32.png) [@User0](https://discourse.itk.org/u/User0)
#### Post date: [March 7, 2023, 8:34am UTC](https://discourse.itk.org/t/crop-patch-out-of-an-image/5784/3 "2023-03-07T08:34:27Z")

</div>

Thank you for the fast reply. I see why i would need to divide the size by the spacing. I just assumed that’s what all the TransformPhysical… functions do. I guess I didn’t get the concept.

The xray\_size makes sense now. The patch seems to have a size of [36.37523560968537, 36.37523560968537, 1.0] which corresponds to what I can measure in slicer. However, I still get a type error despite casting this size to int:

```auto
_itkImageRegionPython.itkImageRegion3_swiginit(self, _itkImageRegionPython.new_itkImageRegion3(*args))
                                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: Expecting a sequence of int (or long)

```

If I understood you correctly my xray\_index is correct. I converted that to int too.

RegionOfInterest sounds like a better approach. I’ve tried to use it for the last 2 hours now but it always failed in various places. I’m still a bit overwhelmed by ITK. Do I have to learn C++ to better understand the documentation? There are no examples in python for certain functions and I struggle to use them correctly.

---

<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: [March 7, 2023, 2:19pm UTC](https://discourse.itk.org/t/crop-patch-out-of-an-image/5784/4 "2023-03-07T14:19:46Z")

</div>

Does this [example](https://examples.itk.org/src/core/common/applyafilteronlytoaspecifiedregionofanimage/documentation) help with syntax?

---

<div class="post-metadata">

### Author: ![User0](https://discourse.itk.org/letter_avatar_proxy/v4/letter/u/94ad74/32.png) [@User0](https://discourse.itk.org/u/User0)
#### Post date: [March 7, 2023, 2:30pm UTC](https://discourse.itk.org/t/crop-patch-out-of-an-image/5784/5 "2023-03-07T14:30:50Z")

</div>

Actually

```auto
smallSize = itk.Size[Dimension]()
smallSize.Fill(10)

index = itk.Index[Dimension]()
index.Fill(0)

```

confused me even more. I didn’t get index in my example either. I see how my patch is 36.37 by 36.37 wide. But I didn’t get what the starting point for my patch is. In the mean time I tried to used target.GetOrigin() as a starting point to my patch which I then convert to the xray image. However, I now get the error that my patch is out of the image.

I’ll try to study your example more. Maybe it starts to make sense. But thank you anyway.

---

<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: [March 7, 2023, 2:54pm UTC](https://discourse.itk.org/t/crop-patch-out-of-an-image/5784/6 "2023-03-07T14:54:02Z")

</div>

How about:

```plaintext
patch_size = itk.Size[Dimension]()
for d in range(Dimension):
  patch_size[d] = xray_size[d]

```

and then you can use `patch_size` for region size.

---

<div class="post-metadata">

### Author: ![User0](https://discourse.itk.org/letter_avatar_proxy/v4/letter/u/94ad74/32.png) [@User0](https://discourse.itk.org/u/User0)
#### Post date: [March 10, 2023, 2:31pm UTC](https://discourse.itk.org/t/crop-patch-out-of-an-image/5784/7 "2023-03-10T14:31:28Z")

</div>

I think I found an/the issue. In some cases the patch to crop is not entirely inside the xray. My workaround will be to check that before, then apply the code as suggested by you. In the end I probably have to pad the image to 224x224 again.

Or is there a smarter work around for that?

---

<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: [March 10, 2023, 2:45pm UTC](https://discourse.itk.org/t/crop-patch-out-of-an-image/5784/8 "2023-03-10T14:45:48Z")

</div>

Why not use resample filter? It does what you want. Use your region of interest to construct the target image. And you could pass a [boundary condition](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Examples/Filtering/ResampleImageFilter8.cxx) to resample filter.

---

<div class="post-metadata">

### Author: ![User0](https://discourse.itk.org/letter_avatar_proxy/v4/letter/u/94ad74/32.png) [@User0](https://discourse.itk.org/u/User0)
#### Post date: [March 10, 2023, 4:05pm UTC](https://discourse.itk.org/t/crop-patch-out-of-an-image/5784/9 "2023-03-10T16:05:54Z")

</div>

Perfect that could help a lot. Thank you very much for your help.

---

<div class="post-metadata">

### Author: ![User0](https://discourse.itk.org/letter_avatar_proxy/v4/letter/u/94ad74/32.png) [@User0](https://discourse.itk.org/u/User0)
#### Post date: [March 13, 2023, 1:23pm UTC](https://discourse.itk.org/t/crop-patch-out-of-an-image/5784/10 "2023-03-13T13:23:55Z")

</div>

I’m sorry I still can’t get it to work. I tried to incorporate the suggestions in the last couple dates. I still keep getting the error “Requested region is (at least partially) outside the largest possible region.”

Is there anymore hints you could give me? I hoped the resample filter will get rid of this.

```auto
def crop_xray(image_B, image_A):
    # Get the starting point and size of Image A
    start_point = np.asarray(image_A.GetOrigin(), dtype=int)
    size = np.array(image_A.GetLargestPossibleRegion().GetSize())

    # Convert the starting point and size of Image A to points in Image B
    end_point = start_point + (size - 1)

    start_point_in_B = image_A.TransformIndexToPhysicalPoint(
        start_point.tolist()
    )
    end_point_in_B = image_A.TransformIndexToPhysicalPoint(end_point.tolist())

    region = itk.ImageRegion[3](
        np.array(
            np.asarray(start_point_in_B) * image_B.GetSpacing(), dtype=int
        ).tolist(),
        np.array(
            np.asarray(end_point_in_B) * image_B.GetSpacing(), dtype=int
        ).tolist(),
    )

    # Use the starting and end points in Image B to extract the corresponding region from Image B
    image_B.SetRequestedRegion(region)
    crop_filter = itk.RegionOfInterestImageFilter[image_B, image_B].New()
    crop_filter.SetInput(image_B)
    crop_filter.SetRegionOfInterest(region)
    interpolator = itk.LinearInterpolateImageFunction.New(image_B)
    resampled = itk.resample_image_filter(
        crop_filter,
        interpolator=interpolator,
        size=[224, 224, 1],
        output_spacing=image_A.GetSpacing(),
        output_origin=image_A.GetOrigin(),
    )
    return resampled

```

---

<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: [March 13, 2023, 2:38pm UTC](https://discourse.itk.org/t/crop-patch-out-of-an-image/5784/11 "2023-03-13T14:38:33Z")

</div>

I suggested bypassing region of interest filter entirely, and the parameters you are giving to RegionOfInterestImageFilter should be given to resample filter directly.

`image_B.SetRequestedRegion(region)` is the most likely cause of “Requested region is (at least partially) outside the largest possible region.”

---

<div class="post-metadata">

### Author: ![User0](https://discourse.itk.org/letter_avatar_proxy/v4/letter/u/94ad74/32.png) [@User0](https://discourse.itk.org/u/User0)
#### Post date: [March 13, 2023, 2:53pm UTC](https://discourse.itk.org/t/crop-patch-out-of-an-image/5784/12 "2023-03-13T14:53:15Z")

</div>

That was it! Works now 🙂 Thanks a lot.

---

<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: [March 13, 2023, 2:54pm UTC](https://discourse.itk.org/t/crop-patch-out-of-an-image/5784/13 "2023-03-13T14:54:54Z")

</div>

Maybe share the working code, preferably the full version, in case someone else has the same question.

---

<div class="post-metadata">

### Author: ![User0](https://discourse.itk.org/letter_avatar_proxy/v4/letter/u/94ad74/32.png) [@User0](https://discourse.itk.org/u/User0)
#### Post date: [March 14, 2023, 8:17am UTC](https://discourse.itk.org/t/crop-patch-out-of-an-image/5784/14 "2023-03-14T08:17:26Z")

</div>

Sure:

```auto
def crop_xray(image_B, image_A):
    # Get the starting point and size of Image A
    start_point = np.asarray(image_A.GetOrigin(), dtype=int)
    size = np.array(image_A.GetLargestPossibleRegion().GetSize())

    # Convert the starting point and size of Image A to points in Image B
    end_point = start_point + (size - 1)

    start_point_in_B = image_A.TransformIndexToPhysicalPoint(
        start_point.tolist()
    )
    end_point_in_B = image_A.TransformIndexToPhysicalPoint(end_point.tolist())

    region = itk.ImageRegion[3](
        np.array(
            np.asarray(start_point_in_B) * image_B.GetSpacing(), dtype=int
        ).tolist(),
        np.array(
            np.asarray(end_point_in_B) * image_B.GetSpacing(), dtype=int
        ).tolist(),
    )

    # Use the starting and end points in Image B to extract the corresponding region from Image B
    image_B.SetRequestedRegion(region)
    interpolator = itk.LinearInterpolateImageFunction.New(image_B)
    resampled = itk.resample_image_filter(
        image_B,
        interpolator=interpolator,
        size=[224, 224, 1],
        output_spacing=image_A.GetSpacing(),
        output_origin=image_A.GetOrigin(),
    )
    return resampled

```
