watershed_hierarchy.cpp 6.29 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
11
12
13
14
15
16
17
18
19
20

#include <gtest/gtest.h>

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

21
22
  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};
23

24
25
  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};
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

  auto [tree, _] = mln::morpho::watershed_hierarchy(
      input, mln::accu::features::count<>(), mln::c4,
      [](const auto& a, const auto& b) -> float { return mln::functional::l2dist_t<>()(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]);
  }
}

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
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(
      input, mln::accu::features::count<>(), mln::c4,
      [](const auto& a, const auto& b) -> std::uint8_t { return mln::functional::l2dist_t<>()(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]);
  }
}

73
74
75
76
77
78
79
80
81
82
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}}, //
  };

83
84
  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};
85

86
87
  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};
88
89
90
91
92
93
94
95
96

  auto [tree, _] = mln::morpho::watershed_hierarchy(
      input, mln::accu::features::count<>(), mln::c4,
      [](const auto& a, const auto& b) -> float { return mln::functional::l2dist_t<>()(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());

97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
  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(
      input, mln::accu::features::count<>(), mln::c8,
      [](const auto& a, const auto& b) -> float { return mln::functional::l2dist_t<>()(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, 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(
      input, mln::accu::features::count<>(), mln::c26,
      [](const auto& a, const auto& b) -> float { return mln::functional::l2dist_t<>()(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());

157
158
159
160
161
162
  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]);
  }
}