# Using (Set/SetUse)ReferenceImage(...) for GridImageSource in ITK Python

**URL:** https://discourse.itk.org/t/using-set-setuse-referenceimage-for-gridimagesource-in-itk-python/7353
**Category:** Engineering
**Created:** [December 17, 2024, 1:01am UTC](https://discourse.itk.org/t/using-set-setuse-referenceimage-for-gridimagesource-in-itk-python/7353 "2024-12-17T01:01:41Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![constantine](https://discourse.itk.org/user_avatar/discourse.itk.org/constantine/32/3915_2.png) [@constantine](https://discourse.itk.org/u/constantine)
#### Post date: [December 17, 2024, 1:01am UTC](https://discourse.itk.org/t/using-set-setuse-referenceimage-for-gridimagesource-in-itk-python/7353/1 "2024-12-17T01:01:41Z")

</div>

Hello Everyone,

When setting a reference image on a filter or a source in ITK, the intention is that the output properties such as size, spacing, direction will be obtained from the reference image — is this correct?

Somehow, when playing with `GridImageSource` in ITK Python, the use of reference image crashes the script on calling the `Update` method. Here’s the code:

```img
img.SetRegions([1024, 1024])
img.SetSpacing([220/1024, 220/1024])
img.Allocate()
grid_source = itk.GridImageSource[img].New()
grid_source.SetUseReferenceImage(True)
grid_source.SetReferenceImage(img)
# grid_source.SetSize(img.GetLargestPossibleRegion().GetSize())
# grid_source.SetSpacing(img.GetSpacing())
grid_source.SetGridSpacing([10, 10])
grid_source.SetSigma([0.2, 0.2])
grid_source.Update()

```

The only output is this:

`Process finished with exit code -1073740940 (0xC0000374)`

While having a closer look at the API, it became apparent that despite the use of reference image, `grid_source.SetSize([...,...])` has to be called — and the things run as expected.

However, there’s also a helper method `grid_source.SetOutputParametersFromImage(img)`, which does the trick, setting spacing, direction **and size**.

_My question is this — what’s the preferred/intended use of those two methods, `SetReferenceImage` and `SetOutputParametersFromImage`, given that they are there to do the same thing, unless I’m missing something?_

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [December 17, 2024, 1:41am UTC](https://discourse.itk.org/t/using-set-setuse-referenceimage-for-gridimagesource-in-itk-python/7353/2 "2024-12-17T01:41:17Z")

</div>

> [@constantine](#):
>
> My question is this — what’s the preferred/intended use of those two methods, `SetReferenceImage` and `SetOutputParametersFromImage`, given that they are there to do the same thing, unless I’m missing something?

The key difference is that `SetReferenceImage` sets an optional pipeline input to the filter, while `SetOutputParametersFromImage` immediately set the SetOrigin, SetSize, etc. methods.

The crash is likely related to how the `img` is connected to a filter that generated it or modified since.

---

<div class="post-metadata">

### Author: ![constantine](https://discourse.itk.org/user_avatar/discourse.itk.org/constantine/32/3915_2.png) [@constantine](https://discourse.itk.org/u/constantine)
#### Post date: [December 17, 2024, 1:44am UTC](https://discourse.itk.org/t/using-set-setuse-referenceimage-for-gridimagesource-in-itk-python/7353/3 "2024-12-17T01:44:30Z")

</div>

@blowekamp — thank you very much, Bradley!

Would you be able to give (point me to) a brief use case where a pipeline would need a reference image? I’m not grasping the idea here. 🤓

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [December 17, 2024, 5:30pm UTC](https://discourse.itk.org/t/using-set-setuse-referenceimage-for-gridimagesource-in-itk-python/7353/4 "2024-12-17T17:30:12Z")

</div>

Hi @constantine ,

You have, indeed, uncovered a bug! 🥸 🔍 🐛

Please review this patch:

[BUG: GenerateImageSource sets Size from ReferenceImage by thewtex · Pull Request #5088 · InsightSoftwareConsortium/ITK · GitHub](https://github.com/InsightSoftwareConsortium/ITK/pull/5088)

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [December 17, 2024, 8:01pm UTC](https://discourse.itk.org/t/using-set-setuse-referenceimage-for-gridimagesource-in-itk-python/7353/5 "2024-12-17T20:01:48Z")

</div>

> [@constantine](#):
>
> Would you be able to give (point me to) a brief use case where a pipeline would need a reference image? I’m not grasping the idea here. 🤓

In general I would read the ITK Software Guide: [https://itk.org/ItkSoftwareGuide.pdf](https://itk.org/ItkSoftwareGuide.pdf) to get a understanding of the pipeline and the advantages of using it.

For this particular case, I can’t think of a reason to prefer the pipelined reference image over the SetOutputParametersFromImage method.

---

<div class="post-metadata">

### Author: ![constantine](https://discourse.itk.org/user_avatar/discourse.itk.org/constantine/32/3915_2.png) [@constantine](https://discourse.itk.org/u/constantine)
#### Post date: [December 18, 2024, 4:16am UTC](https://discourse.itk.org/t/using-set-setuse-referenceimage-for-gridimagesource-in-itk-python/7353/6 "2024-12-18T04:16:01Z")

</div>

Thank you, @blowekamp — Bradley.

I did read the ITK Software Guide (and, in fact contributed and edited a bit a few years back), but upon review there’s not much about the use of reference images in the pipeline, except for metadata source in some instances in Book 2; Book 1 is even less helpful with this matter.

![image](https://discourse.itk.org/uploads/default/original/2X/c/c0b11e0f90dce386a31523f8b2fcefac9fdb6c39.png)

And, that’s why I’m trying to dig a bit deeper for better insights here. 🙂

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [December 19, 2024, 2:20pm UTC](https://discourse.itk.org/t/using-set-setuse-referenceimage-for-gridimagesource-in-itk-python/7353/7 "2024-12-19T14:20:15Z")

</div>

I thought your question was more in general about pipelines and when they could be used advantageously for streaming, out of core processing, GUI driven execution or select ROI updates. For sources I can’t think how those may be useful for these filters.

As I recall both of these methods exists in the base class because there were inconsistencies between different ways of setting these parameters is different “ImageSources” so a common base class was added with both methods. The `SetReferenceImage` method is in some other filters like ResampleImageFilter.
