| 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 itkNonLocalPatchBasedImageFilter_h |
| 19 |
#define itkNonLocalPatchBasedImageFilter_h |
| 20 |
|
| 21 |
#include <iostream> |
| 22 |
#include "itkImageToImageFilter.h" |
| 23 |
#include "AdaptiveDenoisingExport.h" |
| 24 |
|
| 25 |
#include "itkConstNeighborhoodIterator.h" |
| 26 |
|
| 27 |
namespace itk |
| 28 |
{ |
| 29 |
|
| 30 |
/** |
| 31 |
* \class NonLocalPatchBasedImageFilterEnums |
| 32 |
* \brief Non-local patch-based image filter enum classes. |
| 33 |
* \ingroup AdaptiveDenoising |
| 34 |
*/ |
| 35 |
|
| 36 |
class NonLocalPatchBasedImageFilterEnums |
| 37 |
{ |
| 38 |
public: |
| 39 |
/**\class SimilarityMetric |
| 40 |
* \brief Neighborhood patch similarity metric enumerated type. |
| 41 |
* \ingroup AdaptiveDenoising |
| 42 |
*/ |
| 43 |
enum class SimilarityMetric : uint8_t |
| 44 |
{ |
| 45 |
PEARSON_CORRELATION = 0, |
| 46 |
MEAN_SQUARES = 1 |
| 47 |
}; |
| 48 |
}; |
| 49 |
|
| 50 |
extern AdaptiveDenoising_EXPORT std::ostream & |
| 51 |
operator<<(std::ostream & out, const NonLocalPatchBasedImageFilterEnums::SimilarityMetric value); |
| 52 |
|
| 53 |
|
| 54 |
/** |
| 55 |
* \class NonLocalPatchBasedImageFilter |
| 56 |
* \brief Implementation of a non-local image filter. |
| 57 |
* |
| 58 |
* \ingroup AdaptiveDenoising |
| 59 |
*/ |
| 60 |
|
| 61 |
template <typename TInputImage, typename TOutputImage = TInputImage> |
| 62 |
class ITK_TEMPLATE_EXPORT NonLocalPatchBasedImageFilter : public ImageToImageFilter<TInputImage, TOutputImage> |
| 63 |
{ |
| 64 |
public: |
| 65 |
ITK_DISALLOW_COPY_AND_MOVE(NonLocalPatchBasedImageFilter); |
| 66 |
|
| 67 |
/** Standard class typedefs. */ |
| 68 |
using Self = NonLocalPatchBasedImageFilter<TInputImage, TOutputImage>; |
| 69 |
using Superclass = ImageToImageFilter<TInputImage, TOutputImage>; |
| 70 |
using Pointer = SmartPointer<Self>; |
| 71 |
using ConstPointer = SmartPointer<const Self>; |
| 72 |
|
| 73 |
/** Runtime information support. */ |
| 74 |
itkTypeMacro(NonLocalPatchBasedImageFilter, ImageToImageFilter); |
| 75 |
|
| 76 |
/** Standard New method. */ |
| 77 |
itkNewMacro(Self); |
| 78 |
|
| 79 |
/** ImageDimension constants */ |
| 80 |
itkStaticConstMacro(ImageDimension, unsigned int, TInputImage::ImageDimension); |
| 81 |
|
| 82 |
/** Some convenient typedefs. */ |
| 83 |
using InputImageType = TInputImage; |
| 84 |
using InputPixelType = typename InputImageType::PixelType; |
| 85 |
using InputImagePointer = typename InputImageType::Pointer; |
| 86 |
using InputImageList = std::vector<InputImagePointer>; |
| 87 |
using InputImageSetList = std::vector<InputImageList>; |
| 88 |
using RegionType = typename InputImageType::RegionType; |
| 89 |
|
| 90 |
using OutputImageType = TOutputImage; |
| 91 |
using OutputPixelType = typename OutputImageType::PixelType; |
| 92 |
|
| 93 |
using InputImagePixelVectorType = std::vector<InputPixelType>; |
| 94 |
|
| 95 |
using RealType = float; |
| 96 |
using RealImageType = Image<RealType, ImageDimension>; |
| 97 |
using RealImagePointer = typename RealImageType::Pointer; |
| 98 |
using IndexType = typename RealImageType::IndexType; |
| 99 |
|
| 100 |
using NeighborhoodType = Neighborhood<InputPixelType, ImageDimension>; |
| 101 |
using NeighborhoodSizeType = SizeValueType; |
| 102 |
|
| 103 |
using ConstNeighborhoodIteratorType = ConstNeighborhoodIterator<InputImageType>; |
| 104 |
using NeighborhoodRadiusType = typename ConstNeighborhoodIteratorType::RadiusType; |
| 105 |
using NeighborhoodOffsetType = typename ConstNeighborhoodIteratorType::OffsetType; |
| 106 |
|
| 107 |
using NeighborhoodOffsetListType = std::vector<NeighborhoodOffsetType>; |
| 108 |
|
| 109 |
using SimilarityMetricEnum = NonLocalPatchBasedImageFilterEnums::SimilarityMetric; |
| 110 |
#if !defined(ITK_LEGACY_REMOVE) |
| 111 |
using SimilarityMetricType = SimilarityMetricEnum; |
| 112 |
static constexpr SimilarityMetricType PEARSON_CORRELATION = SimilarityMetricEnum::PEARSON_CORRELATION; |
| 113 |
static constexpr SimilarityMetricType MEAN_SQUARES = SimilarityMetricEnum::MEAN_SQUARES; |
| 114 |
#endif |
| 115 |
|
| 116 |
/** |
| 117 |
* Get/set neighborhood search radius. |
| 118 |
* Default = 3x3x... |
| 119 |
*/ |
| 120 |
itkSetMacro(NeighborhoodSearchRadius, NeighborhoodRadiusType); |
| 121 |
itkGetConstMacro(NeighborhoodSearchRadius, NeighborhoodRadiusType); |
| 122 |
|
| 123 |
/** |
| 124 |
* Get/set neighborhood search size. |
| 125 |
*/ |
| 126 |
itkSetMacro(NeighborhoodSearchSize, NeighborhoodSizeType); |
| 127 |
itkGetConstMacro(NeighborhoodSearchSize, NeighborhoodSizeType); |
| 128 |
|
| 129 |
/** |
| 130 |
* Get/set neighborhood search offset list. |
| 131 |
*/ |
| 132 |
virtual void |
| 133 |
SetNeighborhoodSearchOffsetList(const NeighborhoodOffsetListType list) |
| 134 |
{ |
| 135 |
this->m_NeighborhoodSearchOffsetList = list; |
| 136 |
this->Modified(); |
| 137 |
} |
| 138 |
itkGetConstMacro(NeighborhoodSearchOffsetList, NeighborhoodOffsetListType); |
| 139 |
|
| 140 |
/** |
| 141 |
* Get/set neighborhood patch radius. |
| 142 |
* Default = 1x1x... |
| 143 |
*/ |
| 144 |
itkSetMacro(NeighborhoodPatchRadius, NeighborhoodRadiusType); |
| 145 |
itkGetConstMacro(NeighborhoodPatchRadius, NeighborhoodRadiusType); |
| 146 |
|
| 147 |
/** |
| 148 |
* Get/set neighborhood patch size. |
| 149 |
*/ |
| 150 |
itkSetMacro(NeighborhoodPatchSize, NeighborhoodSizeType); |
| 151 |
itkGetConstMacro(NeighborhoodPatchSize, NeighborhoodSizeType); |
| 152 |
|
| 153 |
/** |
| 154 |
* Get/set neighborhood patch offset list. |
| 155 |
*/ |
| 156 |
virtual void |
| 157 |
SetNeighborhoodPatchOffsetList(const NeighborhoodOffsetListType list) |
| 158 |
{ |
| 159 |
this->m_NeighborhoodPatchOffsetList = list; |
| 160 |
this->Modified(); |
| 161 |
} |
| 162 |
itkGetConstMacro(NeighborhoodPatchOffsetList, NeighborhoodOffsetListType); |
| 163 |
|
| 164 |
/** |
| 165 |
* Enumerated type for neighborhood similarity. Default = MEAN_SQUARES |
| 166 |
*/ |
| 167 |
itkSetMacro(SimilarityMetric, SimilarityMetricEnum); |
| 168 |
itkGetConstMacro(SimilarityMetric, SimilarityMetricEnum); |
| 169 |
|
| 170 |
protected: |
| 171 |
NonLocalPatchBasedImageFilter(); |
| 172 |
~NonLocalPatchBasedImageFilter() override = default; |
| 173 |
|
| 174 |
void |
| 175 |
BeforeThreadedGenerateData() override; |
| 176 |
|
| 177 |
void |
| 178 |
PrintSelf(std::ostream & os, Indent indent) const override; |
| 179 |
|
| 180 |
RealType |
| 181 |
ComputeNeighborhoodPatchSimilarity(const InputImageList &, |
| 182 |
const IndexType, |
| 183 |
const InputImagePixelVectorType &, |
| 184 |
const bool); |
| 185 |
|
| 186 |
InputImagePixelVectorType |
| 187 |
VectorizeImageListPatch(const InputImageList &, const IndexType, const bool); |
| 188 |
|
| 189 |
InputImagePixelVectorType |
| 190 |
VectorizeImagePatch(const InputImagePointer, const IndexType, const bool); |
| 191 |
|
| 192 |
void |
| 193 |
GetMeanAndStandardDeviationOfVectorizedImagePatch(const InputImagePixelVectorType &, RealType &, RealType &); |
| 194 |
|
| 195 |
itkSetMacro(TargetImageRegion, RegionType); |
| 196 |
itkGetConstMacro(TargetImageRegion, RegionType); |
| 197 |
|
| 198 |
SimilarityMetricEnum m_SimilarityMetric; |
| 199 |
|
| 200 |
SizeValueType m_NeighborhoodSearchSize; |
| 201 |
NeighborhoodRadiusType m_NeighborhoodSearchRadius; |
| 202 |
NeighborhoodOffsetListType m_NeighborhoodSearchOffsetList; |
| 203 |
|
| 204 |
SizeValueType m_NeighborhoodPatchSize; |
| 205 |
NeighborhoodRadiusType m_NeighborhoodPatchRadius; |
| 206 |
NeighborhoodOffsetListType m_NeighborhoodPatchOffsetList; |
| 207 |
|
| 208 |
RegionType m_TargetImageRegion; |
| 209 |
}; |
| 210 |
|
| 211 |
} // end namespace itk |
| 212 |
|
| 213 |
#ifndef ITK_MANUAL_INSTANTIATION |
| 214 |
# include "itkNonLocalPatchBasedImageFilter.hxx" |
| 215 |
#endif |
| 216 |
|
| 217 |
#endif |