File: ThreadedHistogramImageFilter.hxx

package info (click to toggle)
itksnap 3.6.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 22,132 kB
  • sloc: cpp: 91,089; ansic: 1,994; sh: 327; makefile: 16
file content (167 lines) | stat: -rw-r--r-- 4,015 bytes parent folder | download | duplicates (3)
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
#ifndef THREADEDHISTOGRAMIMAGEFILTER_HXX
#define THREADEDHISTOGRAMIMAGEFILTER_HXX

#include "ThreadedHistogramImageFilter.h"
#include <itkProgressReporter.h>
#include <itkImageRegionConstIterator.h>

template <class TInputImage>
ThreadedHistogramImageFilter<TInputImage>
::ThreadedHistogramImageFilter()
{
  this->SetNumberOfRequiredInputs(3);
  this->SetNumberOfRequiredOutputs(2);

  // Allocate the output histogram
  m_OutputHistogram = ScalarImageHistogram::New();
  this->SetNthOutput(1, m_OutputHistogram);

  m_Bins = 0;
  m_TransformScale = 1.0;
  m_TransformShift = 0.0;
}

template <class TInputImage>
void
ThreadedHistogramImageFilter<TInputImage>
::SetRangeInputs(PixelObjectType *inMin, PixelObjectType *inMax)
{
  m_InputMin = inMin;
  m_InputMax = inMax;
  this->SetNthInput(1, inMin);
  this->SetNthInput(2, inMax);
}

template <class TInputImage>
void
ThreadedHistogramImageFilter<TInputImage>
::SetNumberOfBins(int nBins)
{
  if(m_Bins != nBins)
    {
    m_Bins = nBins;
    this->Modified();
    }
}

template <class TInputImage>
void
ThreadedHistogramImageFilter<TInputImage>
::SetIntensityTransform(double scale, double shift)
{
  if(m_TransformScale != scale || m_TransformShift != shift)
    {
    m_TransformScale = scale;
    m_TransformShift = shift;
    this->Modified();
    }
}

template <class TInputImage>
void
ThreadedHistogramImageFilter<TInputImage>
::GenerateInputRequestedRegion()
{
  Superclass::GenerateInputRequestedRegion();
  if ( this->GetInput() )
    {
    InputImagePointer image =
      const_cast< typename Superclass::InputImageType * >( this->GetInput() );
    image->SetRequestedRegionToLargestPossibleRegion();
    }
}

template <class TInputImage>
void
ThreadedHistogramImageFilter<TInputImage>
::EnlargeOutputRequestedRegion(itk::DataObject *data)
{
  Superclass::EnlargeOutputRequestedRegion(data);
  data->SetRequestedRegionToLargestPossibleRegion();
}

template <class TInputImage>
void
ThreadedHistogramImageFilter<TInputImage>
::AllocateOutputs()
{
  // Pass the input through as the output
  InputImagePointer image =
    const_cast< TInputImage * >( this->GetInput() );

  this->GraftOutput(image);

  // Nothing to be done for the histogram output
}

template <class TInputImage>
void
ThreadedHistogramImageFilter<TInputImage>
::BeforeThreadedGenerateData()
{
  itk::ThreadIdType numberOfThreads = this->GetNumberOfThreads();

  // Get the range of the histogram
  PixelType pxmin = m_InputMin->Get();
  PixelType pxmax = m_InputMax->Get();

  // Initialize the per-thread histograms
  m_ThreadHistogram.resize(numberOfThreads);
  for(unsigned int i = 0; i < numberOfThreads; i++)
    {
    m_ThreadHistogram[i] = HistogramType::New();
    m_ThreadHistogram[i]->Initialize(pxmin, pxmax, m_Bins);
    }

  // Initialize the output
  m_OutputHistogram->Initialize(pxmin, pxmax, m_Bins);
}

template< class TInputImage >
void
ThreadedHistogramImageFilter<TInputImage>
::ThreadedGenerateData(const RegionType &outputRegionForThread,
                       itk::ThreadIdType threadId)
{
  if ( outputRegionForThread.GetNumberOfPixels() == 0 )
    return;

  // Get the histogram for this thread
  HistogramType *hist = m_ThreadHistogram[threadId];

  itk::ImageRegionConstIterator< TInputImage > it(this->GetInput(), outputRegionForThread);
  while(!it.IsAtEnd())
    {
    hist->AddSample(it.Get());
    ++it;
    }
}

template< class TInputImage >
void
ThreadedHistogramImageFilter<TInputImage>
::AfterThreadedGenerateData()
{
  // Add up the partial histograms
  for(unsigned int i = 0; i < m_ThreadHistogram.size(); i++)
    {
    m_OutputHistogram->AddCompatibleHistogram(*m_ThreadHistogram[i]);
    }

  // Apply the transform to the histogram
  m_OutputHistogram->ApplyIntensityTransform(m_TransformScale, m_TransformShift);
}

template< class TInputImage >
void
ThreadedHistogramImageFilter<TInputImage>
::PrintSelf(std::ostream &os, itk::Indent indent) const
{
  Superclass::PrintSelf(os, indent);
}





#endif // THREADEDHISTOGRAMIMAGEFILTER_HXX