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