watershed_hierarchy.cpp 9.89 KB
Newer Older
1
2
3
4
#include "mln/morpho/watershed_hierarchy.hpp"
#include <mln/accu/accumulators/count.hpp>
#include <mln/core/colors.hpp>
#include <mln/core/image/ndimage.hpp>
5
#include <mln/core/neighborhood/c26.hpp>
6
#include <mln/core/neighborhood/c4.hpp>
7
#include <mln/core/neighborhood/c8.hpp>
8
9
10

#include <gtest/gtest.h>

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
TEST(Morpho, HeightAttribute)
{
  mln::image2d<uint8_t> input = {
      {163, 112, 42, 121, 112},  //
      {42, 121, 1, 42, 255},     //
      {1, 112, 121, 112, 121},   //
      {112, 255, 42, 1, 42},     //
      {121, 112, 121, 112, 163}, //
  };

  std::vector<float> expected_attribute = {
      134.f, 134.f, 125.f, 70.f, 70.f, 70.f, 70.f, 70.f, 61.f, 38.f, 0.f, 29.f, 38.f, 0.f, 42.f, 0.f, 0.f,
      0.f,   42.f,  0.f,   0.f,  0.f,  70.f, 0.f,  0.f,  0.f,  0.f,  0.f, 0.f,  0.f,  0.f, 0.f,  0.f, 0.f,
      0.f,   0.f,   0.f,   0.f,  0.f,  0.f,  0.f,  0.f,  0.f,  0.f,  0.f, 0.f,  0.f,  0.f, 0.f};

  // Build BPT
  auto [tree, nm] = mln::morpho::internal::__alphatree<false>(
      input, mln::c4, [](const auto& a, const auto& b) -> float { return mln::functional::l2dist_t<>()(a, b); }, false,
      false);

  auto attribute = mln::morpho::height_attribute(tree, nm);

  ASSERT_EQ(expected_attribute.size(), attribute.size());

  for (std::size_t i = 0; i < expected_attribute.size(); ++i)
    EXPECT_EQ(expected_attribute[i], attribute[i]);
}

TEST(Morpho, DynamicAttribute)
{
  mln::image2d<uint8_t> input = {
      {163, 112, 42, 121, 112},  //
      {42, 121, 1, 42, 255},     //
      {1, 112, 121, 112, 121},   //
      {112, 255, 42, 1, 42},     //
      {121, 112, 121, 112, 163}, //
  };

  // Build BPT
  auto [tree, nm] = mln::morpho::internal::__alphatree<false>(
      input, mln::c4, [](const auto& a, const auto& b) -> float { return mln::functional::l2dist_t<>()(a, b); }, false,
      false);

  std::vector<bool> expected_extrema_attribute = {false, false, false, false, false, false, false, false, false, true,
                                                  false, true,  true,  false, true,  false, false, false, true,  false,
                                                  false, false, true,  false, false, false, false, false, false, false,
                                                  false, false, false, false, false, false, false, false, false, false,
                                                  false, false, false, false, false, false, false, false, false};

  auto extrema_attribute = mln::morpho::extrema_attribute(tree, nm);

  ASSERT_EQ(expected_extrema_attribute.size(), extrema_attribute.size());

  for (std::size_t i = 0; i < expected_extrema_attribute.size(); ++i)
    EXPECT_EQ(expected_extrema_attribute[i], extrema_attribute[i]);

  std::vector<float> expected_dynamic_attribute = {
      134.f, 134.f, 134.f, 70.f, 70.f, 70.f, 70.f, 134.f, 70.f,  38.f,  38.f,  29.f,  38.f, 29.f, 134.f, 134.f, 134.f,
      134.f, 70.f,  70.f,  70.f, 70.f, 70.f, 70.f, 0,     134.f, 134.f, 134.f, 134.f, 38.f, 38.f, 38.f,  0,     134.f,
      70.f,  70.f,  70.f,  70.f, 38.f, 0,    29.f, 29.f,  70.f,  38.f,  70.f,  70.f,  29.f, 70.f, 0};

  auto dynamic_attribute = mln::morpho::dynamic_attribute(tree, nm);

  ASSERT_EQ(expected_dynamic_attribute.size(), dynamic_attribute.size());

  for (std::size_t i = 0; i < expected_dynamic_attribute.size(); ++i)
    EXPECT_EQ(expected_dynamic_attribute[i], dynamic_attribute[i]);
}

