File: itkUniqueLabelMapFiltersGTest.cxx

package info (click to toggle)
insighttoolkit5 5.4.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 704,384 kB
  • sloc: cpp: 783,592; ansic: 628,724; xml: 44,704; fortran: 34,250; python: 22,874; sh: 4,078; pascal: 2,636; lisp: 2,158; makefile: 464; yacc: 328; asm: 205; perl: 203; lex: 146; tcl: 132; javascript: 98; csh: 81
file content (309 lines) | stat: -rw-r--r-- 10,373 bytes parent folder | download
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*=========================================================================
 *
 *  Copyright NumFOCUS
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         https://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/

#include "itkGTest.h"

#include "itkImage.h"
#include "itkLabelImageToStatisticsLabelMapFilter.h"
#include "itkImageFileWriter.h"
#include <algorithm>


#include "itkLabelImageToLabelMapFilter.h"
#include "itkObjectByObjectLabelMapFilter.h"
#include "itkShapeLabelObjectAccessors.h"
#include "itkFlatStructuringElement.h"
#include "itkBinaryDilateImageFilter.h"
#include "itkLabelUniqueLabelMapFilter.h"
#include "itkTestingHashImageFilter.h"
#include "itksys/SystemTools.hxx"
#include "itkTestDriverIncludeRequiredFactories.h"


namespace
{

class UniqueLabelMapFixture : public ::testing::Test
{
public:
  UniqueLabelMapFixture() = default;
  ~UniqueLabelMapFixture() override = default;

protected:
  void
  SetUp() override
  {
    RegisterRequiredFactories();
  }
  void
  TearDown() override
  {}

  std::string
  GetTestName() const
  {
    return ::testing::UnitTest::GetInstance()->current_test_info()->name();
  }

  template <unsigned int D, typename TPixelType = unsigned short>
  struct FixtureUtilities
  {
    static const unsigned int Dimension = D;

    using LabelPixelType = TPixelType;
    using LabelImageType = itk::Image<LabelPixelType, Dimension>;
    using IndexType = typename LabelImageType::IndexType;

    using LabelObjectType = itk::StatisticsLabelObject<LabelPixelType, Dimension>;

    using LabelMapType = itk::LabelMap<itk::LabelObject<LabelPixelType, Dimension>>;


    static typename LabelImageType::Pointer
    CreateLabelImage(const std::vector<IndexType> & indices)
    {
      const size_t size = 25;
      auto         image = LabelImageType::New();

      typename LabelImageType::SizeType imageSize;
      imageSize.Fill(size);
      image->SetRegions(typename LabelImageType::RegionType(imageSize));
      image->Allocate();
      image->FillBuffer(0);

      for (LabelPixelType id = 0; id < indices.size(); ++id)
      {
        image->SetPixel(indices[id], id + 1);
      }
      return image;
    }

    static typename LabelMapType::Pointer
    LabelMapFromLabelImage(const LabelImageType * image, unsigned int dilateRadius = 0)
    {
      using ToLabelMapType = itk::LabelImageToLabelMapFilter<LabelImageType>;
      auto toLabelMap = ToLabelMapType::New();
      toLabelMap->SetInput(image);

      if (dilateRadius == 0)
      {
        toLabelMap->Update();
        return toLabelMap->GetOutput();
      }

      using KernelType = itk::FlatStructuringElement<Dimension>;
      using DilateType = itk::BinaryDilateImageFilter<LabelImageType, LabelImageType, KernelType>;
      auto                          dilate = DilateType::New();
      typename KernelType::SizeType rad;
      rad.Fill(dilateRadius);
      dilate->SetKernel(KernelType::Ball(rad));


      using OIType = itk::ObjectByObjectLabelMapFilter<LabelMapType, LabelMapType, DilateType>;
      auto oi = OIType::New();
      oi->SetInput(toLabelMap->GetOutput());
      oi->SetFilter(dilate);
      oi->SetPadSize(rad);

      oi->Update();

      return oi->GetOutput();
    }
  };

