How to add an image to another one "in-place" (image1 += image2)?

It was very nice to meet you @ https://wbir2018.nl here in Leiden, @matt.mccormick, @dzenanz, @jhlegarreta :grinning: ! Now I have another technical question:

N4BiasFieldCorrectionImageFilter::UpdateBiasFieldEstimate currently does at https://github.com/Kitware/ITK/blob/v5.0a02/Modules/Filtering/BiasCorrection/include/itkN4BiasFieldCorrectionImageFilter.hxx#L611

adder = AdderType::New();
adder->SetInput1( this->m_LogBiasFieldControlPointLattice );
adder->SetInput2( phiLattice );
adder->Update();
this->m_LogBiasFieldControlPointLattice = adder->GetOutput();

I feel that there’s a possible opportunity for a performance improvement here. phiLattice could be added directly to m_LogBiasFieldControlPointLattice, in-place, instead of creating an extra output image (as it now appears to do). Do you have a suggestion how to adapt the code, in order to do so?

2 Likes

You can use MultiThreaderBase::ParallelizeImageRegion to accomplish that with little code and good performance.

2 Likes

The traditional way would be to just set the filter to run “InPlace”. The AddImageFilter is derived from the InPlaceImageFilter which has a method to enable InPlace. The description for how this operates is in the InPlaceImageFitler’s class description.

3 Likes

Thanks, @dzenanz and @blowekamp for suggesting two possible ways to improve the performance of adding one image to another. I haven’t yet had time to further investigate whether these would indeed significantly speed up N4BiasFieldCorrectionImageFilter. I hope to continue on this issue later!