-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cxx
215 lines (158 loc) · 6.62 KB
/
main.cxx
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
/* main.cpp: Bed Removal from Clinical CT Scans using ITK filters.
//////////////////////////////////////////////////////////////////////
BED REMOVAL FROM CT SCANS v1.0.0 (12 Aug 2013)
//////////////////////////////////////////////////////////////////////
Center for Infectious Disease Imaging (CIDI),
Radiology and Imaging Sciences,
National Institutes of Health (NIH),
Bethesda MD
12 Aug 2013
Written by Awais Mansoor, PhD
Primary Contact: [email protected]
Secondary Contact: [email protected]
and
//////////////////////////////////////////////////////////////////////
This code removes the bed out of the abdominal CT scan using intensity
thresholding coupled with morphological operations.
*/
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkBinaryThresholdImageFilter.h"
#include "itkBinaryBallStructuringElement.h"
#include "itkLabelGeometryImageFilter.h"
#include "itkBinaryMorphologicalOpeningImageFilter.h"
#include "itkBinaryMorphologicalClosingImageFilter.h"
typedef float PixelType;
typedef unsigned char OutPixelType;
typedef itk::Image<PixelType, 3> ImageType;
typedef itk::Image< OutPixelType, 3 > OutImageType;
int main(int argc, char * argv[])
{
if( argc < 3 )
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0] << " inputImageFile outputImageFile Output Type (0=Bed Removed Image, 1=Body Mask; def.=0)" << std::endl;
return EXIT_FAILURE;
}
bool Output=0;
if (argc==4) Output=argv[3];
typedef itk::ImageFileReader< ImageType > ReaderType;
typedef itk::ImageFileWriter< ImageType > WriterType;
ReaderType::Pointer reader = ReaderType::New();
WriterType::Pointer writer = WriterType::New();
reader->SetFileName( argv[1] );
writer->SetFileName( argv[2] );
//Pipeline
try
{
reader->Update();
}
catch ( itk::ExceptionObject &err)
{
std::cout<<"Problems reading input image"<<std::endl;
std::cerr << "ExceptionObject caught !" << std::endl;
std::cerr << err << std::endl;
return EXIT_FAILURE;
}
//Get image specs
ImageType::SpacingType spacing = reader->GetOutput()->GetSpacing();
ImageType::PointType origin = reader->GetOutput()->GetOrigin();
ImageType::DirectionType direction = reader->GetOutput()->GetDirection();
ImageType::SizeType insize = reader->GetOutput()->GetRequestedRegion().GetSize();
int pRow, pCol, pSli;
pRow = insize[0];
pCol = insize[1];
pSli = insize[2];
ImageType::RegionType region;
region.SetSize( insize );
//SKin BoundaryExtraction Starts
ImageType::SizeType outputSize;
outputSize[0]=insize[0]/3;
outputSize[1]=insize[1]/3;
outputSize[2]=insize[2];
ImageType::SpacingType outputSpacing;
outputSpacing[0] = reader->GetOutput()->GetSpacing()[0] * (static_cast<double>(insize[0]) / outputSize[0]);
outputSpacing[1] = reader->GetOutput()->GetSpacing()[1] * (static_cast<double>(insize[1]) / outputSize[1]);
outputSpacing[2] = reader->GetOutput()->GetSpacing()[2];
typedef itk::IdentityTransform<double, 3> TransformType;
//std::cout<<"Entering Skin-body Edxtraction..."<<std::endl;
typedef itk::ResampleImageFilter<ImageType, ImageType> ResampleImageFilterType;
ResampleImageFilterType::Pointer resampleDN = ResampleImageFilterType::New();
resampleDN->SetInput(reader->GetOutput());
resampleDN->SetSize(outputSize);
resampleDN->SetOutputSpacing(outputSpacing);
resampleDN->SetOutputOrigin( origin );
resampleDN->SetOutputDirection( direction );
resampleDN->SetTransform(TransformType::New());
//resampleDN->SetInterpolator(pInterpolator);
resampleDN->UpdateLargestPossibleRegion();
typedef itk::BinaryThresholdImageFilter<ImageType, OutImageType > BinaryThresholdImageFilterType;
BinaryThresholdImageFilterType::Pointer binaryskinfilter = BinaryThresholdImageFilterType::New();
binaryskinfilter->SetInput( resampleDN->GetOutput() );
binaryskinfilter->SetOutsideValue( 0 );
binaryskinfilter->SetInsideValue( 1 );
binaryskinfilter->SetLowerThreshold( -300 );
binaryskinfilter->SetUpperThreshold( 3071 );
binaryskinfilter->Update();
typedef itk::BinaryBallStructuringElement<OutImageType::PixelType, OutImageType::ImageDimension>
StructuringElementSkinType;
StructuringElementSkinType structuringElementSkin;
structuringElementSkin.SetRadius(3);
structuringElementSkin.CreateStructuringElement();
typedef itk::BinaryMorphologicalOpeningImageFilter <OutImageType, OutImageType, StructuringElementSkinType> BinaryMorphologicalOpeningImageFilter;
BinaryMorphologicalOpeningImageFilter::Pointer openingFilter
= BinaryMorphologicalOpeningImageFilter::New();
openingFilter->SetInput(binaryskinfilter->GetOutput());
openingFilter->SetKernel(structuringElementSkin);
openingFilter->SetForegroundValue(1);
openingFilter->Update();
typedef itk::BinaryMorphologicalClosingImageFilter <OutImageType, ImageType, StructuringElementSkinType>
BinaryMorphologicalClosingImageFilter;
BinaryMorphologicalClosingImageFilter::Pointer closingFilter = BinaryMorphologicalClosingImageFilter::New();
closingFilter->SetInput(openingFilter->GetOutput());
structuringElementSkin.SetRadius(40);
structuringElementSkin.CreateStructuringElement();
closingFilter->SetKernel(structuringElementSkin);
closingFilter->SetForegroundValue(1);
closingFilter->Update();
ResampleImageFilterType::Pointer resampleUP = ResampleImageFilterType::New();
resampleUP->SetInput(closingFilter->GetOutput());
resampleUP->SetSize(insize);
resampleUP->SetOutputSpacing(reader->GetOutput()->GetSpacing());
resampleUP->SetOutputOrigin( origin );
resampleUP->SetOutputDirection( direction );
resampleUP->SetTransform(TransformType::New());
//resampleUP->SetInterpolator(pInterpolator);
resampleUP->UpdateLargestPossibleRegion();
//Skin Boundary Extraction Ends
//std::cout<<"Ending Skin-body Edxtraction..."<<std::endl;
ImageType::IndexType pixelIndex;
for (int k=0; k<pSli; k++)
for(int i=0; i<pRow; i++)
for(int j=0; j<pCol; j++)
{
pixelIndex[0]=i;
pixelIndex[1]=j;
pixelIndex[2]=k;
if(resampleUP->GetOutput()->GetPixel(pixelIndex)==0 && resampleUP->GetOutput()->GetPixel(pixelIndex)>-3000)
reader->GetOutput()->SetPixel(pixelIndex,-1024);
}
reader->Update();
if(Output==1)
writer->SetInput(resampleUP->GetOutput());
else
writer->SetInput(reader->GetOutput());
try
{
writer->Update();
}
catch( itk::ExceptionObject & err )
{
std::cout<<"ExceptionObject caught !"<<std::endl;
std::cout<< err <<std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}