  template <typename TLabelMap>
  static typename itk::Image<typename TLabelMap::LabelObjectType::LabelType, TLabelMap::ImageDimension>::Pointer
  LabelMapToLabelImage(TLabelMap * labelMap)
  {
    using ImageType = itk::Image<typename TLabelMap::LabelObjectType::LabelType, TLabelMap::ImageDimension>;
    using L2IType = itk::LabelMapToLabelImageFilter<TLabelMap, ImageType>;
    auto l2i = L2IType::New();
    l2i->SetInput(labelMap);
    l2i->Update();
    return l2i->GetOutput();
  }


  template <typename TLabelMap>
  void
  CheckLabelMapOverlap(TLabelMap * labelMap)
  {
    for (auto & labelObject : labelMap->GetLabelObjects())
    {
      // Manually check each label object against all other label objects, to ensure that no two label objects share an
      // index.
      for (itk::SizeValueType lineNumber = 0; lineNumber < labelObject->GetNumberOfLines(); ++lineNumber)
      {
        auto line = labelObject->GetLine(lineNumber);
        auto idx = line.GetIndex();
        ASSERT_LE(line.GetLength(), labelObject->Size());
        for (itk::SizeValueType lengthIndex = 0; lengthIndex < line.GetLength(); ++lengthIndex)
        {
          for (auto & checkObject : labelMap->GetLabelObjects())
          {
            if (checkObject != labelObject)
            {
              EXPECT_FALSE(checkObject->HasIndex(idx))
                << "Label: " << int(labelObject->GetLabel()) << " and " << int(checkObject->GetLabel()) << " has index "
                << idx << std::endl;
            }
          }
          ++idx[0];
        }
      }
    }
  }

  template <typename TImageType>
  static std::string
  MD5Hash(const TImageType * image)
  {

    using HashFilter = itk::Testing::HashImageFilter<TImageType>;
    auto hasher = HashFilter::New();
    hasher->SetInput(image);
    hasher->InPlaceOff();
    hasher->Update();
    return hasher->GetHash();
  }
};
} // namespace


TEST_F(UniqueLabelMapFixture, EmptyImage)
{
  const std::vector<typename FixtureUtilities<2>::IndexType> indices = {};
  auto                                                       image = FixtureUtilities<2>::CreateLabelImage(indices);
  auto labelMap = FixtureUtilities<2>::LabelMapFromLabelImage(image.GetPointer(), 15);

  auto filter = itk::LabelUniqueLabelMapFilter<typename decltype(labelMap)::ObjectType>::New();
  filter->SetInput(labelMap);

  CheckLabelMapOverlap(filter->GetOutput());
  filter->Update();

  auto out = LabelMapToLabelImage(filter->GetOutput());

  // check the hash of out, should be all zeros
  EXPECT_EQ(MD5Hash(out.GetPointer()), "393017b9101a884b66d64849d99a7d05");
}


TEST_F(UniqueLabelMapFixture, OneLabel)
{
  const std::vector<typename FixtureUtilities<2>::IndexType> indices = { { 10, 10 } };
  auto                                                       image = FixtureUtilities<2>::CreateLabelImage(indices);
  auto labelMap = FixtureUtilities<2>::LabelMapFromLabelImage(image.GetPointer(), 0);

  auto filter = itk::LabelUniqueLabelMapFilter<typename decltype(labelMap)::ObjectType>::New();
  filter->SetInput(labelMap);
  filter->Update();

  CheckLabelMapOverlap(filter->GetOutput());

  auto out = LabelMapToLabelImage(filter->GetOutput());

  EXPECT_EQ(MD5Hash(out.GetPointer()), MD5Hash(image.GetPointer()));
  EXPECT_EQ(MD5Hash(out.GetPointer()), "9c8ee8f2fe887fd6d2393d6416df3fb6");
}


