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

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

#include <itkPSMTrimLabelMapImageFilter.h>

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

Public Types

typedef PSMTrimLabelMapImageFilter 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 (PSMTrimLabelMapImageFilter, ImageToImageFilter)
 
 itkStaticConstMacro (Dimension, unsigned int, TImage::ImageDimension)
 
void GenerateData ()
 
 itkSetMacro (ForegroundValue, PixelType)
 
 itkGetMacro (ForegroundValue, PixelType)
 
 itkSetMacro (BackgroundValue, PixelType)
 
 itkGetMacro (BackgroundValue, PixelType)
 
const RegionType & GetBoundingBox () const
 

Protected Member Functions

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

Detailed Description

template<class TImage>
class itk::PSMTrimLabelMapImageFilter< TImage >

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

This filter may be used to turn a label map (segmentation) image into a cropped 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 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 61 of file itkPSMTrimLabelMapImageFilter.h.

Member Typedef Documentation

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

Image-type-related typedefs

Definition at line 72 of file itkPSMTrimLabelMapImageFilter.h.

template<class TImage>
typedef PSMTrimLabelMapImageFilter itk::PSMTrimLabelMapImageFilter< TImage >::Self

Standard class typedefs.

Definition at line 66 of file itkPSMTrimLabelMapImageFilter.h.

Member Function Documentation

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

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

Definition at line 219 of file itkPSMTrimLabelMapImageFilter.hxx.

220 {
221  // // // Reset the origin of the image to the very center of the image.
222  // // // double orig[Dimension];
223  PointType lower;
224  PointType upper;
225  PointType center;
226  work->TransformIndexToPhysicalPoint(work->GetBufferedRegion().GetIndex(),
227  lower);
228  work->TransformIndexToPhysicalPoint(work->GetBufferedRegion().GetIndex()
229  + work->GetBufferedRegion().GetSize(), upper);
230  for (unsigned int i = 0; i < Dimension; i++)
231  {
232  center[i] = (lower[i] + upper[i]) / 2.0;
233  }
234 
235  IndexType idx;
236  work->TransformPhysicalPointToIndex(center,idx);
237 
238  work->SetPixel(idx,4.0);
239  // work->SetOrigin(center);
240 
241  // Copy the original image and find the center of mass.
242  typename ImageType::Pointer simg = ImageType::New();
243  simg->CopyInformation(work);
244  simg->SetRegions(work->GetBufferedRegion());
245  simg->Allocate();
246 
247  ImageRegionIteratorWithIndex<ImageType> oit(work, work->GetBufferedRegion());
248  ImageRegionIteratorWithIndex<ImageType> sit(simg, work->GetBufferedRegion());
249  sit.GoToBegin();
250  oit.GoToBegin();
251 
252  Array<double> params(Dimension);
253  params.Fill(0.0);
254  double count = 0.0;
256  for (; ! oit.IsAtEnd(); ++oit, ++sit)
257  {
258  if (oit.Get() != m_BackgroundValue)
259  {
260  sit.Set(oit.Get());
261 
262  // Get the physical index from the image index.
263  work->TransformIndexToPhysicalPoint(oit.GetIndex(), point);
264  for (unsigned int i = 0; i < Dimension; i++) { params[i] += point[i]; }
265  count += 1.0;
266  }
267  else { sit.Set(m_BackgroundValue); }
268  }
269 
270  // Compute center of mass.
271  for (unsigned int i = 0; i < Dimension; i++)
272  {
273  params[i] = params[i] / count;
274  // std::cout << "params " << i << " = " << params[i] <<std::endl;
275  }
276 
277  // PointType pt;
278  // for (unsigned i = 0; i < Dimension; i++) { pt[i] = params[i];}
279  // work->TransformPhysicalPointToIndex(pt,idx);
280  // work->SetPixel(idx,8.0);
281 
282  // std::cout << "Center of mass = " << pt << std::endl;
283  // std::cout << "idx = " << idx << std::endl;
284  // return;
285  // Zero out the original image.
286  for (oit.GoToBegin(); ! oit.IsAtEnd(); ++oit) { oit.Set(m_BackgroundValue); }
287 
288  // Translate the segmentation back into the original image.
289  typename TranslationTransform<double,Dimension>::Pointer trans
290  = TranslationTransform<double,Dimension>::New();
291  trans->SetParameters(params);
292 
293  typename NearestNeighborInterpolateImageFunction<ImageType,double>::Pointer
294  interp = NearestNeighborInterpolateImageFunction<ImageType,double>::New();
295 
296  typename ResampleImageFilter<ImageType, ImageType>::Pointer resampler
297  = ResampleImageFilter<ImageType, ImageType>::New();
298  resampler->SetOutputParametersFromImage(work);
299  resampler->SetTransform(trans);
300  resampler->SetInterpolator(interp);
301  resampler->SetInput(simg);
302 
303  resampler->Update();
304 
305  // Copy resampled image back to the original image
306  oit.GoToBegin();
307  ImageRegionIterator<ImageType>
308  it(resampler->GetOutput(), work->GetBufferedRegion());
309  for ( ; ! it.IsAtEnd(); ++it, ++oit)
310  {
311  oit.Set(it.Get());
312  }
313 }
Definition: Shape.h:14
template<class TImage >
void itk::PSMTrimLabelMapImageFilter< TImage >::Crop ( ImageType work)
protected