80
81
82
83
84
85
86
87
88
89
TEST(Morpho, AreaWatershedHierarchyGray)
{
  mln::image2d<uint8_t> input = {
      {163, 112, 42, 121, 112},  //
      {42, 121, 1, 42, 255},     //
      {1, 112, 121, 112, 121},   //
      {112, 255, 42, 1, 42},     //
      {121, 112, 121, 112, 163}, //
  };

90
91
  std::vector<int> expected_parent = {0,  0,  1,  1, 0,  4, 5,  6,  7,  1, 9,  10, 2,  8, 12,
                                      14, 15, 16, 2, 14, 3, 20, 17, 18, 3, 24, 21, 22, 14};
92

93
94
  std::vector<unsigned long> expected_values = {6, 3, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
95
96

  auto [tree, _] = mln::morpho::watershed_hierarchy(
97
98
99
100
101
      input,
      [](auto tree, auto nm) -> std::vector<unsigned long> {
        return tree.compute_attribute_on_points(nm, mln::accu::features::count<>());
      },
      mln::c4, [](const auto& a, const auto& b) -> float { return mln::functional::l2dist_t<>()(a, b); });
102
103
104
105
106
107
108
109
110
111
112
113

  ASSERT_EQ(expected_parent.size(), tree.parent.size());
  ASSERT_EQ(expected_values.size(), tree.values.size());
  ASSERT_EQ(tree.parent.size(), tree.values.size());

  for (std::size_t i = 0; i < expected_parent.size(); ++i)
  {
    EXPECT_EQ(expected_parent[i], tree.parent[i]);
    EXPECT_EQ(expected_values[i], tree.values[i]);
  }
}

114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
TEST(Morpho, AreaWatershedHierarchyGrayHQ)
{
  mln::image2d<uint8_t> input = {
      {163, 112, 42, 121, 112},  //
      {42, 121, 1, 42, 255},     //
      {1, 112, 121, 112, 121},   //
      {112, 255, 42, 1, 42},     //
      {121, 112, 121, 112, 163}, //
  };

  std::vector<int> expected_parent = {0,  0,  1,  1, 0,  4, 5,  6,  7,  1, 9,  10, 2,  8, 12,
                                      14, 15, 16, 2, 14, 3, 20, 17, 18, 3, 24, 21, 22, 14};

  std::vector<unsigned long> expected_values = {6, 3, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

  auto [tree, _] = mln::morpho::watershed_hierarchy(
131
132
133
134
135
      input,
      [](auto tree, auto nm) -> std::vector<unsigned long> {
        return tree.compute_attribute_on_points(nm, mln::accu::features::count<>());
      },
      mln::c4, [](const auto& a, const auto& b) -> std::uint8_t { return mln::functional::l2dist_t<>()(a, b); });
136
137
138
139
140
141
142
143
144
145
146
147

  ASSERT_EQ(expected_parent.size(), tree.parent.size());
  ASSERT_EQ(expected_values.size(), tree.values.size());
  ASSERT_EQ(tree.parent.size(), tree.values.size());

  for (std::size_t i = 0; i < expected_parent.size(); ++i)
  {
    EXPECT_EQ(expected_parent[i], tree.parent[i]);
    EXPECT_EQ(expected_values[i], tree.values[i]);
  }
}

148
149
150
151
152
153
154
155
156
157
TEST(Morpho, AreaWatershedHierarchyRGB)
{
  mln::image2d<mln::rgb8> input = {
      {{255, 146, 0}, {31, 174, 0}, {45, 0, 255}, {255, 75, 0}, {31, 174, 0}}, //
      {{45, 0, 255}, {255, 75, 0}, {3, 0, 0}, {45, 0, 255}, {255, 255, 255}},  //
      {{3, 0, 0}, {31, 174, 0}, {255, 75, 0}, {31, 174, 0}, {255, 75, 0}},     //
      {{31, 174, 0}, {255, 255, 255}, {45, 0, 255}, {3, 0, 0}, {45, 0, 255}},  //
      {{255, 75, 0}, {31, 174, 0}, {255, 75, 0}, {31, 174, 0}, {255, 146, 0}}, //
  };

158
159
  std::vector<int> expected_parent = {0, 0,  0,  0,  3, 2, 5,  6, 3, 4, 3,  3,  7,  3,
                                      9, 12, 12, 16, 3, 1, 19, 2, 5, 1, 23, 20, 21, 26};
160

161
162
  std::vector<unsigned long> expected_values = {3, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
163
164

  auto [tree, _] = mln::morpho::watershed_hierarchy(
165
166
167
168
169
      input,
      [](auto tree, auto nm) -> std::vector<unsigned long> {
        return tree.compute_attribute_on_points(nm, mln::accu::features::count<>());
      },
      mln::c4, [](const auto& a, const auto& b) -> float { return mln::functional::l2dist_t<>()(a, b); });
170
171
172
173
174

  ASSERT_EQ(expected_parent.size(), tree.parent.size());
  ASSERT_EQ(expected_values.size(), tree.values.size());
  ASSERT_EQ(tree.parent.size(), tree.values.size());

175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
  for (std::size_t i = 0; i < expected_parent.size(); ++i)
  {
    EXPECT_EQ(expected_parent[i], tree.parent[i]);
    EXPECT_EQ(expected_values[i], tree.values[i]);
  }
}

TEST(Morpho, AreaWatershedHierarchyGrayC8)
{
  mln::image2d<std::uint8_t> input = {
      {128, 124, 150, 137, 106}, //
      {116, 128, 156, 165, 117}, //
      {117, 90, 131, 108, 151},  //
      {107, 87, 118, 109, 167},  //
      {107, 73, 125, 157, 117},  //
  };

  std::vector<int> expected_parent = {0, 0,  0, 0,  1, 3, 2,  3,  1,  4,  6, 5,  7,  8,  9,
                                      2, 11, 4, 13, 4, 5, 15, 21, 17, 19, 5, 21, 22, 17, 23};

  std::vector<unsigned long> expected_values = {7, 3, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

  auto [tree, _] = mln::morpho::watershed_hierarchy(
199
200
201
202
203
      input,
      [](auto tree, auto nm) -> std::vector<unsigned long> {
        return tree.compute_attribute_on_points(nm, mln::accu::features::count<>());
      },
      mln::c8, [](const auto& a, const auto& b) -> float { return mln::functional::l2dist_t<>()(a, b); });
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230

  ASSERT_EQ(expected_parent.size(), tree.parent.size());
  ASSERT_EQ(expected_values.size(), tree.values.size());
  ASSERT_EQ(tree.parent.size(), tree.values.size());

  for (std::size_t i = 0; i < expected_parent.size(); ++i)
  {
    EXPECT_EQ(expected_parent[i], tree.parent[i]);
    EXPECT_EQ(expected_values[i], tree.values[i]);
  }
}

TEST(Morpho, AreaWatershedHierarchy3DImage)
{
  const mln::image3d<uint8_t> input = {
      {{10, 0, 0}, {10, 0, 0}, {12, 20, 38}},   //
      {{13, 22, 16}, {15, 2, 6}, {37, 25, 12}}, //
      {{11, 18, 0}, {25, 17, 11}, {9, 0, 5}}    //
  };

  std::vector<int> expected_parent = {0, 0,  1,  0, 3, 3,  2, 2, 6, 1,  5,  8,  8,  7,  9, 9,
                                      4, 10, 12, 8, 9, 18, 9, 8, 1, 17, 25, 24, 26, 28, 27};

  std::vector<unsigned long> expected_values = {9, 3, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

  auto [tree, _] = mln::morpho::watershed_hierarchy(
231
232
233
234
235
      input,
      [](auto tree, auto nm) -> std::vector<unsigned long> {
        return tree.compute_attribute_on_points(nm, mln::accu::features::count<>());
      },
      mln::c26, [](const auto& a, const auto& b) -> float { return mln::functional::l2dist_t<>()(a, b); });
236
237
238
239
240

  ASSERT_EQ(expected_parent.size(), tree.parent.size());
  ASSERT_EQ(expected_values.size(), tree.values.size());
  ASSERT_EQ(tree.parent.size(), tree.values.size());

241
242
243
244
245
246
  for (std::size_t i = 0; i < expected_parent.size(); ++i)
  {
    EXPECT_EQ(expected_parent[i], tree.parent[i]);
    EXPECT_EQ(expected_values[i], tree.values[i]);
  }
}