Shapeworks Studio  2.1
Shape analysis software suite
List of all members | Public Types | Public Member Functions | Protected Member Functions
itk::PSMCleanAndCenterLabelMapImageFilter< TImage > Class Template Reference

Process an input segmentation to produce an isotropically spaced version with holes filled and center-of-mass at the origin. More...

#include <itkPSMCleanAndCenterLabelMapImageFilter.h>

+ Inheritance diagram for itk::PSMCleanAndCenterLabelMapImageFilter< TImage >:
+ Collaboration diagram for itk::PSMCleanAndCenterLabelMapImageFilter< TImage >:

Public Types

typedef PSMCleanAndCenterLabelMapImageFilter Self
 
typedef ImageToImageFilter< TImage, TImage > Superclass
 
typedef SmartPointer< SelfPointer
 
typedef SmartPointer< const SelfConstPointer
 
typedef TImage ImageType
 
typedef ImageType::PixelType PixelType
 
typedef ImageType::PointType PointType
 
typedef ImageType::RegionType RegionType
 
typedef RegionType::SizeType SizeType
 
typedef RegionType::IndexType IndexType
 

Public Member Functions

 itkNewMacro (Self)
 
 itkTypeMacro (PSMCleanAndCenterLabelMapImageFilter, ImageToImageFilter)
 
 itkStaticConstMacro (Dimension, unsigned int, TImage::ImageDimension)
 
void GenerateData ()
 
 itkSetMacro (ForegroundValue, PixelType)
 
 itkGetMacro (ForegroundValue, PixelType)
 
 itkSetMacro (BackgroundValue, PixelType)
 
 itkGetMacro (BackgroundValue, PixelType)
 

Protected Member Functions

void PrintSelf (std::ostream &os, Indent indent) const
 
void IsolateLargestComponent (ImageType *) const
 
void FillHoles (ImageType *) const
 
void ResampleToIsotropic (ImageType *) const
 
void Center (ImageType *)
 
virtual void GenerateOutputInformation ()
 
virtual void GenerateInputRequestedRegion ()
 

Detailed Description

template<class TImage>
class itk::PSMCleanAndCenterLabelMapImageFilter< TImage >

Process an input segmentation to produce an isotropically spaced version with holes filled and center-of-mass at the origin.

This filter may be used to turn a label map (segmentation) image into an isotropic version with its center-of-mass at the origin. This filter is intended to be used as part of a preprocessing pipeline to prepare segmentations for use with any of the Particle Shape Modeling filters (e.g. PSMEntropyModelFilter).

The filter processes the input image as follows:

1) The largest connected component for the specified foreground component is identified and isolated. All other pixels in the image are set to the background value (default zero).

2) Holes are filled in the segmentation resulting from Step 1.

3) The image is resampled to isotropic voxels, if necessary.

4) The center of mass of the foreground object is computed and the center of the image is transformed to that location.

NOTE: This filter assumes that the upper-left-hand corner of the image is NOT part of the foreground.

WHAT ARE THE PARAMETERS?

This filter is templated over the input image type. It produces an output image of the same type.

Author
Josh Cates

Definition at line 63 of file itkPSMCleanAndCenterLabelMapImageFilter.h.

Member Typedef Documentation

template<class TImage>
typedef TImage itk::PSMCleanAndCenterLabelMapImageFilter< TImage >::ImageType

Image-type-related typedefs

Definition at line 74 of file itkPSMCleanAndCenterLabelMapImageFilter.h.

Standard class typedefs.

Definition at line 68 of file itkPSMCleanAndCenterLabelMapImageFilter.h.

Member Function Documentation

template<class TImage >
void itk::PSMCleanAndCenterLabelMapImageFilter< TImage >::Center ( ImageType work)
protected

Translates the center-of-mass to the center of the image. Also modifies the image information.

Definition at line 214 of file itkPSMCleanAndCenterLabelMapImageFilter.hxx.

