# Binary Functor Filter with VariableVector Input/Output

**URL:** https://discourse.itk.org/t/binary-functor-filter-with-variablevector-input-output/1248
**Category:** Beginner Questions
**Created:** [September 4, 2018, 1:33pm UTC](https://discourse.itk.org/t/binary-functor-filter-with-variablevector-input-output/1248 "2018-09-04T13:33:46Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![spinicist](https://discourse.itk.org/user_avatar/discourse.itk.org/spinicist/32/183_2.png) [@spinicist](https://discourse.itk.org/u/spinicist)
#### Post date: [September 4, 2018, 1:33pm UTC](https://discourse.itk.org/t/binary-functor-filter-with-variablevector-input-output/1248/1 "2018-09-04T13:33:47Z")

</div>

Hello,

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: [https://github.com/spinicist/QUIT/blob/mt/Source/MT/qi\_zspec\_interp.cpp](https://github.com/spinicist/QUIT/blob/mt/Source/MT/qi_zspec_interp.cpp)

Thanks

---

<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: [September 4, 2018, 2:11pm UTC](https://discourse.itk.org/t/binary-functor-filter-with-variablevector-input-output/1248/2 "2018-09-04T14:11:44Z")

</div>

[BinaryFunctorImageFilter](https://itk.org/Doxygen/html/classitk_1_1BinaryFunctorImageFilter.html) has this template signature: `BinaryFunctorImageFilter< TInputImage1, TInputImage2, TOutputImage, TFunction >`. That means you get to control its output type. [Line 71 in your example](https://github.com/spinicist/QUIT/blob/mt/Source/MT/qi_zspec_interp.cpp#L71) defines the output image type, which also includes the definition of its PixelType. Just change that to what you want.

---

<div class="post-metadata">

### Author: ![spinicist](https://discourse.itk.org/user_avatar/discourse.itk.org/spinicist/32/183_2.png) [@spinicist](https://discourse.itk.org/u/spinicist)
#### Post date: [September 4, 2018, 2:13pm UTC](https://discourse.itk.org/t/binary-functor-filter-with-variablevector-input-output/1248/3 "2018-09-04T14:13:51Z")

</div>

Hello,

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.

---

<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: [September 4, 2018, 2:40pm UTC](https://discourse.itk.org/t/binary-functor-filter-with-variablevector-input-output/1248/4 "2018-09-04T14:40:09Z")

</div>

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](https://github.com/InsightSoftwareConsortium/ITKExamples/blob/1d6e9d73753a02d869f3b5c44db19ade7a27d7d8/src/Core/Common/UseParallelizeImageRegion/Code.cxx#L54-L70). This [example](https://github.com/InsightSoftwareConsortium/ITKExamples/blob/1d6e9d73753a02d869f3b5c44db19ade7a27d7d8/src/Core/Common/FilterAndParallelizeImageRegion/Code.cxx#L51-L69) is similar to a unary filter, but it might be helpful too.

---

<div class="post-metadata">

### Author: ![spinicist](https://discourse.itk.org/user_avatar/discourse.itk.org/spinicist/32/183_2.png) [@spinicist](https://discourse.itk.org/u/spinicist)
#### Post date: [September 5, 2018, 7:38pm UTC](https://discourse.itk.org/t/binary-functor-filter-with-variablevector-input-output/1248/5 "2018-09-05T19:38:25Z")

</div>

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.
