ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkImageMomentsCalculator.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkImageMomentsCalculator_h
19 #define itkImageMomentsCalculator_h
20 
21 #include "itkAffineTransform.h"
22 #include "itkImage.h"
23 #include "itkSpatialObject.h"
24 
25 #include "vnl/vnl_vector_fixed.h"
26 #include "vnl/vnl_matrix_fixed.h"
27 #include "vnl/vnl_diag_matrix.h"
28 
29 namespace itk
30 {
59 template< typename TImage >
60 class ITK_TEMPLATE_EXPORT ImageMomentsCalculator:public Object
61 {
62 public:
63  ITK_DISALLOW_COPY_AND_ASSIGN(ImageMomentsCalculator);
64 
67  using Superclass = Object;
70 
72  itkNewMacro(Self);
73 
75  itkTypeMacro(ImageMomentsCalculator, Object);
76 
78  static constexpr unsigned int ImageDimension = TImage::ImageDimension;
79 
81  using ScalarType = double;
82 
85 
88 
92 
94  using MatrixType = Matrix< ScalarType,
95  Self::ImageDimension,
96  Self::ImageDimension >;
97 
99  using ImageType = TImage;
100 
102  using ImagePointer = typename ImageType::Pointer;
103  using ImageConstPointer = typename ImageType::ConstPointer;
104 
108 
110  virtual void SetImage(const ImageType *image)
111  {
112  if ( m_Image != image )
113  {
114  m_Image = image;
115  this->Modified();
116  m_Valid = false;
117  }
118  }
120 
123  {
124  if ( m_SpatialObjectMask != so )
125  {
126  m_SpatialObjectMask = so;
127  this->Modified();
128  m_Valid = false;
129  }
130  }
132 
138  void Compute();
139 
144  ScalarType GetTotalMass() const;
145 
151  VectorType GetFirstMoments() const;
152 
158  MatrixType GetSecondMoments() const;
159 
164  VectorType GetCenterOfGravity() const;
165 
170  MatrixType GetCentralMoments() const;
171 
178  VectorType GetPrincipalMoments() const;
179 
192  MatrixType GetPrincipalAxes() const;
194 
198  AffineTransformPointer GetPrincipalAxesToPhysicalAxesTransform() const;
199 
204  AffineTransformPointer GetPhysicalAxesToPrincipalAxesTransform() const;
205 
206 protected:
208  ~ImageMomentsCalculator() override = default;
209  void PrintSelf(std::ostream & os, Indent indent) const override;
210 
211 private:
212  bool m_Valid; // Have moments been computed yet?
213  ScalarType m_M0; // Zeroth moment
214  VectorType m_M1; // First moments about origin
215  MatrixType m_M2; // Second moments about origin
216  VectorType m_Cg; // Center of gravity (physical units)
217  MatrixType m_Cm; // Second central moments (physical)
218  VectorType m_Pm; // Principal moments (physical)
219  MatrixType m_Pa; // Principal axes (physical)
220 
223 }; // class ImageMomentsCalculator
224 } // end namespace itk
225 
226 #ifndef ITK_MANUAL_INSTANTIATION
227 #include "itkImageMomentsCalculator.hxx"
228 #endif
229 
230 #endif /* itkImageMomentsCalculator_h */
typename ImageType::Pointer ImagePointer
Light weight base class for most itk classes.
typename SpatialObjectType::ConstPointer SpatialObjectConstPointer
typename SpatialObjectType::Pointer SpatialObjectPointer
SpatialObjectConstPointer m_SpatialObjectMask
Compute moments of an n-dimensional image.
Implementation of the composite pattern.
typename AffineTransformType::Pointer AffineTransformPointer
virtual void SetSpatialObjectMask(const SpatialObject< Self::ImageDimension > *so)
Control indentation during Print() invocation.
Definition: itkIndent.h:49
virtual void SetImage(const ImageType *image)
Base class for most ITK classes.
Definition: itkObject.h:60
typename ImageType::ConstPointer ImageConstPointer