-
Notifications
You must be signed in to change notification settings - Fork 0
/
rjbutilities.h
317 lines (263 loc) · 10.4 KB
/
rjbutilities.h
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
#ifndef _RJBUTILITIES_H
#define _RJBUTILITIES_H
#include "itkLabelImageToStatisticsLabelMapFilter.h"
#include "itkLabelStatisticsImageFilter.h"
#include <itkBinaryThresholdImageFilter.h>
#include <itkResampleImageFilter.h>
#include <itkIdentityTransform.h>
#include <itkLinearInterpolateImageFunction.h>
#include <itkNearestNeighborInterpolateImageFunction.h>
#include <itkImageToHistogramFilter.h>
#include <itkMaskedImageToHistogramFilter.h>
#if 1
/////////////////////////////////////////////////////////
template <class RawIm, class MaskIm, class RealType>
std::vector<RealType>
computeMaskQuantiles(typename RawIm::Pointer raw, typename MaskIm::Pointer mask,
std::vector<RealType> quantiles)
{
// robust rescaling based on median and IQR
typedef typename itk::LabelImageToStatisticsLabelMapFilter <MaskIm, RawIm> LabStatsType;
typename LabStatsType::Pointer statsfilt = LabStatsType::New();
statsfilt->SetFeatureImage(raw);
statsfilt->SetInput(mask);
statsfilt->Update();
// typename LabStatsType::RealType Mn = statsfilt->GetMinimum(1);
// typename LabStatsType::RealType Mx = statsfilt->GetMaximum(1);
//statsfilt->SetHistogramParameters(100, Mn, Mx);
statsfilt->SetComputeHistogram(true);
statsfilt->Modified();
statsfilt->Update();
// the output image is actually a label map
typename LabStatsType::OutputImageType * labelMap = statsfilt->GetOutput();
const typename LabStatsType::LabelObjectType * labelObject = labelMap->GetLabelObject( 1 );
const typename LabStatsType::LabelObjectType::HistogramType * hist = labelObject->GetHistogram();
std::vector<RealType> result;
for (unsigned K=0;K<quantiles.size();K++)
{
result.push_back((RealType)hist->Quantile(0, quantiles[K]));
}
return(result);
}
#endif
template <class RawIm, class MaskIm>
void
computeMaskMeanVar(typename RawIm::Pointer raw, typename MaskIm::Pointer mask,
typename MaskIm::PixelType Lab,
double &Mean,
double &Var)
{
// robust rescaling based on median and IQR
typedef typename itk::LabelStatisticsImageFilter<RawIm, MaskIm> StatsType;
typename StatsType::Pointer statsfilt = StatsType::New();
statsfilt->SetInput(raw);
statsfilt->SetLabelInput(mask);
statsfilt->Update();
Mean = (double)statsfilt->GetMean(Lab);
Var = (double)statsfilt->GetVariance(Lab);
}
//////////////////////////////////////////////
template <class TImage>
std::vector<typename TImage::PixelType> computeImQuantile(typename TImage::Pointer in, std::vector<float> quants)
{
typedef typename itk::Statistics::ImageToHistogramFilter<TImage> HistMakerType;
typename HistMakerType::Pointer HistMaker = HistMakerType::New();
HistMaker->SetInput(in);
HistMaker->SetAutoMinimumMaximum(true);
typename HistMakerType::HistogramSizeType hsize(in->GetNumberOfComponentsPerPixel());
hsize.Fill(256);
HistMaker->SetHistogramSize(hsize);
HistMaker->Update();
std::vector< typename TImage::PixelType > result(quants.size(), 0);
const typename HistMakerType::HistogramType * hP = HistMaker->GetOutput();
int size = hP->GetSize(0);
int first = 0;
while( first < size && hP->GetFrequency(first, 0) == 0 )
{
first++;
}
if (first == size)
{
std::cerr << "No data in histogram";
return(result);
}
for (unsigned i = 0; i < quants.size(); i++)
{
result[i]=hP->Quantile(0, quants[i]);
}
return(result);
}
//////////////////////////////////////////////
template <class TImage, class MaskImage>
std::vector<typename TImage::PixelType> computeImQuantile(typename TImage::Pointer in, typename MaskImage::Pointer mask, std::vector<float> quants)
{
//std::cout << "start hist create" << std::endl;
typedef typename itk::Statistics::MaskedImageToHistogramFilter<TImage, MaskImage> HistMakerType;
mask->Update();
typename HistMakerType::Pointer HistMaker = HistMakerType::New();
HistMaker->SetInput(in);
HistMaker->SetMaskImage(mask);
HistMaker->SetAutoMinimumMaximum(true);
HistMaker->SetMaskValue(1);
typename HistMakerType::HistogramSizeType hsize(in->GetNumberOfComponentsPerPixel());
hsize.Fill(256);
HistMaker->SetHistogramSize(hsize);
HistMaker->Update();
// std::cout << "start hist process" << std::endl;
std::vector< typename TImage::PixelType > result(quants.size(), 0);
const typename HistMakerType::HistogramType * hP = HistMaker->GetOutput();
int size = hP->GetSize(0);
int first = 0;
while( first < size && hP->GetFrequency(first, 0) == 0 )
{
first++;
}
if (first == size)
{
std::cerr << "No data in histogram";
return(result);
}
for (unsigned i = 0; i < quants.size(); i++)
{
result[i]=hP->Quantile(0, quants[i]);
}
return(result);
}
/////////////////////////////////////////////////////////
template <class RawIm, class MaskIm>
typename MaskIm::Pointer doThresh(typename RawIm::Pointer raw, float threshVal, float scale = 1.0)
{
// a convenience function - sacrifices streaming
typedef typename itk::BinaryThresholdImageFilter<RawIm, MaskIm> ThreshType;
typename ThreshType::Pointer wthresh = ThreshType::New();
wthresh->SetInput(raw);
// take into account spm's scaling
wthresh->SetUpperThreshold((typename RawIm::PixelType)(threshVal * scale));
wthresh->SetLowerThreshold(0);
wthresh->SetInsideValue(0);
wthresh->SetOutsideValue(1);
typename MaskIm::Pointer result = wthresh->GetOutput();
result->Update();
result->DisconnectPipeline();
return(result);
}
/////////////////////////////////////////////////////////
template <class RawIm, class MaskIm>
typename MaskIm::Pointer doThresh2(typename RawIm::Pointer raw, float threshVal, float scale = 1.0)
{
// a convenience function - sacrifices streaming
typedef typename itk::BinaryThresholdImageFilter<RawIm, MaskIm> ThreshType;
typename ThreshType::Pointer wthresh = ThreshType::New();
wthresh->SetInput(raw);
// take into account spm's scaling
wthresh->SetUpperThreshold((typename RawIm::PixelType)(threshVal * scale));
wthresh->SetLowerThreshold(0);
wthresh->SetInsideValue(1);
wthresh->SetOutsideValue(0);
typename MaskIm::Pointer result = wthresh->GetOutput();
result->Update();
result->DisconnectPipeline();
return(result);
}
///////////////////////////////////////////////////
template <class RawIm>
typename RawIm::Pointer resampleIm(typename RawIm::Pointer input, typename RawIm::SpacingType NewSpacing, int interp=1)
{
const int dim = RawIm::ImageDimension;
typedef typename RawIm::PixelType PixelType;
typedef typename itk::ResampleImageFilter<RawIm, RawIm > ResampleFilterType;
typedef typename itk::IdentityTransform< double, dim > TransformType;
typename ResampleFilterType::Pointer resampler = ResampleFilterType::New();
input->Update();
typename TransformType::Pointer transform = TransformType::New();
transform->SetIdentity();
resampler->SetTransform( transform );
typedef typename itk::LinearInterpolateImageFunction<RawIm, double > LInterpolatorType;
typedef typename itk::NearestNeighborInterpolateImageFunction<RawIm, double > NNInterpolatorType;
typename ResampleFilterType::InterpolatorPointerType interpolator;
switch (interp)
{
case 0:
interpolator = NNInterpolatorType::New();
break;
case 1:
interpolator = LInterpolatorType::New();
break;
default:
std::cout << "Unsupported interpolator" << std::endl;
}
resampler->SetInterpolator( interpolator );
resampler->SetDefaultPixelValue( 0 );
const typename RawIm::SpacingType& inputSpacing = input->GetSpacing();
//typename RawIm::SpacingType spacing;
typename RawIm::SizeType inputSize = input->GetLargestPossibleRegion().GetSize();
typename RawIm::SizeType size;
typename RawIm::PointType newOrigin = input->GetOrigin();
typedef typename RawIm::SizeType::SizeValueType SizeValueType;
// use the continuous index concept to generate a world coordinate
// for the origin
typedef typename itk::ContinuousIndex<double, RawIm::ImageDimension> ContIndType;
ContIndType NewContInd;
// keep eye on this one for a while. Not sure it is correct
typename RawIm::PointType oldPoint, newPoint;
input->TransformIndexToPhysicalPoint(input->GetLargestPossibleRegion().GetIndex(), oldPoint);
newPoint = oldPoint - (inputSpacing - NewSpacing)/2;
input->TransformPhysicalPointToContinuousIndex(newPoint, NewContInd);
typename RawIm::IndexType idx = input->GetLargestPossibleRegion().GetIndex();
for (int i = 0; i < dim; i++)
{
float factor = inputSpacing[i]/NewSpacing[i];
size[i] = static_cast< SizeValueType >(round( inputSize[i] * factor));
newOrigin[i] -= (idx[i] - NewContInd[i])*inputSpacing[i];
idx[i] *= factor;
}
resampler->SetSize( size );
resampler->SetOutputSpacing( NewSpacing );
resampler->SetOutputOrigin( newOrigin);
// need to be careful setting the index
resampler->SetOutputStartIndex ( idx );
resampler->SetOutputDirection(input->GetDirection());
resampler->SetInput(input);
typename RawIm::Pointer result = resampler->GetOutput();
result->Update();
result->DisconnectPipeline();
return(result);
}
///////////////////////////////////////////////////
template <class RawIm>
typename RawIm::Pointer resampleIm(typename RawIm::Pointer input, typename RawIm::Pointer exampleIm, int interp=1)
{
const int dim = RawIm::ImageDimension;
typedef typename RawIm::PixelType PixelType;
typedef typename itk::ResampleImageFilter<RawIm, RawIm > ResampleFilterType;
typedef typename itk::IdentityTransform< double, dim > TransformType;
typename ResampleFilterType::Pointer resampler = ResampleFilterType::New();
input->Update();
typename TransformType::Pointer transform = TransformType::New();
transform->SetIdentity();
resampler->SetTransform( transform );
typedef typename itk::LinearInterpolateImageFunction<RawIm, double > LInterpolatorType;
typedef typename itk::NearestNeighborInterpolateImageFunction<RawIm, double > NNInterpolatorType;
typename ResampleFilterType::InterpolatorPointerType interpolator;
switch (interp)
{
case 0:
interpolator = NNInterpolatorType::New();
break;
case 1:
interpolator = LInterpolatorType::New();
break;
default:
std::cout << "Unsupported interpolator" << std::endl;
}
resampler->UseReferenceImageOn();
resampler->SetReferenceImage(exampleIm);
resampler->SetInterpolator( interpolator );
resampler->SetDefaultPixelValue( 0 );
resampler->SetInput(input);
typename RawIm::Pointer result = resampler->GetOutput();
result->Update();
result->DisconnectPipeline();
return(result);
}
#endif