Binary Functor Filter with VariableVector Input/Output


If I use a BinaryFunctorImageFilter with input 1 as a itk::VectorImage, input 2 as an itk::Image and the output as a itk::VectorImage, is there a way to control the number of components per pixel in the output image?

My algorithm takes a vector and a constant as input and produces a differently sized vector as the output. Currently itk sets the number of components per pixel of the output image to match the first input, which when the output vector is shorter than the input leaves junk the remaining components.

Code is here:


BinaryFunctorImageFilter has this template signature: BinaryFunctorImageFilter< TInputImage1, TInputImage2, TOutputImage, TFunction >. That means you get to control its output type. Line 71 in your example defines the output image type, which also includes the definition of its PixelType. Just change that to what you want.


I didn’t made myself clear. The problem is not the output pixel type, but is the number of elements/components in the output vector.

As an example, my input vector image might have 30 elements/components but the output vector image should only have 15.

I went through the itk code and the current implementation copies the input information to the output, so the output image ends up with 30 elements. Because I only return a vector of 15 elements from the functor, the last 15 elements are filled with junk.

I think I have to roll my own filter - understandably this looks like a corner case that wasn’t considered in the original design.

Have you tried filter->GetOutput()->SetVectorLength(15); before filter->Update();? And if that doesn’t work you could inherit BinaryFunctorImageFilter and only modify the relevant part.

Also, if you are using ITK 5, you could use ParallelizeImageRegion and pass your logic as a lambda function, similar to this example. This example is similar to a unary filter, but it might be helpful too.

1 Like

Thanks @dzenanz

There isn’t a SetVectorLength() method, there is SetNumberOfComponentsPerPixel(). However calling it before Update() does not help, I think because Update() calls GenerateOutputInformation() which will in turn call SetNumberOfComponentsPerPixel() to the same number as the input image.

However, using ParallelizeImageRegion as per your example worked. It doesn’t look anything like normal ITK code, but it works!I might re-write some of my other code in this style.