# How should I write a function that takes two images as inputs?

**URL:** https://discourse.itk.org/t/how-should-i-write-a-function-that-takes-two-images-as-inputs/4812
**Category:** Algorithms
**Created:** [February 18, 2022, 2:01am UTC](https://discourse.itk.org/t/how-should-i-write-a-function-that-takes-two-images-as-inputs/4812 "2022-02-18T02:01:10Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Oddone-Scatena](https://discourse.itk.org/user_avatar/discourse.itk.org/oddone-scatena/32/2651_2.png) [@Oddone-Scatena](https://discourse.itk.org/u/Oddone-Scatena)
#### Post date: [February 18, 2022, 2:01am UTC](https://discourse.itk.org/t/how-should-i-write-a-function-that-takes-two-images-as-inputs/4812/1 "2022-02-18T02:01:10Z")

</div>

Can I write a function that takes two images as inputs and calculates the min pixel this way? I am following the deepcopy example. Thanks

```cpp
template <typename TImage>
void
Min_of_Image(typename TImage::Pointer input, typename TImage::Pointer input_2, typename TImage::Pointer output)
{
  output->SetRegions(input->GetLargestPossibleRegion());
  output->Allocate();

  itk::ImageRegionConstIterator<TImage> inputIterator(input, input->GetLargestPossibleRegion());
  itk::ImageRegionConstIterator<TImage> inputIterator_2(input_2, input_2->GetLargestPossibleRegion());
  itk::ImageRegionIterator<TImage> outputIterator(output, output->GetLargestPossibleRegion());

  while (!inputIterator.IsAtEnd())
  {
    outputIterator.Set(std::min(inputIterator.Get(),inputIterator_2.Get()));
    ++inputIterator;
    ++inputIterator_2;
    ++outputIterator;
  }
}

```

---

<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: [February 18, 2022, 2:34pm UTC](https://discourse.itk.org/t/how-should-i-write-a-function-that-takes-two-images-as-inputs/4812/2 "2022-02-18T14:34:39Z")

</div>

I think this would work. But an immediate style suggestion is: instead of taking `output` as a third parameter, you could return `typename TImage::Pointer` from your function:

```cpp
template <typename TImage>
typename TImage::Pointer
Min_of_Image(typename TImage::Pointer input, typename TImage::Pointer input_2)
{
  typename TImage::Pointer output = TImage::New();
  output->CopyInformation(input);
  output->SetRegions(input->GetLargestPossibleRegion());
  output->Allocate();
  // etc;
  return output;
}

```

Finally, why not use [BinaryGeneratorImageFilter](https://itk.org/Doxygen/html/classitk_1_1BinaryGeneratorImageFilter.html)? You would only need to provide a lambda function which operates on two pixels. It will run in parallel, and can probably be streamed.
