1 #ifndef LABELMAPPREPROCESSOR_H 2 #define LABELMAPPREPROCESSOR_H 5 #include "itkImageRegionIterator.h" 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)
37 typedef itk::Image<pixel_t, 3> image_t;
39 typedef itk::ImageRegionIterator<image_t> imageRegionIterator_t;
42 imageRegionIterator_t iter(originalLabelMap, originalLabelMap->GetLargestPossibleRegion() );
45 typename image_t::SizeType sz = originalLabelMap->GetLargestPossibleRegion().GetSize();
47 std::vector<pixel_t> uniqueLabels(sz[0] * sz[1] * sz[2]);
49 for( ; !iter.IsAtEnd(); ++iter )
51 uniqueLabels[i++] = iter.Get();
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() );
58 if( uniqueLabels[0] != 0 )
60 std::cerr <<
"Error: least label is not 0? no background?\n";
64 short numOfLabels = uniqueLabels.size() - 1;
67 if( 1 == numOfLabels )
69 return originalLabelMap;
73 if( !std::binary_search(uniqueLabels.begin(), uniqueLabels.end(), desiredLabel) )
75 return originalLabelMap;
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);
85 imageRegionIterator_t iterNew(newLabelMap, newLabelMap->GetLargestPossibleRegion() );
88 for( ; !iter.IsAtEnd(); ++iter, ++iterNew )
90 if( iter.Get() == desiredLabel )
itk::Image< pixel_t, 3 >::Pointer preprocessLabelMap(typename itk::Image< pixel_t, 3 >::Pointer originalLabelMap, pixel_t desiredLabel)