Crops the image to the smallest possible bounding box that contains the image. This method alters the output of the image.

Definition at line 317 of file itkPSMTrimLabelMapImageFilter.hxx.

318 {
319  // Find the bounding box containing the foreground
320  ImageRegionIteratorWithIndex<ImageType>
321  it(work, work->GetBufferedRegion());
322 
323  SizeType workSize = work->GetBufferedRegion().GetSize();
324  IndexType workCorner = work->GetBufferedRegion().GetIndex();
325 
326  // Search for bounding box.
327  IndexType idx;
328  IndexType lower = workCorner + workSize;
329  IndexType upper = workCorner;
330 
331  for (it.GoToBegin(); ! it.IsAtEnd(); ++it)
332  {
333  if (it.Get() != m_BackgroundValue)
334  {
335  idx = it.GetIndex();
336 
337  for (unsigned int i = 0; i < Dimension; i++)
338  {
339  if (lower[i] > idx[i]) lower[i] = idx[i];
340  if (upper[i] < idx[i]) upper[i] = idx[i];
341  }
342  }
343  }
344 
345  SizeType size;
346  for (unsigned int i = 0; i < Dimension; i++)
347  {
348  size[i] = upper[i] - lower[i];
349  }
350 
351  // Create bounding box region
352  m_BoundingBox.SetSize(size);
353  m_BoundingBox.SetIndex(lower);
354 
355  // Create cropping filter
356  typename itk::ExtractImageFilter<ImageType, ImageType>::Pointer extractor
357  = itk::ExtractImageFilter<ImageType, ImageType>::New();
358  extractor->SetExtractionRegion(m_BoundingBox);
359  extractor->SetInput(work);
360 
361  // We are finally able to allocate an output image now that we know
362  // the size.
363  this->GetOutput()->SetRegions(m_BoundingBox);
364  this->GetOutput()->Allocate();
365 
366  // pipe to the output image
367  extractor->GraftOutput(this->GetOutput());
368  extractor->Update();
369  // also necessary for pipe to output
370  // see itkImageSource for description
371  this->GraftOutput(extractor->GetOutput());
372 
373  // Make sure the image information is correct.
374  // itk::Matrix<double,3,3> I;
375  // I.SetIdentity();
376  // extractor->GetOutput()->SetDirection(I);
377  // double ss[3];
378  // ss[0] = ss[1] = ss[2] = 1.0;
379 
380  // Origin at the center of the image
381  // float o[3];
382  // o[0] = - (extractor->GetOutput()->GetBufferedRegion().GetSize()[0] / 2.0);
383  // o[1] = - (extractor->GetOutput()->GetBufferedRegion().GetSize()[1] / 2.0);
384  // o[2] = - (extractor->GetOutput()->GetBufferedRegion().GetSize()[2] / 2.0);
385 
386  // std::cout << "---" << o[0] << " " << o[1] << " " << o[2] << std::endl;
387 
388  // extractor->GetOutput()->SetOrigin(o);
389  // extractor->GetOutput()->SetSpacing(ss);
390 }
template<class TImage >
void itk::PSMTrimLabelMapImageFilter< TImage >::FillHoles ( ImageType work) const
protected

Fills holes in the foreground segmentation.

Definition at line 186 of file itkPSMTrimLabelMapImageFilter.hxx.

187 {
188  // Find a background value seed at the boundary. Assumption is that
189  // the corner index is background.
190  typename ConnectedThresholdImageFilter<ImageType, ImageType>::IndexType seed;
191  seed.Fill(NumericTraits<PixelType>::Zero);
192 
193  // Flood fill the background with the foreground value.
194  typename itk::ConnectedThresholdImageFilter<ImageType, ImageType >::Pointer
195  ccfilter = itk::ConnectedThresholdImageFilter<ImageType, ImageType>::New();
196  ccfilter->SetInput(work);
197  ccfilter->SetLower(m_BackgroundValue);
198  ccfilter->SetUpper(m_BackgroundValue);
199  ccfilter->SetSeed(seed);
200  ccfilter->SetReplaceValue(m_ForegroundValue);
201  ccfilter->Update();
202 
203  // "or" the original image with the inverse (foreground/background flipped)
204  // of the flood-filled copy.
205  itk::ImageRegionConstIterator<ImageType> rit(ccfilter->GetOutput(),
206  work->GetBufferedRegion());
207  itk::ImageRegionIterator<ImageType> it(work,work->GetBufferedRegion());
208  for (; !rit.IsAtEnd(); ++rit, ++it)
209  {
210  if (rit.Get() == m_BackgroundValue)
211  {
212  it.Set(m_ForegroundValue);
213  }
214  }
215 }
template<class TImage >
void itk::PSMTrimLabelMapImageFilter< TImage >::GenerateData ( )