TEST_F(UniqueLabelMapFixture, OnesLabel)
{
  const std::vector<typename FixtureUtilities<2>::IndexType> indices = { { 0, 0 }, { 1, 0 }, { 2, 0 }, { 3, 0 },
                                                                         { 0, 4 }, { 2, 4 }, { 0, 5 } };
  auto                                                       image = FixtureUtilities<2>::CreateLabelImage(indices);
  auto labelMap = FixtureUtilities<2>::LabelMapFromLabelImage(image.GetPointer(), 0);

  auto filter = itk::LabelUniqueLabelMapFilter<typename decltype(labelMap)::ObjectType>::New();
  filter->SetInput(labelMap);
  filter->Update();

  CheckLabelMapOverlap(filter->GetOutput());

  auto out = LabelMapToLabelImage(filter->GetOutput());

  EXPECT_EQ(MD5Hash(out.GetPointer()), MD5Hash(image.GetPointer()));
  EXPECT_EQ(MD5Hash(out.GetPointer()), "220048b56395d98a8f20a5b1733bdde6");
}


TEST_F(UniqueLabelMapFixture, Dilate1)
{
  const std::vector<typename FixtureUtilities<2>::IndexType> indices = { { 0, 0 }, { 1, 0 }, { 2, 0 }, { 3, 0 },
                                                                         { 0, 4 }, { 2, 4 }, { 0, 10 } };
  auto                                                       image = FixtureUtilities<2>::CreateLabelImage(indices);
  auto labelMap = FixtureUtilities<2>::LabelMapFromLabelImage(image.GetPointer(), 1);

  auto filter = itk::LabelUniqueLabelMapFilter<typename decltype(labelMap)::ObjectType>::New();
  filter->SetInput(labelMap);
  filter->InPlaceOff();
  filter->ReverseOrderingOff();
  filter->Update();

  CheckLabelMapOverlap(filter->GetOutput());

  auto out = LabelMapToLabelImage(filter->GetOutput());

  EXPECT_EQ(MD5Hash(out.GetPointer()), "8bfc8570ee203fa68be18fe055cf389d");
  itk::WriteImage(out.GetPointer(), GetTestName() + "_off.png");

  filter->ReverseOrderingOn();
  filter->Update();

  CheckLabelMapOverlap(filter->GetOutput());

  out = LabelMapToLabelImage(filter->GetOutput());

  EXPECT_EQ(MD5Hash(out.GetPointer()), "bef76c79168969548f1a8090d46b5f7e");
  itk::WriteImage(out.GetPointer(), GetTestName() + "_on.png");
}


TEST_F(UniqueLabelMapFixture, Dilate2)
{
  const std::vector<typename FixtureUtilities<2>::IndexType> indices = { { 0, 0 }, { 1, 0 }, { 2, 0 }, { 3, 0 },
                                                                         { 0, 4 }, { 2, 4 }, { 0, 5 } };
  auto                                                       image = FixtureUtilities<2>::CreateLabelImage(indices);
  auto labelMap = FixtureUtilities<2>::LabelMapFromLabelImage(image.GetPointer(), 2);

  auto filter = itk::LabelUniqueLabelMapFilter<typename decltype(labelMap)::ObjectType>::New();
  filter->SetInput(labelMap);
  filter->InPlaceOff();
  filter->ReverseOrderingOff();
  filter->Update();

  CheckLabelMapOverlap(filter->GetOutput());

  auto out = LabelMapToLabelImage(filter->GetOutput());

  EXPECT_EQ(MD5Hash(out.GetPointer()), "02ec62c193413dd82cb0feaee1ee0b12");
  itk::WriteImage(out.GetPointer(), GetTestName() + "_off.png");


  filter->ReverseOrderingOn();
  filter->Update();

  CheckLabelMapOverlap(filter->GetOutput());

  out = LabelMapToLabelImage(filter->GetOutput());

  EXPECT_EQ(MD5Hash(out.GetPointer()), "1c6901199b01ac095511792f447578a3");

  itk::WriteImage(out.GetPointer(), GetTestName() + "_on.png");
}