215 {
216  // Reset the physical coordinates at the center of the image to (0,0,0)
217  PointType lower;
218  PointType upper;
219  PointType ul_physical_coords;
220  work->TransformIndexToPhysicalPoint(work->GetBufferedRegion().GetIndex(),
221  lower);
222  work->TransformIndexToPhysicalPoint(work->GetBufferedRegion().GetIndex()
223  + work->GetBufferedRegion().GetSize(), upper);
224  for (unsigned int i = 0; i < Dimension; i++)
225  {
226  ul_physical_coords[i] = -(lower[i] + upper[i]) / 2.0;
227  }
228 
229  // IndexType idx;
230  // work->TransformPhysicalPointToIndex(ul_physical_coords,idx);
231 
232  // It is confusing, but the following command sets the physical coordinates
233  // of pixel number 0,0,0... (i.e. the upper-left corner of the volume).
234  work->SetOrigin(ul_physical_coords);
235 
236  // Find the center of mass.
237  ImageRegionIteratorWithIndex<ImageType> oit(work, work->GetBufferedRegion());
238  oit.GoToBegin();
239 
240  Array<double> params(Dimension);
241  params.Fill(0.0);
242  double count = 0.0;
244  for (; ! oit.IsAtEnd(); ++oit)
245  {
246  if (oit.Get() != m_BackgroundValue)
247  {
248  // Get the physical index from the image index.
249  work->TransformIndexToPhysicalPoint(oit.GetIndex(), point);
250  for (unsigned int i = 0; i < Dimension; i++) { params[i] += point[i]; }
251  count += 1.0;
252  }
253  }
254 
255  // Compute center of mass.
256  for (unsigned int i = 0; i < Dimension; i++)
257  {
258  params[i] = params[i] / count;
259  }
260 
261  // Translate the segmentation into the output
262  typename TranslationTransform<double,Dimension>::Pointer trans
263  = TranslationTransform<double,Dimension>::New();
264  trans->SetParameters(params);
265 
266  typename NearestNeighborInterpolateImageFunction<ImageType,double>::Pointer
267  interp = NearestNeighborInterpolateImageFunction<ImageType,double>::New();
268 
269  typename ResampleImageFilter<ImageType, ImageType>::Pointer resampler
270  = ResampleImageFilter<ImageType, ImageType>::New();
271  resampler->SetOutputParametersFromImage(work);
272  resampler->SetTransform(trans);
273  resampler->SetInterpolator(interp);
274  resampler->SetInput(work);
275 
276  // the size.
277  this->GetOutput()->SetRegions(this->GetInput()->GetLargestPossibleRegion());
278  this->GetOutput()->Allocate();
279 
280  // pipe to the output image
281  resampler->GraftOutput(this->GetOutput());
282  resampler->Update();
283 
284  // also necessary for pipe to output
285  // see itkImageSource for description
286  this->GraftOutput(resampler->GetOutput());
287 
288  // Make sure the image information is correct.
289  // itk::Matrix<double,3,3> I;
290  // I.SetIdentity();
291  // this->GetOutput()->SetDirection(I);
292 }
Definition: Shape.h:14
template<class TImage >
void itk::PSMCleanAndCenterLabelMapImageFilter< TImage >::FillHoles ( ImageType work) const
protected

Fills holes in the foreground segmentation.

Definition at line 171 of file itkPSMCleanAndCenterLabelMapImageFilter.hxx.

172 {
173 
174  // TODO: Need a check to see if the UL corner is background value.
175  IndexType idx;
176  idx.Fill(0);
177 
178  if (work->GetPixel(idx) == m_ForegroundValue)
179  {
180  itkWarningMacro("Could not fill holes. Upper-left hand corner is not in the background. See documentation for this filter for more information.")
181  }
182 
183  // Find a background value seed at the boundary. Assumption is that
184  // the corner index is background.
185  typename ConnectedThresholdImageFilter<ImageType, ImageType>::IndexType seed;
186  seed.Fill(NumericTraits<PixelType>::Zero);
187 
188  // Flood fill the background with the foreground value.
189  typename itk::ConnectedThresholdImageFilter<ImageType, ImageType >::Pointer
190  ccfilter = itk::ConnectedThresholdImageFilter<ImageType, ImageType>::New();
191  ccfilter->SetInput(work);
192  ccfilter->SetLower(m_BackgroundValue);
193  ccfilter->SetUpper(m_BackgroundValue);
194  ccfilter->SetSeed(seed);
195  ccfilter->SetReplaceValue(m_ForegroundValue);
196  ccfilter->Update();
197 
198  // Logical "OR" the original image with the inverse (foreground/background flipped)
199  // of the flood-filled copy.
200  itk::ImageRegionConstIterator<ImageType> rit(ccfilter->GetOutput(),
201  work->GetBufferedRegion());
202  itk::ImageRegionIterator<ImageType> it(work,work->GetBufferedRegion());
203  for (; !rit.IsAtEnd(); ++rit, ++it)
204  {
205  if (rit.Get() == m_BackgroundValue)
206  {
207  it.Set(m_ForegroundValue);
208  }
209  }
210 }
template<class TImage >
void itk::PSMCleanAndCenterLabelMapImageFilter< TImage >::GenerateData ( )

Do the work of batch-processing the input images.

Definition at line 91 of file itkPSMCleanAndCenterLabelMapImageFilter.hxx.

92 {
93  // Allocate work image. Same size as input. Later we will crop it.
94  typename ImageType::Pointer work = ImageType::New();
95  work->CopyInformation(this->GetInput());
96  work->SetRegions(this->GetInput()->GetRequestedRegion());
97  work->Allocate();
98 
99  // Isolate largest connected component into the work image.
100  // Modifies the work image.
101  this->IsolateLargestComponent(work);
102 
103  // We are done with inputs. Release them if requested.
104  this->ReleaseInputs();
105 
106  // Fill holes in the working image
107  this->FillHoles(work);
108 
109  // Resample the image to isotropic volumes, in case it is not already.
110  this->ResampleToIsotropic(work);
111 
112  // Reset the origin to the center of the image and translate the
113  // center-of-mass of the segmentation to the origin.
114  this->Center(work);
115 
116 
117 
118 }
template<class TImage >
void itk::PSMCleanAndCenterLabelMapImageFilter< TImage >::GenerateInputRequestedRegion ( )
protectedvirtual

