File: dilationtest.cpp

package info (click to toggle)
aoflagger 3.4.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,960 kB
  • sloc: cpp: 83,076; python: 10,187; sh: 260; makefile: 178
file content (184 lines) | stat: -rw-r--r-- 4,536 bytes parent folder | download | duplicates (2)
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
91
92
93
94
95
96
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "../../structures/mask2d.h"

#include "../../algorithms/morphologicalflagger.h"

#include <boost/test/unit_test.hpp>

using algorithms::MorphologicalFlagger;

BOOST_AUTO_TEST_SUITE(dilation, *boost::unit_test::label("algorithms"))

static std::string maskToString(Mask2DCPtr mask, bool flip) {
  std::stringstream s;
  if (flip) {
    for (unsigned x = 0; x < mask->Width(); ++x) {
      for (unsigned y = 0; y < mask->Height(); ++y)
        s << (mask->Value(x, y) ? 'x' : ' ');
      s << '\n';
    }
  } else {
    for (unsigned y = 0; y < mask->Height(); ++y) {
      for (unsigned x = 0; x < mask->Width(); ++x)
        s << (mask->Value(x, y) ? 'x' : ' ');
      s << '\n';
    }
  }
  return s.str();
}

static void setMask(Mask2DPtr mask, bool flip, const std::string& str) {
  std::string::const_iterator i = str.begin();
  if (flip) {
    for (unsigned x = 0; x < mask->Width(); ++x) {
      for (unsigned y = 0; y < mask->Height(); ++y) {
        mask->SetValue(x, y, (*i) == 'x');
        ++i;
      }
      ++i;  // newline
    }
  } else {
    for (unsigned y = 0; y < mask->Height(); ++y) {
      for (unsigned x = 0; x < mask->Width(); ++x) {
        mask->SetValue(x, y, (*i) == 'x');
        ++i;
      }
      ++i;  // newline
    }
  }
}

template <bool Flip, typename DilateFunction>
static void testDilation(DilateFunction dilate) {
  std::string expected;
  Mask2DPtr mask = Mask2D::CreateSetMaskPtr<false>(5, 5);
  dilate(mask.get(), 0);
  expected =
      "     \n"
      "     \n"
      "     \n"
      "     \n"
      "     \n";
  BOOST_CHECK_EQUAL(maskToString(mask, Flip), expected);
  dilate(mask.get(), 1);
  BOOST_CHECK_EQUAL(maskToString(mask, Flip), expected);
  dilate(mask.get(), 2);
  BOOST_CHECK_EQUAL(maskToString(mask, Flip), expected);
  dilate(mask.get(), 3);
  BOOST_CHECK_EQUAL(maskToString(mask, Flip), expected);
  dilate(mask.get(), 4);
  BOOST_CHECK_EQUAL(maskToString(mask, Flip), expected);
  dilate(mask.get(), 5);
  BOOST_CHECK_EQUAL(maskToString(mask, Flip), expected);
  dilate(mask.get(), 6);
  BOOST_CHECK_EQUAL(maskToString(mask, Flip), expected);

  expected =
      "     \n"
      "     \n"
      "  x  \n"
      "     \n"
      "     \n";
  setMask(mask, Flip, expected);
  dilate(mask.get(), 0);
  BOOST_CHECK_EQUAL(maskToString(mask, Flip), expected);

  dilate(mask.get(), 1);
  expected =
      "     \n"
      "     \n"
      " xxx \n"
      "     \n"
      "     \n";
  BOOST_CHECK_EQUAL(maskToString(mask, Flip), expected);

  dilate(mask.get(), 1);
  expected =
      "     \n"
      "     \n"
      "xxxxx\n"
      "     \n"
      "     \n";
  BOOST_CHECK_EQUAL(maskToString(mask, Flip), expected);
  dilate(mask.get(), 1);
  BOOST_CHECK_EQUAL(maskToString(mask, Flip), expected);
  dilate(mask.get(), 100);
  BOOST_CHECK_EQUAL(maskToString(mask, Flip), expected);

  setMask(mask, Flip,
          " x   \n"
          "   x \n"
          "x    \n"
          "    x\n"
          " x x \n");
  dilate(mask.get(), 2);
  expected =
      "xxxx \n"
      " xxxx\n"
      "xxx  \n"
      "  xxx\n"
      "xxxxx\n";
  BOOST_CHECK_EQUAL(maskToString(mask, Flip), expected);

  setMask(mask, Flip,
          "x    \n"
          " x   \n"
          "  x  \n"
          "   x \n"
          "    x\n");
  dilate(mask.get(), 6);
  expected =
      "xxxxx\n"
      "xxxxx\n"
      "xxxxx\n"
      "xxxxx\n"
      "xxxxx\n";
  BOOST_CHECK_EQUAL(maskToString(mask, Flip), expected);

  if (Flip)
    mask = Mask2D::CreateSetMaskPtr<false>(4, 6);
  else
    mask = Mask2D::CreateSetMaskPtr<false>(6, 4);
  dilate(mask.get(), 0);
  expected =
      "      \n"
      "      \n"
      "      \n"
      "      \n";
  BOOST_CHECK_EQUAL(maskToString(mask, Flip), expected);

  setMask(mask, Flip,
          "x     \n"
          "  x   \n"
          "    x \n"
          "     x\n");
  dilate(mask.get(), 4);
  expected =
      "xxxxx \n"
      "xxxxxx\n"
      "xxxxxx\n"
      " xxxxx\n";
  BOOST_CHECK_EQUAL(maskToString(mask, Flip), expected);

  setMask(mask, Flip,
          "x     \n"
          "      \n"
          "    x \n"
          "     x\n");
  dilate(mask.get(), 7);
  expected =
      "xxxxxx\n"
      "      \n"
      "xxxxxx\n"
      "xxxxxx\n";
  BOOST_CHECK_EQUAL(maskToString(mask, Flip), expected);
}

BOOST_AUTO_TEST_CASE(horizontal_dilation) {
  testDilation<false>(MorphologicalFlagger::DilateFlagsHorizontally);
}

BOOST_AUTO_TEST_CASE(vertical_dilation) {
  testDilation<true>(MorphologicalFlagger::DilateFlagsVertically);
}

BOOST_AUTO_TEST_SUITE_END()