forked from InsightSoftwareConsortium/ITK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
itkHistogramMatchingImageFilter.txx
478 lines (386 loc) · 12.7 KB
/
itkHistogramMatchingImageFilter.txx
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: itkHistogramMatchingImageFilter.txx
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef _itkHistogramMatchingImageFilter_txx
#define _itkHistogramMatchingImageFilter_txx
#include "itkHistogramMatchingImageFilter.h"
#include "itkImageRegionIterator.h"
#include "itkImageRegionConstIterator.h"
#include "itkNumericTraits.h"
#include <vector>
namespace itk
{
/*
*
*/
template <class TInputImage, class TOutputImage>
HistogramMatchingImageFilter<TInputImage,TOutputImage>
::HistogramMatchingImageFilter()
{
this->SetNumberOfRequiredInputs( 2 );
m_NumberOfHistogramLevels = 256;
m_NumberOfMatchPoints = 1;
m_QuantileTable.set_size( 3, m_NumberOfMatchPoints + 2 );
m_QuantileTable.fill(0);
m_Gradients.set_size( m_NumberOfMatchPoints + 1 );
m_Gradients.fill(0);
m_ThresholdAtMeanIntensity = true;
m_SourceIntensityThreshold = 0;
m_ReferenceIntensityThreshold = 0;
m_LowerGradient = 0.0;
m_UpperGradient = 0.0;
// Create histograms.
m_SourceHistogram = HistogramType::New();
m_ReferenceHistogram = HistogramType::New();
m_OutputHistogram = HistogramType::New();
}
/*
*
*/
template <class TInputImage, class TOutputImage>
void
HistogramMatchingImageFilter<TInputImage,TOutputImage>
::PrintSelf(std::ostream& os, Indent indent) const
{
Superclass::PrintSelf(os,indent);
os << indent << "NumberOfHistogramLevels: ";
os << m_NumberOfHistogramLevels << std::endl;
os << indent << "NumberOfMatchPoints: ";
os << m_NumberOfMatchPoints << std::endl;
os << indent << "ThresholdAtMeanIntensity: ";
os << m_ThresholdAtMeanIntensity << std::endl;
os << indent << "SourceIntensityThreshold: ";
os << m_SourceIntensityThreshold << std::endl;
os << indent << "ReferenceIntensityThreshold: ";
os << m_ReferenceIntensityThreshold << std::endl;
os << indent << "OutputIntensityThreshold: ";
os << m_ReferenceIntensityThreshold << std::endl;
os << indent << "Source histogram: ";
os << m_SourceHistogram.GetPointer() << std::endl;
os << indent << "Reference histogram: ";
os << m_ReferenceHistogram.GetPointer() << std::endl;
os << indent << "Output histogram: ";
os << m_OutputHistogram.GetPointer() << std::endl;
os << indent << "QuantileTable: " << std::endl;
os << m_QuantileTable << std::endl;
os << indent << "Gradients: " << std::endl;
os << m_Gradients << std::endl;
os << indent << "LowerGradient: ";
os << m_LowerGradient << std::endl;
os << indent << "UpperGradient: ";
os << m_UpperGradient << std::endl;
}
/*
*
*/
template <class TInputImage, class TOutputImage>
void
HistogramMatchingImageFilter<TInputImage,TOutputImage>
::SetReferenceImage( const InputImageType * reference )
{
this->ProcessObject::SetNthInput(1,
const_cast< InputImageType * >( reference ) );
}
/*
*
*/
template <class TInputImage, class TOutputImage>
const typename HistogramMatchingImageFilter<TInputImage,TOutputImage>
::InputImageType *
HistogramMatchingImageFilter<TInputImage,TOutputImage>
::GetReferenceImage()
{
if ( this->GetNumberOfInputs() < 2 )
{
return NULL;
}
return dynamic_cast<TInputImage*>(
this->ProcessObject::GetInput(1) );
}
/*
* This filter requires all of the input images to be
* in the buffer.
*/
template <class TInputImage, class TOutputImage>
void
HistogramMatchingImageFilter<TInputImage,TOutputImage>
::GenerateInputRequestedRegion()
{
this->Superclass::GenerateInputRequestedRegion();
for ( unsigned int idx = 0; idx < this->GetNumberOfInputs(); ++idx )
{
if ( this->GetInput(idx) )
{
InputImagePointer image =
const_cast< InputImageType * >( this->GetInput(idx) );
image->SetRequestedRegionToLargestPossibleRegion();
}
}
}
/*
*
*/
template <class TInputImage, class TOutputImage>
void
HistogramMatchingImageFilter<TInputImage,TOutputImage>
::BeforeThreadedGenerateData()
{
unsigned int j;
InputImageConstPointer source = this->GetSourceImage();
InputImageConstPointer reference = this->GetReferenceImage();
this->ComputeMinMaxMean( source, m_SourceMinValue,
m_SourceMaxValue, m_SourceMeanValue );
this->ComputeMinMaxMean( reference, m_ReferenceMinValue,
m_ReferenceMaxValue, m_ReferenceMeanValue );
if ( m_ThresholdAtMeanIntensity )
{
m_SourceIntensityThreshold = static_cast<InputPixelType>(m_SourceMeanValue);
m_ReferenceIntensityThreshold = static_cast<InputPixelType>(m_ReferenceMeanValue);
}
else
{
m_SourceIntensityThreshold = static_cast<InputPixelType>(m_SourceMinValue);
m_ReferenceIntensityThreshold = static_cast<InputPixelType>(m_ReferenceMinValue);
}
this->ConstructHistogram( source, m_SourceHistogram,
m_SourceIntensityThreshold, m_SourceMaxValue );
this->ConstructHistogram( reference, m_ReferenceHistogram,
m_ReferenceIntensityThreshold,
m_ReferenceMaxValue );
// Fill in the quantile table.
m_QuantileTable.set_size( 3, m_NumberOfMatchPoints + 2 );
m_QuantileTable[0][0] = m_SourceIntensityThreshold;
m_QuantileTable[1][0] = m_ReferenceIntensityThreshold;
m_QuantileTable[0][m_NumberOfMatchPoints + 1] = m_SourceMaxValue;
m_QuantileTable[1][m_NumberOfMatchPoints + 1] = m_ReferenceMaxValue;
double delta = 1.0 / ( double(m_NumberOfMatchPoints) + 1.0 );
for ( j = 1; j < m_NumberOfMatchPoints + 1; j++ )
{
m_QuantileTable[0][j] = m_SourceHistogram->Quantile(
0, double(j) * delta );
m_QuantileTable[1][j] = m_ReferenceHistogram->Quantile(
0, double(j) * delta );
}
// Fill in the gradient array.
m_Gradients.set_size( m_NumberOfMatchPoints + 1 );
double denominator;
for ( j = 0; j < m_NumberOfMatchPoints + 1; j++ )
{
denominator = m_QuantileTable[0][j+1] -
m_QuantileTable[0][j];
if ( denominator != 0 )
{
m_Gradients[j] = m_QuantileTable[1][j+1] -
m_QuantileTable[1][j];
m_Gradients[j] /= denominator;
}
else
{
m_Gradients[j] = 0.0;
}
}
denominator = m_QuantileTable[0][0] - m_SourceMinValue;
if ( denominator != 0 )
{
m_LowerGradient = m_QuantileTable[1][0] - m_ReferenceMinValue;
m_LowerGradient /= denominator;
}
else
{
m_LowerGradient = 0.0;
}
denominator = m_QuantileTable[0][m_NumberOfMatchPoints+1] -
m_SourceMaxValue;
if ( denominator != 0 )
{
m_UpperGradient = m_QuantileTable[1][m_NumberOfMatchPoints+1] -
m_ReferenceMaxValue;
m_UpperGradient /= denominator;
}
else
{
m_UpperGradient = 0.0;
}
}
/*
*
*/
template <class TInputImage, class TOutputImage>
void
HistogramMatchingImageFilter<TInputImage,TOutputImage>
::AfterThreadedGenerateData()
{
OutputImagePointer output = this->GetOutput();
this->ComputeMinMaxMean( output, m_OutputMinValue,
m_OutputMaxValue, m_OutputMeanValue );
if ( m_ThresholdAtMeanIntensity )
{
m_OutputIntensityThreshold = static_cast<OutputPixelType>(m_OutputMeanValue);
}
else
{
m_OutputIntensityThreshold = static_cast<OutputPixelType>(m_OutputMinValue);
}
this->ConstructHistogram( output, m_OutputHistogram,
m_OutputIntensityThreshold, m_OutputMaxValue );
// Fill in the quantile table.
m_QuantileTable[2][0] = m_OutputIntensityThreshold;
m_QuantileTable[2][m_NumberOfMatchPoints + 1] = m_OutputMaxValue;
double delta = 1.0 / ( double(m_NumberOfMatchPoints) + 1.0 );
for ( unsigned int j = 1; j < m_NumberOfMatchPoints + 1; j++ )
{
m_QuantileTable[2][j] = m_OutputHistogram->Quantile(
0, double(j) * delta );
}
}
/*
*
*/
template <class TInputImage, class TOutputImage>
void
HistogramMatchingImageFilter<TInputImage,TOutputImage>
::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread,
int threadId )
{
int i;
unsigned int j;
// Get the input and output pointers;
InputImageConstPointer input = this->GetInput();
OutputImagePointer output = this->GetOutput();
// Transform the source image and write to output.
typedef ImageRegionConstIterator<InputImageType> InputConstIterator;
typedef ImageRegionIterator<OutputImageType> OutputIterator;
InputConstIterator inIter( input, outputRegionForThread );
OutputIterator outIter( output, outputRegionForThread );
// support progress methods/callbacks
unsigned long updateVisits = 0;
unsigned long totalPixels = 0;
if ( threadId == 0 )
{
totalPixels = outputRegionForThread.GetNumberOfPixels();
updateVisits = totalPixels / 10;
if( updateVisits < 1 ) updateVisits = 1;
}
double srcValue, mappedValue;
for ( i = 0; !outIter.IsAtEnd(); ++inIter, ++outIter, i++ )
{
if ( threadId == 0 && !(i % updateVisits ) )
{
this->UpdateProgress((float)i / (float)totalPixels);
}
srcValue = static_cast<double>( inIter.Get() );
for ( j = 0; j < m_NumberOfMatchPoints + 2; j++ )
{
if ( srcValue < m_QuantileTable[0][j] )
{
break;
}
}
if ( j == 0 )
{
// Linear interpolate from min to point[0]
mappedValue = m_ReferenceMinValue +
( srcValue - m_SourceMinValue ) * m_LowerGradient;
}
else if ( j == m_NumberOfMatchPoints + 2 )
{
// Linear interpolate from point[m_NumberOfMatchPoints+1] to max
mappedValue = m_ReferenceMaxValue +
( srcValue - m_SourceMaxValue ) * m_UpperGradient;
}
else
{
// Linear interpolate from point[j] and point[j+1].
mappedValue = m_QuantileTable[1][j-1] +
( srcValue - m_QuantileTable[0][j-1] ) * m_Gradients[j-1];
}
outIter.Set( static_cast<OutputPixelType>( mappedValue ) );
}
}
/*
* Compute min, max and mean of an image.
*/
template <class TInputImage, class TOutputImage>
void
HistogramMatchingImageFilter<TInputImage,TOutputImage>
::ComputeMinMaxMean(
const InputImageType * image,
double& minValue,
double& maxValue,
double& meanValue )
{
typedef ImageRegionConstIterator<InputImageType> ConstIterator;
ConstIterator iter( image, image->GetBufferedRegion() );
double sum = 0.0;
long int count = 0;
minValue = static_cast<double>( iter.Get() );
maxValue = minValue;
double value;
while ( !iter.IsAtEnd() )
{
value = static_cast<double>( iter.Get() );
sum += value;
if ( value < minValue ) { minValue = value; }
if ( value > maxValue ) { maxValue = value; }
++iter;
++count;
}
meanValue = ( sum / count );
}
/*
* Construct a histogram from an image.
*/
template <class TInputImage, class TOutputImage>
void
HistogramMatchingImageFilter<TInputImage,TOutputImage>
::ConstructHistogram(
const InputImageType * image,
HistogramType * histogram,
double minValue,
double maxValue )
{
// allocate memory for the histogram
typename HistogramType::SizeType size;
size[0] = m_NumberOfHistogramLevels;
histogram->Initialize( size );
// set up min/max values in the histogram
float stepSize = ( maxValue - minValue ) /
static_cast<float>( m_NumberOfHistogramLevels );
unsigned long ibin;
for ( ibin = 0; ibin < m_NumberOfHistogramLevels - 1; ibin++ )
{
histogram->SetBinMin( 0, ibin, ibin * stepSize + minValue );
histogram->SetBinMax( 0, ibin, ( ibin + 1 ) * stepSize + minValue );
}
histogram->SetBinMin( 0, ibin, ibin * stepSize + minValue );
histogram->SetBinMax( 0, ibin, maxValue + stepSize );
// put each image pixel into the histogram
typedef ImageRegionConstIterator<InputImageType> ConstIterator;
ConstIterator iter( image, image->GetBufferedRegion() );
typename HistogramType::MeasurementVectorType measurement;
typedef typename HistogramType::MeasurementType MeasurementType;
measurement[0] = NumericTraits<MeasurementType>::Zero;
while ( !iter.IsAtEnd() )
{
InputPixelType value = iter.Get();
if ( static_cast<double>(value) >= minValue &&
static_cast<double>(value) <= maxValue )
{
// add sample to histogram
measurement[0] = value;
histogram->IncreaseFrequency( measurement, 1 );
}
++iter;
}
}
} // end namespace itk
#endif