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

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

N4BiasFieldCorrectionImageFilter::UpdateBiasFieldEstimate currently does at

adder = AdderType::New();
adder->SetInput1( this->m_LogBiasFieldControlPointLattice );
adder->SetInput2( phiLattice );
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?


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


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.


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!