# Multiply vector with scalar value

Hello,

i want to multiply either a vector with a single scalar (`s * {v_0,...,v_n} = {s * v_0,...,s * v_n}`) or a elementwise multiplication of two vectors (`{w_0,...,w_n} * {v_0,...,v_n} = {w_0 * v_0,..., w_n * v_n}`).
This operation shall be apply to an image of vectors.

The obvious choice would be the MultiplyImageFilter but once template it over a VectorImage the constant has to be a vector.
This example does not work because `factor` is not a vector:

``````auto multiply = itk::MultiplyImageFilter<VectorImageType>::New();
multiply->SetInput(vectorImage);
multiply->SetConstant(factor);
``````

This does not work either:

``````auto multiply = itk::MultiplyImageFilter<VectorImageType>::New();
itk::Image<itk::CovariantVector<double, 3>, 3>::PixelType factor;
factor.Fill(1);
factor = 100;
multiply->SetInput(vectorImage);
multiply->SetConstant(factor);
``````

All the output values are multiplied as a vector multiplication.

Is there a filter I have not considered because its not obvious?
Is this type of operation done by an accessor or castimage filter?

Thanks,
Gordian

I think that BinaryGeneratorImageFilter can do what you want. You will need to define a per-pixel operation via a lambda function or a functor. You can read more about it here.

1 Like

To multiply by a scalar constant the second image type needs to be a scalar. The full class template signature is:

``````itk::MultiplyImageFilter< TInputImage1, TInputImage2, TOutputImage >
``````

Try something like:

``````using ScalarImageType = itk::Image<double,ImageDimension>;
auto multiply = itk::MultiplyImageFilter<VectorImageType, ScalarImageType, VectorImageType>::New()
multiply->SetInput(vectorImage);
multiply->SetConstant2(factor);
``````
2 Likes