|
| 1 | +/*========================================================================= |
| 2 | + * |
| 3 | + * Copyright NumFOCUS |
| 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 | + * https://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 itkNMinimaMaximaImageCalculator_h |
| 19 | +#define itkNMinimaMaximaImageCalculator_h |
| 20 | + |
| 21 | +#include "itkMacro.h" |
| 22 | +#include "itkObject.h" |
| 23 | +#include "itkObjectFactory.h" |
| 24 | +#include <mutex> |
| 25 | +#include <vector> |
| 26 | + |
| 27 | +namespace itk |
| 28 | +{ |
| 29 | +/** \class NMinimaMaximaImageCalculator |
| 30 | + * \brief Computes the N highest and/or lowest intensity values of an image. |
| 31 | + * |
| 32 | + * This class is templated over input image type. If only Maxima or |
| 33 | + * Minima are needed, just call ComputeMaxima() or ComputeMinima(). |
| 34 | + * Compute() will compute both. |
| 35 | + * |
| 36 | + * \ingroup Operators |
| 37 | + * \ingroup ITKCommon |
| 38 | + * \ingroup Montage |
| 39 | + */ |
| 40 | +template <typename TInputImage> |
| 41 | +class ITK_TEMPLATE_EXPORT NMinimaMaximaImageCalculator : public Object |
| 42 | +{ |
| 43 | +public: |
| 44 | + ITK_DISALLOW_COPY_AND_MOVE(NMinimaMaximaImageCalculator); |
| 45 | + |
| 46 | + /** Standard class type aliases. */ |
| 47 | + using Self = NMinimaMaximaImageCalculator; |
| 48 | + using Superclass = Object; |
| 49 | + using Pointer = SmartPointer<Self>; |
| 50 | + using ConstPointer = SmartPointer<const Self>; |
| 51 | + |
| 52 | + /** Method for creation through the object factory. */ |
| 53 | + itkNewMacro(Self); |
| 54 | + |
| 55 | + /** Run-time type information (and related methods). */ |
| 56 | + itkOverrideGetNameOfClassMacro(NMinimaMaximaImageCalculator); |
| 57 | + |
| 58 | + /** Type definition for the input image. */ |
| 59 | + using ImageType = TInputImage; |
| 60 | + |
| 61 | + /** Pointer type for the image. */ |
| 62 | + using ImagePointer = typename TInputImage::Pointer; |
| 63 | + |
| 64 | + /** Const Pointer type for the image. */ |
| 65 | + using ImageConstPointer = typename TInputImage::ConstPointer; |
| 66 | + |
| 67 | + /** Type definition for the input image pixel type. */ |
| 68 | + using PixelType = typename TInputImage::PixelType; |
| 69 | + |
| 70 | + /** Image dimensionality */ |
| 71 | + static constexpr unsigned int ImageDimension = TInputImage::ImageDimension; |
| 72 | + // constexpr unsigned ImageDimension = TInputImage::VImageDimension; |
| 73 | + |
| 74 | + /** Type definition for the input image index type. */ |
| 75 | + using IndexType = typename TInputImage::IndexType; |
| 76 | + |
| 77 | + /** Type definition for the input image region type. */ |
| 78 | + using RegionType = typename TInputImage::RegionType; |
| 79 | + |
| 80 | + /** Sorted vector of minima or maxima. */ |
| 81 | + using ValueVector = std::vector<PixelType>; |
| 82 | + |
| 83 | + /** Sorted vector of pixel indices of minima or maxima. */ |
| 84 | + using IndexVector = std::vector<IndexType>; |
| 85 | + |
| 86 | + |
| 87 | + /** Set the input image. */ |
| 88 | + itkSetConstObjectMacro(Image, ImageType); |
| 89 | + |
| 90 | + /** Compute the minimum value of intensity of the input image. */ |
| 91 | + void |
| 92 | + ComputeMinima(); |
| 93 | + |
| 94 | + /** Compute the maximum value of intensity of the input image. */ |
| 95 | + void |
| 96 | + ComputeMaxima(); |
| 97 | + |
| 98 | + /** Compute the minimum and maximum values of intensity of the input image. */ |
| 99 | + void |
| 100 | + Compute(); |
| 101 | + |
| 102 | + /** Return the N minimum intensity values. */ |
| 103 | + itkGetConstReferenceMacro(Minima, ValueVector); |
| 104 | + |
| 105 | + /** Return the N maximum intensity values. */ |
| 106 | + itkGetConstReferenceMacro(Maxima, ValueVector); |
| 107 | + |
| 108 | + /** Return the indices of the N minimum intensity values. */ |
| 109 | + itkGetConstReferenceMacro(IndicesOfMinima, IndexVector); |
| 110 | + |
| 111 | + /** Return the indices of the N maximum intensity values. */ |
| 112 | + itkGetConstReferenceMacro(IndicesOfMaxima, IndexVector); |
| 113 | + |
| 114 | + /** Set the region over which the values will be computed */ |
| 115 | + void |
| 116 | + SetRegion(const RegionType & region); |
| 117 | + |
| 118 | + /** Get/Set the number of extreme intensity values to keep. */ |
| 119 | + itkGetConstMacro(N, SizeValueType); |
| 120 | + itkSetMacro(N, SizeValueType); |
| 121 | + |
| 122 | +protected: |
| 123 | + NMinimaMaximaImageCalculator(); |
| 124 | + ~NMinimaMaximaImageCalculator() override = default; |
| 125 | + void |
| 126 | + PrintSelf(std::ostream & os, Indent indent) const override; |
| 127 | + |
| 128 | + template <typename TComparator = std::less<PixelType>> |
| 129 | + void |
| 130 | + SortedInsert(ValueVector & vals, |
| 131 | + IndexVector & indices, |
| 132 | + const PixelType & val, |
| 133 | + const IndexType & ind, |
| 134 | + TComparator comp = TComparator()); |
| 135 | + void |
| 136 | + InternalCompute(); |
| 137 | + |
| 138 | +private: |
| 139 | + ImageConstPointer m_Image; |
| 140 | + ValueVector m_Minima; |
| 141 | + ValueVector m_Maxima; |
| 142 | + IndexVector m_IndicesOfMinima; |
| 143 | + IndexVector m_IndicesOfMaxima; |
| 144 | + SizeValueType m_N{ 7 }; |
| 145 | + |
| 146 | + RegionType m_Region; |
| 147 | + bool m_RegionSetByUser{ false }; |
| 148 | + bool m_ComputeMaxima{ true }; |
| 149 | + bool m_ComputeMinima{ true }; |
| 150 | + std::mutex m_Mutex; |
| 151 | +}; |
| 152 | +} // end namespace itk |
| 153 | + |
| 154 | +#ifndef ITK_MANUAL_INSTANTIATION |
| 155 | +# include "itkNMinimaMaximaImageCalculator.hxx" |
| 156 | +#endif |
| 157 | + |
| 158 | +#endif /* itkNMinimaMaximaImageCalculator_h */ |
0 commit comments