-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPreProcess.cpp
More file actions
209 lines (168 loc) · 7.67 KB
/
PreProcess.cpp
File metadata and controls
209 lines (168 loc) · 7.67 KB
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
#include "PreProcess.h"
PreProcess::PreProcess()
{
//Constructor
}
PreProcess::~PreProcess()
{
//Destructor
}
PreProcess::ImageType::Pointer PreProcess::nonLinearIntensityRemap(float lowerThreshold, float upperThreshold, float alpha, float beta)
{
typedef itk::SigmoidImageFilter< ImageType, ImageType > SigmoidFilterType;
SigmoidFilterType::Pointer sigmoidFilter = SigmoidFilterType::New();
sigmoidFilter->SetInput( OriginalImage );
sigmoidFilter->SetOutputMinimum( lowerThreshold );
sigmoidFilter->SetOutputMaximum( upperThreshold );
sigmoidFilter->SetAlpha( alpha );
sigmoidFilter->SetBeta( beta );
typedef itk::RescaleIntensityImageFilter< ImageType, ImageType > RescaleFilter;
RescaleFilter::Pointer rescale = RescaleFilter::New();
rescale->SetInput( sigmoidFilter->GetOutput() );
rescale->SetOutputMinimum(0.0f);
rescale->SetOutputMaximum(255.0f);
rescale->Update();
return rescale->GetOutput();
}
//PreProcess::ImageType::Pointer PreProcess::curvatureSmoothImage(ImageType::Pointer remappedImage, float conductance, int numberOfIterations)
//{
// const ImageType::SpacingType& sp = remappedImage->GetSpacing();
// float timeStep = sp[2]/(powf(2, 4)); //4 = Dimension + 1
// std::cout<<"TimeStep = "<<timeStep<<std::endl;
//
// int numberOfIterations;
// if (sp[2] >= 1.0f) {
// numberOfIterations = 5;
// }
// else {
// numberOfIterations = 10;
// }
//
// typedef itk::CurvatureAnisotropicDiffusionImageFilter< ImageType, ImageType > SmoothingFilterType;
// SmoothingFilterType::Pointer smoothing = SmoothingFilterType::New();
// smoothing->SetInput( remappedImage );
// smoothing->SetTimeStep( timeStep );
// smoothing->SetNumberOfIterations( 5 );
// smoothing->SetConductanceParameter( 9.0 );
// smoothing->Update();
//
// typedef itk::RescaleIntensityImageFilter< ImageType, ImageType > RescaleFilter;
// RescaleFilter::Pointer rescale = RescaleFilter::New();
// rescale->SetInput( smoothing->GetOutput() );
// rescale->SetOutputMinimum(0.0f);
// rescale->SetOutputMaximum(255.0f);
// rescale->Update();
//
//
// return rescale->GetOutput();
//}
PreProcess::ImageType::Pointer PreProcess::gradientSmoothImage(ImageType::Pointer remappedImage, float conductance, int numberOfIterations)
{
const ImageType::SpacingType& sp = remappedImage->GetSpacing();
float min_Spacing = sp[0];
if (min_Spacing > sp[1]) {
min_Spacing = sp[1];
}
if (min_Spacing > sp[2]){
min_Spacing = sp[2];
}
float timeStep = min_Spacing/(powf(2, 4)); //4 = Dimension + 1
// int numberOfIterations = 10; //30
// float conductance = 8.0f; //30
//GPU Smoothing
typedef itk::GPUGradientAnisotropicDiffusionImageFilter< ImageType, ImageType > GPUAnisoDiffFilterType;
typename GPUAnisoDiffFilterType::Pointer smoothing = GPUAnisoDiffFilterType::New();
smoothing->SetInput( remappedImage );
smoothing->SetNumberOfIterations( numberOfIterations );
smoothing->SetTimeStep( timeStep );
smoothing->SetConductanceParameter( conductance );
smoothing->UseImageSpacingOn();
smoothing->Update();
// typedef itk::GradientAnisotropicDiffusionImageFilter< ImageType, ImageType > SmoothingFilterType;
// SmoothingFilterType::Pointer smoothing = SmoothingFilterType::New();
// smoothing->SetInput( remappedImage );
// smoothing->SetTimeStep( timeStep );
// smoothing->SetNumberOfIterations( numberOfIterations );
// smoothing->SetConductanceParameter( 9.0 );
// smoothing->Update();
typedef itk::RescaleIntensityImageFilter< ImageType, ImageType > RescaleFilter;
RescaleFilter::Pointer rescale = RescaleFilter::New();
rescale->SetInput( smoothing->GetOutput() );
rescale->SetOutputMinimum(0.0f);
rescale->SetOutputMaximum(255.0f);
rescale->Update();
return rescale->GetOutput();
}
PreProcess::ImageType::Pointer PreProcess::resampleImage(ImageType::Pointer smoothedImage)
{
std::cout << "resampleImage" << std::endl;
typedef itk::Image<ImageType::PixelType,3> CPUImageType;
typedef itk::ResampleImageFilter< CPUImageType, ImageType > FilterType;
FilterType::Pointer filter = FilterType::New();
typedef itk::AffineTransform< double, 3 > TransformType;
TransformType::Pointer transform = TransformType::New();
//transform->SetIdentity();
filter->SetTransform( transform );
typedef itk::LinearInterpolateImageFunction< CPUImageType, double > InterpolatorType;
InterpolatorType::Pointer interpolator = InterpolatorType::New();
filter->SetInterpolator( interpolator );
filter->SetDefaultPixelValue( 0 );
const ImageType::SpacingType& sp = smoothedImage->GetSpacing();
float min_Spacing = sp[0];
if (min_Spacing > sp[1]) {
min_Spacing = sp[1];
}
if (min_Spacing > sp[2]){
min_Spacing = sp[2];
}
ImageType::SpacingType newSp;
newSp[0] = min_Spacing;
newSp[1] = min_Spacing;
newSp[2] = min_Spacing;
std::cout<<"new spacing"<<newSp<<std::endl;
filter->SetOutputSpacing( newSp );
filter->SetOutputOrigin( smoothedImage->GetOrigin() );
ImageType::DirectionType direction;
direction.SetIdentity();
filter->SetOutputDirection( direction );
ImageType::SizeType NewSize;
NewSize[0] = int((double(smoothedImage->GetLargestPossibleRegion().GetSize()[0])*double(smoothedImage->GetSpacing()[0]))/double(newSp[0])); // number of pixels along X
NewSize[1] = int((double(smoothedImage->GetLargestPossibleRegion().GetSize()[1])*double(smoothedImage->GetSpacing()[1]))/double(newSp[1])); // number of pixels along Y
NewSize[2] = int((double(smoothedImage->GetLargestPossibleRegion().GetSize()[2])*double(smoothedImage->GetSpacing()[2]))/double(newSp[2])); // number of pixels along Z
std::cout<<"new size"<<NewSize<<std::endl;
filter->SetSize( NewSize );
filter->SetInput( smoothedImage );
filter->Update();
std::cout<<"Done resampleImage"<<std::endl;
return filter->GetOutput();
}
void PreProcess::RunPreProcess(std::string inputFilename, std::string outputFilename, float lowerThreshold, float upperThreshold, float alpha, float beta, float conductance, int numberOfIterations)
{
//Read Input Image
typedef itk::ImageFileReader< ImageType > ReaderType;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(inputFilename);
reader->Update();
OriginalImage = reader->GetOutput();
itk::TimeProbe clock1;
clock1.Start();
//Calculate remapped image to get the vessel intensiy range enhanced
std::cout<< std::endl <<"Intensity Remapping ..."<<std::endl;
ImageType::Pointer remappedImage = nonLinearIntensityRemap(lowerThreshold, upperThreshold, alpha, beta);
std::cout<< " Done (1/3)"<< std::endl;
//smooth the image
std::cout<< std::endl <<"Smoothing ..."<<std::endl;
ImageType::Pointer smoothedImage = gradientSmoothImage(remappedImage, conductance, numberOfIterations);
std::cout<< " Done (2/3)"<< std::endl;
// resample the smoothed image to isotropic image voxels
std::cout<< std::endl <<"Resampling ..."<<std::endl;
ImageType::Pointer resampledImage = resampleImage(smoothedImage);
std::cout<< " Done (3/3)"<< std::endl;
clock1.Stop();
std::cout<< std::endl<<"Total Time taken for Preprocessing Image = "<< clock1.GetMean() <<"sec\n"<< std::endl;
typedef itk::ImageFileWriter<ImageType> WriterType;
WriterType::Pointer writer = WriterType::New();
writer->SetInput( resampledImage );
writer->SetFileName(outputFilename);
writer->Update();
}