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 itkMaskedRankImageFilter_h 19 #define itkMaskedRankImageFilter_h 20 21 #include "itkMaskedMovingHistogramImageFilter.h" 22 #include <list> 23 #include <map> 24 #include <set> 25 #include "itkRankHistogram.h" 26 #include "itkFlatStructuringElement.h" 27 28 namespace itk 29 { 30 /** 31 * \class MaskedRankImageFilter 32 * \brief Rank filter of a greyscale image 33 * 34 * Nonlinear filter in which each output pixel is a user defined 35 * rank of input pixels in a user defined neighborhood. The default 36 * rank is 0.5 (median). The boundary conditions are different to the 37 * standard itkMedianImageFilter. In this filter the neighborhood is 38 * cropped at the boundary, and is therefore smaller. 39 * 40 * This filter uses a recursive implementation - essentially the one 41 * by Huang 1979, I believe, to compute the rank, 42 * and is therefore usually a lot faster than the direct 43 * implementation. The extensions to Huang are support for arbitrary 44 * pixel types (using c++ maps) and arbitrary neighborhoods. I presume 45 * that these are not new ideas. 46 * 47 * This filter is based on the sliding window code from the 48 * consolidatedMorphology package on InsightJournal. 49 * 50 * The structuring element is assumed to be composed of binary 51 * values (zero or one). Only elements of the structuring element 52 * having values > 0 are candidates for affecting the center pixel. 53 * 54 * This code was contributed in the Insight Journal paper: 55 * "Efficient implementation of kernel filtering" 56 * by Beare R., Lehmann G 57 * https://hdl.handle.net/1926/555 58 * http://www.insight-journal.org/browse/publication/160 59 * 60 * \author Richard Beare 61 * \ingroup ITKMathematicalMorphology 62 */ 63 64 template< typename TInputImage, typename TMaskImage, typename TOutputImage, typename TKernel = 65 FlatStructuringElement< TInputImage::ImageDimension > > 66 class ITK_TEMPLATE_EXPORT MaskedRankImageFilter: 67 public MaskedMovingHistogramImageFilter< TInputImage, TMaskImage, TOutputImage, TKernel, 68 Function::RankHistogram< typename TInputImage::PixelType > > 69 { 70 public: 71 ITK_DISALLOW_COPY_AND_ASSIGN(MaskedRankImageFilter); 72 73 /** Standard class type aliases. */ 74 using Self = MaskedRankImageFilter; 75 using Superclass = MaskedMovingHistogramImageFilter< TInputImage, TMaskImage, TOutputImage, TKernel, 76 Function::RankHistogram< typename TInputImage::PixelType > >; 77 78 using Pointer = SmartPointer< Self >; 79 using ConstPointer = SmartPointer< const Self >; 80 81 /** Standard New method. */ 82 itkNewMacro(Self); 83 84 /** Runtime information support. */ 85 itkTypeMacro(MaskedRankImageFilter, 86 MovingHistogramImageFilter); 87 88 /** Image related type alias. */ 89 using InputImageType = TInputImage; 90 using OutputImageType = TOutputImage; 91 using RegionType = typename TInputImage::RegionType; 92 using SizeType = typename TInputImage::SizeType; 93 using IndexType = typename TInputImage::IndexType; 94 using PixelType = typename TInputImage::PixelType; 95 using OffsetType = typename TInputImage::OffsetType; 96 using OutputImageRegionType = typename Superclass::OutputImageRegionType; 97 using OutputPixelType = typename TOutputImage::PixelType; 98 using InputPixelType = typename TInputImage::PixelType; 99 100 using HistogramType = typename Superclass::HistogramType; 101 102 /** Image related type alias. */ 103 static constexpr unsigned int ImageDimension = TInputImage::ImageDimension; 104 105 /** Kernel type alias. */ 106 using KernelType = TKernel; 107 108 /** Kernel (structuring element) iterator. */ 109 using KernelIteratorType = typename KernelType::ConstIterator; 110 111 /** n-dimensional Kernel radius. */ 112 using RadiusType = typename KernelType::SizeType; 113 114 itkSetClampMacro(Rank, float, 0.0, 1.0); itkGetConstMacro(Rank,float)115 itkGetConstMacro(Rank, float) 116 117 bool GetUseVectorBasedAlgorithm() const 118 { 119 return HistogramType::UseVectorBasedAlgorithm(); 120 } 121 122 protected: 123 MaskedRankImageFilter(); 124 ~MaskedRankImageFilter() override = default; 125 126 void PrintSelf(std::ostream & os, Indent indent) const override; 127 128 void ConfigureHistogram( HistogramType & histogram ) override; 129 130 private: 131 float m_Rank; 132 }; // end of class 133 } // end namespace itk 134 135 #ifndef ITK_MANUAL_INSTANTIATION 136 #include "itkMaskedRankImageFilter.hxx" 137 #endif 138 139 #endif 140