This filter must provide an implementation for GenerateInputRequestedRegion() in order to inform the pipeline execution model.

See also
ProcessObject::GenerateInputRequestedRegion()

Definition at line 62 of file itkPSMCleanAndCenterLabelMapImageFilter.hxx.

63 {
64  // We need the whole image!
65  // Get pointers to the input and output.
66  typename Superclass::InputImagePointer inputPtr =
67  const_cast< TImage * >( this->GetInput() );
68  typename Superclass::OutputImagePointer outputPtr
69  = this->GetOutput();
70 
71  if ( !inputPtr || !outputPtr )
72  {
73  return;
74  }
75 
76  inputPtr->SetRequestedRegion( inputPtr->GetLargestPossibleRegion());
77 }
template<class TImage >
void itk::PSMCleanAndCenterLabelMapImageFilter< TImage >::GenerateOutputInformation ( )
protectedvirtual

This filter must provide an implementation for GenerateOutputInformation() in order to inform the pipeline execution model. The original documentation of this method is below.

See also
ProcessObject::GenerateOutputInformaton()

Definition at line 82 of file itkPSMCleanAndCenterLabelMapImageFilter.hxx.

83 {
84  // call the superclass' implementation of this method
85  Superclass::GenerateOutputInformation();
86 }
template<class TImage >
void itk::PSMCleanAndCenterLabelMapImageFilter< TImage >::IsolateLargestComponent ( ImageType work) const
protected

Isolates the largest connected component in an image. Pixels in this component are set to the foreground value and pixels in other components are set to the background value.

Definition at line 122 of file itkPSMCleanAndCenterLabelMapImageFilter.hxx.

123 {
124  // Create a connected component filter
125  typename itk::ConnectedComponentImageFilter<TImage, TImage>::Pointer ccfilter
126  = itk::ConnectedComponentImageFilter<TImage, TImage>::New();
127  ccfilter->SetInput(this->GetInput());
128  ccfilter->FullyConnectedOn();
129  ccfilter->Update();
130 
131  // First find the size of the connected components.
132  std::map<int, int> sizes;
133  ImageRegionConstIterator<TImage> rit(ccfilter->GetOutput(),
134  ccfilter->GetOutput()->GetBufferedRegion());
135  for (; !rit.IsAtEnd(); ++rit)
136  {
137  if (rit.Get() > 0)
138  {
139  sizes[rit.Get()] = sizes[rit.Get()] + 1;
140  }
141  }
142 
143  // Find largest connected component. Assumes connected component
144  // algorithm enumerates sequentially. (This is true for the ITK
145  // algorithm). Copy largest connected component into work image.
146  int maxsize = 0;
147  int bigcomponent = 0;
148  for (typename std::map<int, int>::const_iterator mapit = sizes.begin();
149  mapit != sizes.end(); mapit++)
150  {
151  if ((*mapit).second > maxsize)
152  {
153  bigcomponent = (*mapit).first;
154  maxsize = (*mapit).second;
155  }
156  }
157 
158  // Copy result into work image. Remove all but the largest connected component.
159  ImageRegionIterator<TImage> it(work, work->GetBufferedRegion());
160  rit.GoToBegin();
161  it.GoToBegin();
162  for ( ; ! rit.IsAtEnd(); ++it, ++rit)
163  {
164  if (rit.Get() == bigcomponent) it.Set(m_ForegroundValue);
165  else it.Set(m_BackgroundValue);
166  }
167 }
template<class TImage>
itk::PSMCleanAndCenterLabelMapImageFilter< TImage >::itkNewMacro ( Self  )

Method for creation through the object factory.

template<class TImage>
itk::PSMCleanAndCenterLabelMapImageFilter< TImage >::itkSetMacro ( ForegroundValue  ,
PixelType   
)

Set/Get the foreground value. This is the label of interest in the input images. All other values in the input images will be considered background and relabeled to the background value in the output. You MUST set this value. Default value is 1.

template<class TImage>
itk::PSMCleanAndCenterLabelMapImageFilter< TImage >::itkSetMacro ( BackgroundValue  ,
PixelType   
)

Get/Set the background value. This is the value to which all non-foreground values will be changed in the output. The default value is zero.

template<class TImage>
itk::PSMCleanAndCenterLabelMapImageFilter< TImage >::itkStaticConstMacro ( Dimension  ,
unsigned  int,
TImage::ImageDimension   
)

Dimensionality of the domain of the particle system.

template<class TImage>
itk::PSMCleanAndCenterLabelMapImageFilter< TImage >::itkTypeMacro ( PSMCleanAndCenterLabelMapImageFilter< TImage >  ,
ImageToImageFilter   
)

Run-time type information (and related methods).

template<class TImage>
void itk::PSMCleanAndCenterLabelMapImageFilter< TImage >::ResampleToIsotropic ( ImageType ) const
inlineprotected

Resamples the image to isotropic voxel spacing if needed.

Definition at line 120 of file itkPSMCleanAndCenterLabelMapImageFilter.h.

121  {
122  return;
123  }

The documentation for this class was generated from the following files: