For_each

Include <mln/core/algorithm/for_each.hpp>

void for_each(InputImage in, std::UnaryFunction f)

Applies the function f to every value of in. It can be used for in-place transformation. For storing the result in another image, see mln::transform().

This is equivalent to the following code:

for (auto&& v : in.values())
    f(v);

This function has a parallel implementation, see following section for an example.

Parameters:
  • in – The input image.

  • f – The function to apply on values.

Template Parameters:

InputImage – A model of InputImage

Examples

  1. Add one to each element:

    mln::image2d<uint8_t> ima = { {1, 2, 3}, {4, 5, 6} };
    auto out = mln::for_each(ima, [](uint8_t& x) { x += 1; });
    
  2. Using parallel for_each to add one to each element:

    mln::image2d<uint8_t> ima = { {1, 2, 3}, {4, 5, 6} };
    auto out = mln::parallel::for_each(ima, [](uint8_t& x) { x += 1; });
    

Complexity

Linear in the number of pixels.