How to get properly-typed second input for BinaryGeneratorImageFilter?

I’m working with ITK 5.2.1. I created a 2-input filter that subclasses BinaryGeneratorImageFilter. It is instantiated with three separate template types: BinaryGeneratorImageFilter<TIn1, TIn2, Tout>.

Inside my filter, I wanted to get the second input. I did not find a function named “GetInput2()” available, so I tried “this->GetInput(1)” and discovered that it returns the first type, TIn1. Digging through code, I came across code that works with my testing so far:

dynamic_cast<const TIn2*>(this->ProcessObject::GetInput(1))

Is this the accepted methodology? Are there “watchouts” with this approach?

GetInput(0) is the first input, and GetInput(1) is the second one. I think that the code you suggest is the usual approach.

Taking a look at:

and

it seems that BinaryGeneratorImageFilter “calls” its inputs 1 and 2. And the developer inheriting from it should be aware of this.

I think that @blowekamp wrote that class, so he might comment about it.

For this filter the “watch outs” are that indexed input 0 and 1 could be an image or a constant. Also indexed input 0 is called input 1 and indexed input 1 is called input 2 in the member functions. However these indexed inputs don’t have a “name” per the ProcessObject named object book keeping.

These oddities with the naming is for compatibility with the BinaryFunctorImageFilter.

Since both inputs can be a constant or an image there is not a single return type that GetInput1 ( if it would be implemented) could return. You can see how that is implemented and used here:

It is important to check the results of the dynamic cast to determine if the input might not be an image and may be a constant.

Lastly, if you need to access these input images and are not just configuring a lambda or a functor the usage may be outside of the original design of the filter.

1 Like

Thanks @dzenanz and @blowekamp for the comments, they reassure me.

Truthfully, I probably am mis-using the filter slightly and should just correct my code. Basically, I am treating the second input as optional. I had originally written a filter to compute on an entire image and decided later I needed to use a binary image as a mask to constrain which pixels are involved in the computation. I am checking the second input to see if a mask is available.

Optional input are handled rather different. A good class to look at and copy some code from is the: HistogramThresholdImageFilter.

Specifically, the line:

And then in the header the Set/Get Mask methods should be relevant.

1 Like