Slicer  4.8
Slicer is a multi-platform, free and open source software package for visualization and medical image computing
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
labelMapPreprocessor.h
Go to the documentation of this file.
1 #ifndef LABELMAPPREPROCESSOR_H
2 #define LABELMAPPREPROCESSOR_H
3 
4 #include "itkImage.h"
5 #include "itkImageRegionIterator.h"
6 
7 #include <algorithm>
8 
9 template <typename pixel_t>
10 typename itk::Image<pixel_t, 3>::Pointer
11 preprocessLabelMap(typename itk::Image<pixel_t, 3>::Pointer originalLabelMap, pixel_t desiredLabel)
12 {
13  /*
14  If there is a single non-zero label in the originalLabelMap, then
15  use that as the label map to feed the segmentor.
16 
17  If originalLabelMap contains multiple labels, extract
18  desiredLabel, forms a new label map containing only desiredLabel
19  to feed the segmentor.
20 
21  If originalLabelMap contains multiple labels, but do NOT contain
22  desiredLabel, then use all the non-zero labels as a single label
23  and output label value is desiredLabel.
24 
25  1. count number of different labels in the originalLabelMap
26 
27  2. if #= 1, return it
28 
29  3. if #= 2, if desiredLabel is not there, then return originalLableMap itself
30 
31  4. if #= 2, go thru originalLabelMap and fill in a new lable map
32  with 1 where the label matches.
33 
34 
35  */
36 
37  typedef itk::Image<pixel_t, 3> image_t;
38 
39  typedef itk::ImageRegionIterator<image_t> imageRegionIterator_t;
40 
41  // 1.
42  imageRegionIterator_t iter(originalLabelMap, originalLabelMap->GetLargestPossibleRegion() );
43  iter.GoToBegin();
44 
45  typename image_t::SizeType sz = originalLabelMap->GetLargestPossibleRegion().GetSize();
46 
47  std::vector<pixel_t> uniqueLabels(sz[0] * sz[1] * sz[2]);
48  long i = 0;
49  for( ; !iter.IsAtEnd(); ++iter )
50  {
51  uniqueLabels[i++] = iter.Get();
52  }
53 
54  std::sort(uniqueLabels.begin(), uniqueLabels.end() );
55  typename std::vector<pixel_t>::iterator itl = std::unique(uniqueLabels.begin(), uniqueLabels.end() );
56  uniqueLabels.resize( itl - uniqueLabels.begin() );
57 
58  if( uniqueLabels[0] != 0 )
59  {
60  std::cerr << "Error: least label is not 0? no background?\n";
61  raise(SIGABRT);
62  }
63 
64  short numOfLabels = uniqueLabels.size() - 1; // 0 not count
65 
66  // 2.
67  if( 1 == numOfLabels )
68  {
69  return originalLabelMap;
70  }
71 
72  // 3.
73  if( !std::binary_search(uniqueLabels.begin(), uniqueLabels.end(), desiredLabel) )
74  {
75  return originalLabelMap;
76  }
77 
78  // 4.
79  typename image_t::Pointer newLabelMap = image_t::New();
80  newLabelMap->CopyInformation(originalLabelMap);
81  newLabelMap->SetRegions( originalLabelMap->GetLargestPossibleRegion() );
82  newLabelMap->Allocate();
83  newLabelMap->FillBuffer(0);
84 
85  imageRegionIterator_t iterNew(newLabelMap, newLabelMap->GetLargestPossibleRegion() );
86  iterNew.GoToBegin();
87  iter.GoToBegin();
88  for( ; !iter.IsAtEnd(); ++iter, ++iterNew )
89  {
90  if( iter.Get() == desiredLabel )
91  {
92  iterNew.Set(1);
93  }
94  }
95 
96  return newLabelMap;
97 }
98 
99 #endif
itk::Image< pixel_t, 3 >::Pointer preprocessLabelMap(typename itk::Image< pixel_t, 3 >::Pointer originalLabelMap, pixel_t desiredLabel)