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

**URL:** https://discourse.itk.org/t/how-to-add-an-image-to-another-one-in-place-image1-image2/1090
**Category:** Engineering
**Created:** [July 4, 2018, 4:29pm UTC](https://discourse.itk.org/t/how-to-add-an-image-to-another-one-in-place-image1-image2/1090 "2018-07-04T16:29:07Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)
#### Post date: [July 4, 2018, 4:29pm UTC](https://discourse.itk.org/t/how-to-add-an-image-to-another-one-in-place-image1-image2/1090/1 "2018-07-04T16:29:07Z")

</div>

It was very nice to meet you @ [https://wbir2018.nl](https://wbir2018.nl) here in Leiden, @matt.mccormick, @dzenanz, @jhlegarreta 😀 ! 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](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?

---

<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: [July 4, 2018, 4:46pm UTC](https://discourse.itk.org/t/how-to-add-an-image-to-another-one-in-place-image1-image2/1090/2 "2018-07-04T16:46:43Z")

</div>

You can use [MultiThreaderBase::ParallelizeImageRegion](https://itk.org/ITKExamples/src/Core/Common/FilterAndParallelizeImageRegion/Documentation.html) to accomplish that with little code and good performance.

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [July 5, 2018, 4:43pm UTC](https://discourse.itk.org/t/how-to-add-an-image-to-another-one-in-place-image1-image2/1090/3 "2018-07-05T16:43:10Z")

</div>

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](https://itk.org/Doxygen/html/classitk_1_1InPlaceImageFilter.html#a579b1ce34d8ed0aca2e4620ce9056501). The description for how this operates is in the [InPlaceImageFitler’s class description](https://itk.org/Doxygen/html/classitk_1_1InPlaceImageFilter.html#a579b1ce34d8ed0aca2e4620ce9056501).

---

<div class="post-metadata">

### Author: ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)
#### Post date: [July 8, 2018, 12:25pm UTC](https://discourse.itk.org/t/how-to-add-an-image-to-another-one-in-place-image1-image2/1090/4 "2018-07-08T12:25:27Z")

</div>

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!
