Accumulate

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

  1. template<class V, class BinaryFunction>
    V accumulate(InputImage in, V init, BinaryFunction op)
  2. template<class Accumulator>
    auto accumulate(InputImage in, Accumulator accu)
  3. template<class Accumulator>
    auto accumulate(InputImage in, Accumulator accu, Extractor ex)

    Computes a fold left of the values of the image over an operation. 1) Uses a pair binary function and an initial value? 2) Use an accumulator/extractor pair.

    (1) is equivalent to the following code:

    for (auto&& v : in.values())
        init = op(init, v);
    return init;
    

    (2) is equivalent to the following code:

    for (auto&& v : in.values())
        accu.take(v);
    return accu.to_result();
    

    (3) is equivalent to the following code:

    for (auto&& v : in.values())
        accu.take(v);
    return ex(accu);
    
    Parameters:
    • in – The input image.

    • op – A binary operator

    • init – The initial value to sum

    • accu – The accumulator or feature

    • ex – The value extractor from the accumulator

    Template Parameters:
    • InputImage – A model of InputImage

    • Accumulator – A model of AccumulatorLike

Examples

  1. Sum up the values of an image:

    mln::image2d<uint8_t> ima = { {1, 2, 3}, {4, 5, 6} };
    int s = mln::accumulate(ima, 0, std::plus<int>());
    
  2. Sum up the values of an image with an accumulator:

    #include <mln/accu/accumulators/sum.hpp>
    
    mln::image2d<uint8_t> ima = { {1, 2, 3}, {4, 5, 6} };
    int s = mln::accumulate(ima, mln::accu::features::sum<>());
    

Complexity

Linear in the number of pixels.