watershed_hierarchy.cpp 10.7 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
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>(
28
      input, mln::c4, [](const auto& a, const auto& b) -> float { return mln::functional::l2dist(a, b); }, false,
29
30
      false);

31
  auto attribute = mln::morpho::internal::height_attribute(tree, nm);
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

  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>(
51
      input, mln::c4, [](const auto& a, const auto& b) -> float { return mln::functional::l2dist(a, b); }, false,
52
53
54
55
56
57
58
59
      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};

60
  auto extrema_attribute = mln::morpho::internal::extrema_attribute(tree, nm);
61
62
63
64
65
66
67
68
69
70
71

  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};

72
  auto dynamic_attribute = mln::morpho::internal::dynamic_attribute(tree, nm);
73
74
75
76
77
78
79

  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, 1, 2, 2, 3, 3};
  std::vector<unsigned long> expected_values = {6, 3, 2, 2, 0, 0, 0, 0, 0, 0};
92
93

  auto [tree, _] = mln::morpho::watershed_hierarchy(
94
      input,
95
      [](auto tree, auto nm) -> std::vector<size_t> {
96
97
        return tree.compute_attribute_on_points(nm, mln::accu::features::count<>());
      },
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
      mln::c4, [](const auto& a, const auto& b) -> float { return mln::functional::l2dist(a, b); });

  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, DynamicWatershedHierarchyGray)
{
  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}, //
  };

121
122
  std::vector<int>           expected_parent = {0, 0, 1, 0, 1, 2, 1, 2, 0};
  std::vector<unsigned long> expected_values = {70, 38, 29, 0, 0, 0, 0, 0, 0};
123
124

  auto [tree, _] = mln::morpho::watershed_hierarchy(
125
126
      input, mln::morpho::WatershedAttribute::DYNAMIC, mln::c4,
      [](const auto& a, const auto& b) -> float { return mln::functional::l2dist(a, b); });
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148

  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, HeightWatershedHierarchyGray)
{
  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}, //
  };

149
150
  std::vector<int>           expected_parent = {0, 0, 1, 0, 1, 2, 1, 2, 0};
  std::vector<unsigned long> expected_values = {70, 38, 29, 0, 0, 0, 0, 0, 0};
151
152

  auto [tree, _] = mln::morpho::watershed_hierarchy(
153
154
      input, mln::morpho::WatershedAttribute::HEIGHT, mln::c4,
      [](const auto& a, const auto& b) -> float { return mln::functional::l2dist(a, b); });
155
156
157
158
159
160
161
162
163
164
165
166

  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]);
  }
}

167
168
169
170
171
172
173
174
175
176
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}, //
  };

177
178
  std::vector<int>           expected_parent = {0, 0, 1, 1, 0, 1, 2, 2, 3, 3};
  std::vector<unsigned long> expected_values = {6, 3, 2, 2, 0, 0, 0, 0, 0, 0};
179
180

  auto [tree, _] = mln::morpho::watershed_hierarchy(
181
      input,
182
      [](auto tree, auto nm) -> std::vector<size_t> {
183
184
        return tree.compute_attribute_on_points(nm, mln::accu::features::count<>());
      },
185
      mln::c4, [](const auto& a, const auto& b) -> std::uint8_t { return mln::functional::l2dist(a, b); });
186
187
188
189
190
191
192
193
194
195
196
197

  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]);
  }
}

198
199
200
201
202
203
204
205
206
207
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}}, //
  };

208
209
  std::vector<int>           expected_parent = {0, 0, 0, 0, 2, 1, 2, 1};
  std::vector<unsigned long> expected_values = {3, 2, 2, 0, 0, 0, 0, 0};
210
211

  auto [tree, _] = mln::morpho::watershed_hierarchy(
212
      input,
213
      [](auto tree, auto nm) -> std::vector<size_t> {
214
215
        return tree.compute_attribute_on_points(nm, mln::accu::features::count<>());
      },
216
      mln::c4, [](const auto& a, const auto& b) -> float { return mln::functional::l2dist(a, b); });
217
218
219
220
221

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

222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
  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},  //
  };

239
240
  std::vector<int>           expected_parent = {0, 0, 0, 0, 1, 3, 2, 3, 1, 4, 2, 4, 4};
  std::vector<unsigned long> expected_values = {7, 3, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0};
241
242

  auto [tree, _] = mln::morpho::watershed_hierarchy(
243
      input,
244
      [](auto tree, auto nm) -> std::vector<size_t> {
245
246
        return tree.compute_attribute_on_points(nm, mln::accu::features::count<>());
      },
247
      mln::c8, [](const auto& a, const auto& b) -> float { return mln::functional::l2dist(a, b); });
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267

  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}}    //
  };

268
269
  std::vector<int>           expected_parent = {0, 0, 1, 0, 3, 3, 2, 2, 1, 1};
  std::vector<unsigned long> expected_values = {9, 3, 2, 2, 0, 0, 0, 0, 0, 0};
270
271

  auto [tree, _] = mln::morpho::watershed_hierarchy(
272
      input,
273
      [](auto tree, auto nm) -> std::vector<size_t> {
274
275
        return tree.compute_attribute_on_points(nm, mln::accu::features::count<>());
      },
276
      mln::c26, [](const auto& a, const auto& b) -> float { return mln::functional::l2dist(a, b); });
277
278
279
280
281

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

282
283
284
285
286
287
  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]);
  }
}