Debian Codegraph @

include/itkNonLocalPatchBasedImageFilter.hxx

raw symbols SCIP
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_hxx
19 #define itkNonLocalPatchBasedImageFilter_hxx
20
21
22 #include "itkNeighborhood.h"
23
24 namespace itk
25 {
26
27 template <typename TInputImage, typename TOutputImage>
28 NonLocalPatchBasedImageFilter<TInputImage, TOutputImage>::NonLocalPatchBasedImageFilter()
29 {
30 this->m_SimilarityMetric = SimilarityMetricEnum::MEAN_SQUARES;
31
32 this->m_NeighborhoodPatchRadius.Fill(1);
33 this->m_NeighborhoodPatchOffsetList.clear();
34
35 this->m_NeighborhoodSearchRadius.Fill(3);
36 this->m_NeighborhoodSearchOffsetList.clear();
37 }
38
39 template <typename TInputImage, typename TOutputImage>
40 void
41 NonLocalPatchBasedImageFilter<TInputImage, TOutputImage>::BeforeThreadedGenerateData()
42 {
43 // Set up the search neighborhood parameters
44
45 this->m_NeighborhoodSearchOffsetList.clear();
46
47 NeighborhoodType searchNeighborhood;
48 searchNeighborhood.SetRadius(this->m_NeighborhoodSearchRadius);
49
50 this->m_NeighborhoodSearchSize = searchNeighborhood.Size();
51 for (unsigned int n = 0; n < this->m_NeighborhoodSearchSize; n++)
52 {
53 this->m_NeighborhoodSearchOffsetList.push_back(searchNeighborhood.GetOffset(n));
54 }
55
56 // Set up the patch neighborhood parameters
57
58 this->m_NeighborhoodPatchOffsetList.clear();
59
60 NeighborhoodType patchNeighborhood;
61 patchNeighborhood.SetRadius(this->m_NeighborhoodPatchRadius);
62
63 this->m_NeighborhoodPatchSize = patchNeighborhood.Size();
64 for (unsigned int n = 0; n < this->m_NeighborhoodPatchSize; n++)
65 {
66 this->m_NeighborhoodPatchOffsetList.push_back(patchNeighborhood.GetOffset(n));
67 }
68
69 this->m_TargetImageRegion = this->GetInput()->GetRequestedRegion();
70 }
71
72 template <typename TInputImage, typename TOutputImage>
73 typename NonLocalPatchBasedImageFilter<TInputImage, TOutputImage>::InputImagePixelVectorType
74 NonLocalPatchBasedImageFilter<TInputImage, TOutputImage>::VectorizeImageListPatch(const InputImageList & imageList,
75 const IndexType index,
76 const bool normalize)
77 {
78 InputImagePixelVectorType patchVector(this->m_NeighborhoodPatchSize * imageList.size());
79 for (unsigned int i = 0; i < imageList.size(); i++)
80 {
81 InputImagePixelVectorType patchVectorPerModality = this->VectorizeImagePatch(imageList[i], index, normalize);
82 for (unsigned int j = 0; j < this->m_NeighborhoodPatchSize; j++)
83 {
84 patchVector[i * this->m_NeighborhoodPatchSize + j] = patchVectorPerModality[j];
85 }
86 }
87 return patchVector;
88 }
89
90 template <typename TInputImage, typename TOutputImage>
91 typename NonLocalPatchBasedImageFilter<TInputImage, TOutputImage>::InputImagePixelVectorType
92 NonLocalPatchBasedImageFilter<TInputImage, TOutputImage>::VectorizeImagePatch(const InputImagePointer image,
93 const IndexType index,
94 const bool normalize)
95 {
96 InputImagePixelVectorType patchVector(this->m_NeighborhoodPatchSize);
97 for (SizeValueType i = 0; i < this->m_NeighborhoodPatchSize; i++)
98 {
99 IndexType neighborhoodIndex = index + this->m_NeighborhoodPatchOffsetList[i];
100
101 bool isInBounds = this->m_TargetImageRegion.IsInside(neighborhoodIndex);
102 if (isInBounds)
103 {
104 InputPixelType pixel = image->GetPixel(neighborhoodIndex);
105 patchVector[i] = pixel;
106 }
107 else
108 {
109 patchVector[i] = std::numeric_limits<RealType>::quiet_NaN();
110 }
111 }
112
113 if (normalize)
114 {
115 // Convert the current image patch to a z-score.
116 RealType mean = 0.0;
117 RealType standardDeviation = 0.0;
118 this->GetMeanAndStandardDeviationOfVectorizedImagePatch(patchVector, mean, standardDeviation);
119 if (standardDeviation < NumericTraits<RealType>::epsilon())
120 {
121 for (auto & it : patchVector)
122 {
123 it = 0.;
124 }
125 }
126 const auto inv_standardDeviation = 1. / standardDeviation;
127 for (auto & it : patchVector)
128 {
129 it = (it - mean) * inv_standardDeviation;
130 }
131 }
132 return patchVector;
133 }
134
135 template <typename TInputImage, typename TOutputImage>
136 void
137 NonLocalPatchBasedImageFilter<TInputImage, TOutputImage>::GetMeanAndStandardDeviationOfVectorizedImagePatch(
138 const InputImagePixelVectorType & patchVector,
139 RealType & mean,
140 RealType & standardDeviation)
141 {
142 RealType sum = 0.0;
143 RealType sumOfSquares = 0.0;
144 RealType count = 0.0;
145
146 for (const auto it : patchVector)
147 {
148 if (std::isfinite(it)) // Silently skip non-finite values used to indicate
149 // out-of-bounds.
150 {
151 sum += it;
152 sumOfSquares += itk::Math::sqr(it);
153 count += itk::NumericTraits<RealType>::OneValue();
154 }
155 }
156 mean = sum / count;
157 standardDeviation =
158 std::sqrt((sumOfSquares - count * itk::Math::sqr(mean)) / (count - itk::NumericTraits<RealType>::OneValue()));
159 }
160
161 template <typename TInputImage, typename TOutputImage>
162 typename NonLocalPatchBasedImageFilter<TInputImage, TOutputImage>::RealType
163 NonLocalPatchBasedImageFilter<TInputImage, TOutputImage>::ComputeNeighborhoodPatchSimilarity(
164 const InputImageList & imageList,
165 const IndexType index,
166 const InputImagePixelVectorType & patchVectorY,
167 const bool useOnlyFirstImage)
168 {
169 unsigned int numberOfImagesToUse = imageList.size();
170 if (useOnlyFirstImage)
171 {
172 numberOfImagesToUse = 1;
173 }
174
175 RealType sumX = 0.0;
176 RealType sumOfSquaresX = 0.0;
177 RealType sumOfSquaredDifferencesXY = 0.0;
178 RealType sumXY = 0.0;
179 RealType N = 0.0;
180
181 SizeValueType count = 0;
182 for (SizeValueType i = 0; i < numberOfImagesToUse; i++)
183 {
184 for (SizeValueType j = 0; j < this->m_NeighborhoodPatchSize; j++)
185 {
186 IndexType neighborhoodIndex = index + this->m_NeighborhoodPatchOffsetList[j];
187
188 bool isInBounds = this->m_TargetImageRegion.IsInside(neighborhoodIndex);
189 if (isInBounds && std::isfinite(patchVectorY[count]))
190 {
191 auto x = static_cast<RealType>(imageList[i]->GetPixel(neighborhoodIndex));
192 auto y = static_cast<RealType>(patchVectorY[count]);
193
194 sumX += x;
195 sumOfSquaresX += itk::Math::sqr(x);
196 sumXY += (x * y);
197
198 sumOfSquaredDifferencesXY += itk::Math::sqr(y - x);
199 N += itk::NumericTraits<RealType>::OneValue();
200 }
201 ++count;
202 }
203 }
204
205 // If we are on the boundary, a neighborhood patch might not overlap
206 // with the image. If we have 2 voxels or less for a neighborhood patch
207 // we don't consider it to be a suitable match.
208 if (N < static_cast<RealType>(3.0))
209 {
210 return NumericTraits<RealType>::max();
211 }
212 if (this->m_SimilarityMetric == SimilarityMetricEnum::PEARSON_CORRELATION)
213 {
214 const RealType varianceX = sumOfSquaresX - itk::Math::sqr(sumX) / N;
215 const RealType measure =
216 (varianceX > std::numeric_limits<RealType>::epsilon()) ? itk::Math::sqr(sumXY) / varianceX : 0.;
217 if (sumXY > 0)
218 {
219 return -measure;
220 }
221 else
222 {
223 return measure;
224 }
225 }
226 else if (this->m_SimilarityMetric == SimilarityMetricEnum::MEAN_SQUARES)
227 {
228 return (sumOfSquaredDifferencesXY / N);
229 }
230 else
231 {
232 itkExceptionMacro("Unrecognized similarity metric.");
233 }
234 }
235
236 template <typename TInputImage, typename TOutputImage>
237 void
238 NonLocalPatchBasedImageFilter<TInputImage, TOutputImage>::PrintSelf(std::ostream & os, Indent indent) const
239 {
240 Superclass::PrintSelf(os, indent);
241
242 if (this->m_SimilarityMetric == SimilarityMetricEnum::PEARSON_CORRELATION)
243 {
244 os << "Using Pearson correlation to measure the patch similarity." << std::endl;
245 }
246 else if (this->m_SimilarityMetric == SimilarityMetricEnum::MEAN_SQUARES)
247 {
248 os << "Using mean squares to measure the patch similarity." << std::endl;
249 }
250
251 os << indent << "Neighborhood search radius = " << this->m_NeighborhoodSearchRadius << std::endl;
252 os << indent << "Neighborhood patch radius = " << this->m_NeighborhoodPatchRadius << std::endl;
253 }
254
255 } // end namespace itk
256
257 #endif