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 itkSpatialObjectToImageStatisticsCalculator_h 19 #define itkSpatialObjectToImageStatisticsCalculator_h 20 21 #include "itkObject.h" 22 #include "itkMatrix.h" 23 #include "itkNumericTraits.h" 24 #include "itkListSample.h" 25 #include "itkVector.h" 26 27 namespace itk 28 { 29 /** \class SpatialObjectToImageStatisticsCalculator 30 * This calculator computes the mean and the covariance matrice of a certain 31 * region of an image specified by a spatial object. 32 * \ingroup Operators 33 * \ingroup ITKSpatialObjects 34 */ 35 template< typename TInputImage, typename TInputSpatialObject, unsigned int TSampleDimension = 1 > 36 class ITK_TEMPLATE_EXPORT SpatialObjectToImageStatisticsCalculator:public Object 37 { 38 public: 39 ITK_DISALLOW_COPY_AND_ASSIGN(SpatialObjectToImageStatisticsCalculator); 40 41 /** Standard class type aliases. */ 42 using Self = SpatialObjectToImageStatisticsCalculator; 43 using Superclass = Object; 44 using Pointer = SmartPointer< Self >; 45 using ConstPointer = SmartPointer< const Self >; 46 47 /** Method for creation through the object factory. */ 48 itkNewMacro(Self); 49 50 /** Run-time type information (and related methods). */ 51 itkTypeMacro(SpatialObjectToImageStatisticsCalculator, Object); 52 53 /** Type definitions for the input image. */ 54 using ImageType = TInputImage; 55 using ImagePointer = typename TInputImage::Pointer; 56 using ImageConstPointer = typename TInputImage::ConstPointer; 57 using PixelType = typename TInputImage::PixelType; 58 using IndexType = typename TInputImage::IndexType; 59 using PointType = typename TInputImage::PointType; 60 using RegionType = typename TInputImage::RegionType; 61 using SizeType = typename RegionType::SizeType; 62 63 using AccumulateType = typename NumericTraits< PixelType >::AccumulateType; 64 65 static constexpr unsigned int ImageDimension = ImageType::ImageDimension; 66 67 static constexpr unsigned int SampleDimension = TSampleDimension; 68 69 static constexpr unsigned int ObjectDimension = TInputSpatialObject::ObjectDimension; 70 71 /** Type definitions for the input spatial object. */ 72 using SpatialObjectType = TInputSpatialObject; 73 using SpatialObjectPointer = typename SpatialObjectType::Pointer; 74 using SpatialObjectConstPointer = typename SpatialObjectType::ConstPointer; 75 76 /** Vector and Matrix Type */ 77 using VectorType = Vector< double, TSampleDimension >; 78 using MatrixType = Matrix< double, TSampleDimension, TSampleDimension >; 79 80 /** Type definitions for the samples */ 81 using SampleType = itk::Statistics::ListSample< VectorType >; 82 83 /** Set/Get the direction of the sample */ 84 itkSetMacro(SampleDirection, unsigned int); 85 itkGetConstMacro(SampleDirection, unsigned int); 86 87 /** Set the input image. */ 88 itkSetConstObjectMacro(Image, ImageType); 89 90 /** Set the input spatial object. */ 91 itkSetObjectMacro(SpatialObject, SpatialObjectType); 92 93 /** Get the mean */ GetMean()94 const VectorType & GetMean() const { return m_Mean; } 95 96 /** Get the covariance matrix */ GetCovarianceMatrix()97 const MatrixType & GetCovarianceMatrix() const { return m_CovarianceMatrix; } 98 99 /** Get the sum of pixels */ GetSum()100 AccumulateType GetSum() const { return m_Sum; } 101 102 /** Get the number of pixels inside the object */ 103 itkGetConstMacro(NumberOfPixels, SizeValueType); 104 105 /** Compute of the input image. */ 106 void Update(); 107 108 protected: 109 SpatialObjectToImageStatisticsCalculator(); 110 ~SpatialObjectToImageStatisticsCalculator() override = default; 111 void PrintSelf(std::ostream & os, Indent indent) const override; 112 113 bool ComputeStatistics(); 114 115 private: 116 ImageConstPointer m_Image; 117 SpatialObjectPointer m_SpatialObject; 118 VectorType m_Mean; 119 AccumulateType m_Sum; 120 SizeValueType m_NumberOfPixels; 121 MatrixType m_CovarianceMatrix; 122 unsigned int m_SampleDirection; 123 ModifiedTimeType m_InternalImageTime; 124 ModifiedTimeType m_InternalSpatialObjectTime; 125 TimeStamp m_ModifiedTime; 126 127 typename SampleType::Pointer m_Sample; 128 }; 129 } // end namespace itk 130 131 #ifndef ITK_MANUAL_INSTANTIATION 132 #include "itkSpatialObjectToImageStatisticsCalculator.hxx" 133 #endif 134 135 #endif /* itkSpatialObjectToImageStatisticsCalculator_h */ 136