-
Notifications
You must be signed in to change notification settings - Fork 20
/
itkSpectra1DImageFilter.hxx
341 lines (300 loc) · 13.9 KB
/
itkSpectra1DImageFilter.hxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef itkSpectra1DImageFilter_hxx
#define itkSpectra1DImageFilter_hxx
#include "itkImageLinearConstIteratorWithIndex.h"
#include "itkImageLinearIteratorWithIndex.h"
#include "itkImageRegionConstIterator.h"
#include "itkImageScanlineIterator.h"
#include "itkImageScanlineConstIterator.h"
#include "itkMetaDataObject.h"
#include "itkSpectra1DSupportWindowImageFilter.h"
namespace itk
{
template <typename TInputImage, typename TSupportWindowImage, typename TOutputImage>
Spectra1DImageFilter<TInputImage, TSupportWindowImage, TOutputImage>::Spectra1DImageFilter()
{
this->AddRequiredInputName("SupportWindowImage");
this->DynamicMultiThreadingOff();
}
template <typename TInputImage, typename TSupportWindowImage, typename TOutputImage>
void
Spectra1DImageFilter<TInputImage, TSupportWindowImage, TOutputImage>::GenerateOutputInformation()
{
Superclass::GenerateOutputInformation();
OutputImageType * output = this->GetOutput();
const SupportWindowImageType * supportWindowImage = this->GetSupportWindowImage();
// Support windows inform output image region and metadata
output->SetSpacing(supportWindowImage->GetSpacing());
output->SetLargestPossibleRegion(supportWindowImage->GetLargestPossibleRegion());
const MetaDataDictionary & dict = supportWindowImage->GetMetaDataDictionary();
FFT1DSizeType fft1DSize = 32;
ExposeMetaData<FFT1DSizeType>(dict, "FFT1DSize", fft1DSize);
// Number of frequency bins represented by each vector pixel.
// Divide by two for Hermitian symmetry. Divide by two for Welch's method
// with 50% overlap. Subtract one for discarding DC component.
const FFT1DSizeType spectraComponents = fft1DSize / 2 / 2 - 1;
output->SetVectorLength(spectraComponents);
}
template <typename TInputImage, typename TSupportWindowImage, typename TOutputImage>
void
Spectra1DImageFilter<TInputImage, TSupportWindowImage, TOutputImage>::BeforeThreadedGenerateData()
{
const SupportWindowImageType * supportWindowImage = this->GetSupportWindowImage();
const MetaDataDictionary & dict = supportWindowImage->GetMetaDataDictionary();
FFT1DSizeType fft1DSize = 32;
ExposeMetaData<FFT1DSizeType>(dict, "FFT1DSize", fft1DSize);
// Divide by two for Hermitian symmetry. Divide by two for Welch's method
// with 50% overlap. Subtract one for discarding DC component.
const FFT1DSizeType spectraComponents = fft1DSize / 2 / 2 - 1;
const ThreadIdType numberOfWorkUnits = this->GetNumberOfWorkUnits();
this->m_PerThreadDataContainer.resize(numberOfWorkUnits);
for (ThreadIdType threadId = 0; threadId < numberOfWorkUnits; ++threadId)
{
PerThreadData & perThreadData = this->m_PerThreadDataContainer[threadId];
perThreadData.ComplexVector.set_size(fft1DSize / 2);
perThreadData.SpectraVector.resize(spectraComponents);
perThreadData.LineImageRegionSize.Fill(1);
perThreadData.LineImageRegionSize[0] = fft1DSize;
}
}
template <typename TInputImage, typename TSupportWindowImage, typename TOutputImage>
void
Spectra1DImageFilter<TInputImage, TSupportWindowImage, TOutputImage>::VerifyInputInformation() const
{}
template <typename TInputImage, typename TSupportWindowImage, typename TOutputImage>
void
Spectra1DImageFilter<TInputImage, TSupportWindowImage, TOutputImage>::AddLineWindow(FFT1DSizeType length,
LineWindowMapType & lineWindowMap)
{
if (lineWindowMap.count(length) == 1)
{
return;
}
// Currently using a Hamming Window
SpectraVectorType window(length);
ScalarType sum = NumericTraits<ScalarType>::ZeroValue();
for (FFT1DSizeType sample = 0; sample < length; ++sample)
{
window[sample] = 0.54 + 0.46 * std::cos((Math::twopi * sample) / (length - 1));
sum += window[sample];
}
for (FFT1DSizeType sample = 0; sample < length; ++sample)
{
window[sample] /= sum;
}
lineWindowMap[length] = window;
}
template <typename TInputImage, typename TSupportWindowImage, typename TOutputImage>
void
Spectra1DImageFilter<TInputImage, TSupportWindowImage, TOutputImage>::ComputeSpectra(const IndexType & lineIndex,
ThreadIdType threadId,
SpectraLineType & spectraLine)
{
const InputImageType * input = this->GetInput();
PerThreadData & perThreadData = this->m_PerThreadDataContainer[threadId];
const FFT1DSizeType fftSize = static_cast<FFT1DSizeType>(perThreadData.ComplexVector.size());
const typename InputImageType::RegionType lineRegion(lineIndex, perThreadData.LineImageRegionSize);
InputImageIteratorType inputIt(input, lineRegion);
inputIt.GoToBegin();
perThreadData.ComplexVector.fill(0);
typename ComplexVectorType::iterator complexVectorIt = perThreadData.ComplexVector.begin();
const typename ComplexVectorType::iterator complexVectorEnd = perThreadData.ComplexVector.end();
typename SpectraVectorType::const_iterator windowIt = perThreadData.LineWindowMap[fftSize].begin();
typename ComplexVectorType::const_iterator complexVectorConstIt = perThreadData.ComplexVector.begin();
typename SpectraVectorType::iterator spectraVectorIt = perThreadData.SpectraVector.begin();
const size_t highFreq = perThreadData.SpectraVector.size();
for (size_t freq = 0; freq < highFreq; ++freq)
{
spectraVectorIt[freq] = 0.0f;
}
const double overlap = 0.5;
IndexType segmentIndex(lineIndex);
const double spectralScale = 1.0 / (fftSize * fftSize);
// 3 segments for Welch's method with 50% overlap (D=M/2)
for (unsigned int segment = 0; segment < 3; ++segment)
{
segmentIndex[0] =
static_cast<IndexValueType>(lineIndex[0] + segment * perThreadData.LineImageRegionSize[0] * overlap / 3.0);
inputIt.SetIndex(segmentIndex);
complexVectorIt = perThreadData.ComplexVector.begin();
windowIt = perThreadData.LineWindowMap[fftSize].begin();
while (complexVectorIt != complexVectorEnd)
{
*complexVectorIt = inputIt.Value() * *windowIt;
++inputIt;
++complexVectorIt;
++windowIt;
}
FFT1DType fft1D(fftSize);
fft1D.bwd_transform(perThreadData.ComplexVector);
complexVectorConstIt = perThreadData.ComplexVector.begin();
spectraVectorIt = perThreadData.SpectraVector.begin();
// drop DC component
++complexVectorConstIt;
// Each spectral component = (Re^2 + Im^2) / 3 / (fftSize)^2
for (size_t freq = 0; freq < highFreq; ++freq)
{
spectraVectorIt[freq] +=
std::real(*complexVectorConstIt * std::conj(*complexVectorConstIt)) / 3.0 * spectralScale;
++complexVectorConstIt;
}
}
spectraLine.first = lineIndex;
spectraLine.second = perThreadData.SpectraVector;
}
template <typename TInputImage, typename TSupportWindowImage, typename TOutputImage>
void
Spectra1DImageFilter<TInputImage, TSupportWindowImage, TOutputImage>::ThreadedGenerateData(
const OutputImageRegionType & outputRegionForThread,
ThreadIdType threadId)
{
OutputImageType * output = this->GetOutput();
const SupportWindowImageType * supportWindowImage = this->GetSupportWindowImage();
using OutputIteratorType = ImageLinearIteratorWithIndex<OutputImageType>;
OutputIteratorType outputIt(output, outputRegionForThread);
outputIt.SetDirection(1);
const MetaDataDictionary & dict = supportWindowImage->GetMetaDataDictionary();
PerThreadData & perThreadData = this->m_PerThreadDataContainer[threadId];
this->AddLineWindow(perThreadData.ComplexVector.size(), perThreadData.LineWindowMap);
SpectraLinesContainerType spectraLines;
using SupportWindowIteratorType = ImageLinearConstIteratorWithIndex<SupportWindowImageType>;
SupportWindowIteratorType supportWindowIt(supportWindowImage, outputRegionForThread);
supportWindowIt.SetDirection(1);
SpectraLineType spectraLine;
for (outputIt.GoToBegin(), supportWindowIt.GoToBegin(); !outputIt.IsAtEnd();
outputIt.NextLine(), supportWindowIt.NextLine())
{
spectraLines.clear();
while (!outputIt.IsAtEndOfLine())
{
// Compute the per line spectra.
const SupportWindowType & supportWindow = supportWindowIt.Value();
if (spectraLines.size() == 0) // first window in this lateral direction
{
const typename SupportWindowType::const_iterator windowLineEnd = supportWindow.end();
for (typename SupportWindowType::const_iterator windowLine = supportWindow.begin(); windowLine != windowLineEnd;
++windowLine)
{
const IndexType & lineIndex = *windowLine;
this->ComputeSpectra(lineIndex, threadId, spectraLine);
spectraLines.push_back(spectraLine);
}
}
else // subsequent window along a line
{
const IndexValueType desiredFirstLine = supportWindow.front()[1];
while (spectraLines.front().first[1] < desiredFirstLine)
{
spectraLines.pop_front();
}
const typename SupportWindowType::const_iterator windowLineEnd = supportWindow.end();
typename SpectraLinesContainerType::iterator spectraLinesIt = spectraLines.begin();
const typename SpectraLinesContainerType::iterator spectraLinesEnd = spectraLines.end();
for (typename SupportWindowType::const_iterator windowLine = supportWindow.begin(); windowLine != windowLineEnd;
++windowLine)
{
const IndexType & lineIndex = *windowLine;
if (spectraLinesIt == spectraLinesEnd) // past the end of the previously processed lines
{
this->ComputeSpectra(lineIndex, threadId, spectraLine);
spectraLines.push_back(spectraLine);
}
else if (lineIndex[1] == (spectraLinesIt->first)[1]) // one of the same lines that was previously computed
{
if (lineIndex[0] != (spectraLinesIt->first)[0])
{
this->ComputeSpectra(lineIndex, threadId, spectraLine);
*spectraLinesIt = spectraLine;
}
++spectraLinesIt;
}
else
{
itkExceptionMacro("Unexpected line");
}
}
}
// lateral window and sum
const size_t spectraLinesCount = spectraLines.size();
this->AddLineWindow(spectraLinesCount, perThreadData.LineWindowMap);
typename OutputImageType::PixelType outputPixel;
const FFT1DSizeType spectralComponents = perThreadData.SpectraVector.size();
outputPixel.SetSize(spectralComponents);
outputPixel.Fill(NumericTraits<ScalarType>::ZeroValue());
typename SpectraVectorType::const_iterator windowIt = perThreadData.LineWindowMap[spectraLinesCount].begin();
typename SpectraLinesContainerType::iterator linesIt = spectraLines.begin();
for (size_t line = 0; line < spectraLinesCount; ++line)
{
typename SpectraVectorType::const_iterator spectraIt = linesIt->second.begin();
for (FFT1DSizeType sample = 0; sample < spectralComponents; ++sample)
{
outputPixel[sample] += *windowIt * *spectraIt;
++spectraIt;
}
++windowIt;
++linesIt;
}
outputIt.Set(outputPixel);
++outputIt;
++supportWindowIt;
}
}
// Optionally normalize for system noise via reference spectra image input
const OutputImageType * referenceSpectra = this->GetReferenceSpectraImage();
if (referenceSpectra != nullptr)
{
using ReferenceSpectraIteratorType = ImageScanlineConstIterator<OutputImageType>;
ReferenceSpectraIteratorType referenceSpectraIt(referenceSpectra, outputRegionForThread);
using PopulatedOutputIteratorType = ImageScanlineIterator<OutputImageType>;
PopulatedOutputIteratorType populatedOutputIt(output, outputRegionForThread);
const unsigned int numberOfComponents = referenceSpectra->GetNumberOfComponentsPerPixel();
if (numberOfComponents != output->GetNumberOfComponentsPerPixel())
{
itkExceptionMacro("ReferenceSpectraImage has " << numberOfComponents << " while the output image has "
<< output->GetNumberOfComponentsPerPixel() << " components");
}
for (referenceSpectraIt.GoToBegin(), populatedOutputIt.GoToBegin(); !populatedOutputIt.IsAtEnd();)
{
while (!populatedOutputIt.IsAtEndOfLine())
{
using PixelType = typename OutputImageType::PixelType;
PixelType outputPixel = populatedOutputIt.Get();
const PixelType referencePixel = referenceSpectraIt.Get();
for (unsigned int component = 0; component < numberOfComponents; ++component)
{
if (Math::FloatAlmostEqual(referencePixel[component], NumericTraits<typename PixelType::ValueType>::Zero))
{
outputPixel[component] = NumericTraits<typename PixelType::ValueType>::Zero;
}
else
{
outputPixel[component] /= referencePixel[component];
}
}
populatedOutputIt.Set(outputPixel);
++populatedOutputIt;
++referenceSpectraIt;
}
populatedOutputIt.NextLine();
referenceSpectraIt.NextLine();
}
}
}
} // end namespace itk
#endif