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

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.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?

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.

@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. :nerd_face:

Hi @constantine ,

You have, indeed, uncovered a bug! :disguised_face: :mag: :bug:

Please review this patch:

BUG: GenerateImageSource sets Size from ReferenceImage by thewtex · Pull Request #5088 · InsightSoftwareConsortium/ITK · GitHub

3 Likes

In general I would read the ITK Software Guide: 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.

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

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

1 Like

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.

1 Like