BMMorphoTos.cpp 2.32 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <mln/core/algorithm/transform.hpp>
#include <mln/core/colors.hpp>
#include <mln/core/image/experimental/ndimage.hpp>

#include <mln/core/neighborhood/c4.hpp>
#include <mln/core/neighb2d.hpp>

#include <mln/io/experimental/imread.hpp>

#include <mln/morpho/experimental/tos.hpp>
#include <mln/morpho/tos/tos.hpp>


#include <benchmark/benchmark.h>

#include <fixtures/ImagePath/image_path.hpp>

class BMMorpho : public benchmark::Fixture
{
public:
  using image_t = mln::experimental::image2d<uint8_t>;
  using image_ref_t = mln::image2d<uint8_t>;

  BMMorpho()
  {
    if (!g_loaded)
    {
      const char* filename = "Aerial_view_of_Olbia.jpg";
      mln::experimental::image2d<mln::rgb8> input;
      mln::io::experimental::imread(filename, input);

      g_input = mln::transform(input, [](mln::rgb8 x) -> uint8_t { return x[0]; });
      g_loaded = true;
    }
    m_input = g_input;
    int nr = m_input.width();
    int nc = m_input.height();
    mln::resize(m_output, m_input);
    m_size = nr * nc;

    m_input.to(m_input_, false);
    m_output.to(m_output_, false);

  }


  void run(benchmark::State& st, std::function<void(const image_t& input)> callback)
  {
    for (auto _ : st)
      callback(m_input);
    st.SetBytesProcessed(int64_t(st.iterations()) * int64_t(m_size));
  }

  void run2(benchmark::State& st, std::function<void(const image_ref_t& input)> callback)
  {
    for (auto _ : st)
      callback(m_input_);
    st.SetBytesProcessed(int64_t(st.iterations()) * int64_t(m_size));
  }


protected:
  static bool                                g_loaded;
  static mln::experimental::image2d<uint8_t> g_input;
  mln::experimental::image2d<uint8_t>        m_input;
  mln::experimental::image2d<uint8_t>        m_output;
  std::size_t                                m_size;

  mln::image2d<uint8_t>               m_input_;
  mln::image2d<uint8_t>               m_output_;
};

bool                                BMMorpho::g_loaded = false;
mln::experimental::image2d<uint8_t> BMMorpho::g_input;



BENCHMARK_F(BMMorpho, ToSNew)(benchmark::State& st)
{
  auto f = [](const image_t& input) { mln::morpho::experimental::tos(input, input.domain().tl()); };
  this->run(st, f);
}

BENCHMARK_F(BMMorpho, ToSRef)(benchmark::State& st)
{
  auto f = [](const image_ref_t& input) { mln::morpho::tos(input); };
  this->run2(st, f);
}

BENCHMARK_MAIN();