Do the work of batch-processing the input images.

Definition at line 91 of file itkPSMTrimLabelMapImageFilter.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  // std::cout << "A work regions = " << work->GetBufferedRegion() << std::endl;
100  // std::cout << "A input regions = " << this->GetInput()->GetBufferedRegion() << std::endl;
101 
102  // Isolate largest connected component into the work image.
103  // Modifies the work image.
104  this->IsolateLargestComponent(work);
105 
106  // We are done with inputs. Release them if requested.
107  this->ReleaseInputs();
108 
109  // Fill holes in the working image
110  this->FillHoles(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  // typename ImageFileWriter<ImageType>::Pointer writer = ImageFileWriter<ImageType>::New();
117  // writer->SetInput(work);
118  // writer->SetFileName("tmp.nrrd");
119  // writer->Update();
120 
121  // Finally, crop the image. This method allocates the output image
122  // and grafts the final result to the output image.
123  this->Crop(work);
124 
125  // std::cout << "E work regions = " << work->GetBufferedRegion() << std::endl;
126 
127  // Finally, make sure all the image information is correct: origin
128  // is in the CENTER of the image and the proper spacing information
129  // has been maintained.
130 
131 
132 
133 }
template<class TImage >
void itk::PSMTrimLabelMapImageFilter< 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 itkPSMTrimLabelMapImageFilter.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::PSMTrimLabelMapImageFilter< 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 itkPSMTrimLabelMapImageFilter.hxx.

83 {
84  // call the superclass' implementation of this method
85  Superclass::GenerateOutputInformation();
86 }
template<class TImage>
const RegionType& itk::PSMTrimLabelMapImageFilter< TImage >::GetBoundingBox ( ) const
inline

Returns the bounding box of the centered and cropped foreground label map. Only valid AFTER the filter has been run.

Definition at line 107 of file itkPSMTrimLabelMapImageFilter.h.

108  {
109  return m_BoundingBox;
110  }
template<class TImage >
void itk::PSMTrimLabelMapImageFilter< 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 137 of file itkPSMTrimLabelMapImageFilter.hxx.

138 {
139  // Create a connected component filter
140  typename itk::ConnectedComponentImageFilter<TImage, TImage>::Pointer ccfilter
141  = itk::ConnectedComponentImageFilter<TImage, TImage>::New();
142  ccfilter->SetInput(this->GetInput());
143  ccfilter->FullyConnectedOn();
144  ccfilter->Update();
145 
146  // First find the size of the connected components.
147  std::map<int, int> sizes;
148  ImageRegionConstIterator<TImage> rit(ccfilter->GetOutput(),
149  ccfilter->GetOutput()->GetBufferedRegion());
150  for (; !rit.IsAtEnd(); ++rit)
151  {
152  if (rit.Get() > 0)
153  {
154  sizes[rit.Get()] = sizes[rit.Get()] + 1;
155  }
156  }
157 
158  // Find largest connected component. Assumes connected component
159  // algorithm enumerates sequentially. (This is true for the ITK
160  // algorithm). Copy largest connected component into work image.
161  int maxsize = 0;
162  int bigcomponent = 0;
163  for (typename std::map<int, int>::const_iterator mapit = sizes.begin();
164  mapit != sizes.end(); mapit++)
165  {
166  if ((*mapit).second > maxsize)
167  {
168  bigcomponent = (*mapit).first;
169  maxsize = (*mapit).second;
170  }
171  }
172 
173  // Copy result into work image. Remove all but the largest connected component.
174  ImageRegionIterator<TImage> it(work, work->GetBufferedRegion());
175  rit.GoToBegin();
176  it.GoToBegin();
177  for ( ; ! rit.IsAtEnd(); ++it, ++rit)
178  {
179  if (rit.Get() == bigcomponent) it.Set(m_ForegroundValue);
180  else it.Set(m_BackgroundValue);
181  }
182 }
template<class TImage>
itk::PSMTrimLabelMapImageFilter< TImage >::itkNewMacro ( Self  )

Method for creation through the object factory.

template<class TImage>
itk::PSMTrimLabelMapImageFilter< 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::PSMTrimLabelMapImageFilter< 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::PSMTrimLabelMapImageFilter< TImage >::itkStaticConstMacro ( Dimension  ,
unsigned  int,
TImage::ImageDimension   
)

Dimensionality of the domain of the particle system.

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

Run-time type information (and related methods).


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