-
Notifications
You must be signed in to change notification settings - Fork 2
/
close_bin_para.cxx
252 lines (203 loc) · 8.85 KB
/
close_bin_para.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
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
////program for itkBinaryCloseParaImageFilter
//01: based on open_bin_para.cxx
#include "itkFilterWatcher.h"
#include <itkImageFileReader.h>
#include <itkBinaryThresholdImageFilter.h>
#include <itkBinaryCloseParaImageFilter.h>
#include <itkImageFileWriter.h>
template<typename InputComponentType, typename InputPixelType, size_t Dimension>
int DoIt(int argc, char *argv[]){
typedef uint8_t OutputPixelType;
typedef itk::Image<InputPixelType, Dimension> InputImageType;
typedef itk::Image<OutputPixelType, Dimension> OutputImageType;
typedef itk::ImageFileReader<InputImageType> ReaderType;
typename ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(argv[1]);
reader->ReleaseDataFlagOn();
FilterWatcher watcherI(reader);
watcherI.QuietOn();
watcherI.ReportTimeOn();
try{
reader->Update();
}
catch(itk::ExceptionObject &ex){
std::cerr << ex << std::endl;
return EXIT_FAILURE;
}
const typename InputImageType::Pointer& input= reader->GetOutput();
typedef itk::BinaryThresholdImageFilter<InputImageType, OutputImageType> ThrFilterType;
typename ThrFilterType::Pointer thr= ThrFilterType::New();
thr->SetInput(input);
thr->SetUpperThreshold(0);
thr->SetOutsideValue(1);
thr->SetInsideValue(0);
thr->ReleaseDataFlagOn();
FilterWatcher watcherThr(thr);
typedef itk::BinaryCloseParaImageFilter<OutputImageType> FilterType;
typename FilterType::Pointer filter= FilterType::New();
filter->SetInput(thr->GetOutput());
filter->SetRadius(atof(argv[4]));
filter->SetUseImageSpacing(atoi(argv[5]));
filter->SafeBorderOn(); //meaning changed?
filter->CircularOn();
filter->ReleaseDataFlagOn();
FilterWatcher watcher1(filter);
try{
filter->Update();
}
catch(itk::ExceptionObject &ex){
std::cerr << ex << std::endl;
return EXIT_FAILURE;
}
const typename OutputImageType::Pointer& output= filter->GetOutput();
typedef itk::ImageFileWriter<OutputImageType> WriterType;
typename WriterType::Pointer writer = WriterType::New();
FilterWatcher watcherO(writer);
writer->SetFileName(argv[2]);
writer->SetInput(output);
writer->SetUseCompression(atoi(argv[3]));
try{
writer->Update();
}
catch(itk::ExceptionObject &ex){
std::cerr << ex << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
template<typename InputComponentType, typename InputPixelType>
int dispatch_D(size_t dimensionType, int argc, char *argv[]){
int res= 0;
switch (dimensionType){
case 2:
res= DoIt<InputComponentType, InputPixelType, 2>(argc, argv);
break;
case 3:
res= DoIt<InputComponentType, InputPixelType, 3>(argc, argv);
break;
default:
std::cerr << "Error: Images of dimension " << dimensionType << " are not handled!" << std::endl;
break;
}//switch
return res;
}
template<typename InputComponentType>
int dispatch_pT(itk::ImageIOBase::IOPixelType pixelType, size_t dimensionType, int argc, char *argv[]){
int res= 0;
//http://www.itk.org/Doxygen45/html/classitk_1_1ImageIOBase.html#abd189f096c2a1b3ea559bc3e4849f658
//http://www.itk.org/Doxygen45/html/itkImageIOBase_8h_source.html#l00099
//IOPixelType:: UNKNOWNPIXELTYPE, SCALAR, RGB, RGBA, OFFSET, VECTOR, POINT, COVARIANTVECTOR, SYMMETRICSECONDRANKTENSOR, DIFFUSIONTENSOR3D, COMPLEX, FIXEDARRAY, MATRIX
switch (pixelType){
case itk::ImageIOBase::SCALAR:{
typedef InputComponentType InputPixelType;
res= dispatch_D<InputComponentType, InputPixelType>(dimensionType, argc, argv);
} break;
case itk::ImageIOBase::UNKNOWNPIXELTYPE:
default:
std::cerr << std::endl << "Error: Pixel type not handled!" << std::endl;
break;
}//switch
return res;
}
int dispatch_cT(itk::ImageIOBase::IOComponentType componentType, itk::ImageIOBase::IOPixelType pixelType, size_t dimensionType, int argc, char *argv[]){
int res= 0;
//http://www.itk.org/Doxygen45/html/classitk_1_1ImageIOBase.html#a8dc783055a0af6f0a5a26cb080feb178
//http://www.itk.org/Doxygen45/html/itkImageIOBase_8h_source.html#l00107
//IOComponentType: UNKNOWNCOMPONENTTYPE, UCHAR, CHAR, USHORT, SHORT, UINT, INT, ULONG, LONG, FLOAT, DOUBLE
switch (componentType){
case itk::ImageIOBase::UCHAR:{ // uint8_t
typedef unsigned char InputComponentType;
res= dispatch_pT<InputComponentType>(pixelType, dimensionType, argc, argv);
} break;
case itk::ImageIOBase::CHAR:{ // int8_t
typedef char InputComponentType;
res= dispatch_pT<InputComponentType>(pixelType, dimensionType, argc, argv);
} break;
case itk::ImageIOBase::USHORT:{ // uint16_t
typedef unsigned short InputComponentType;
res= dispatch_pT<InputComponentType>(pixelType, dimensionType, argc, argv);
} break;
case itk::ImageIOBase::SHORT:{ // int16_t
typedef short InputComponentType;
res= dispatch_pT<InputComponentType>(pixelType, dimensionType, argc, argv);
} break;
case itk::ImageIOBase::UINT:{ // uint32_t
typedef unsigned int InputComponentType;
res= dispatch_pT<InputComponentType>(pixelType, dimensionType, argc, argv);
} break;
case itk::ImageIOBase::INT:{ // int32_t
typedef int InputComponentType;
res= dispatch_pT<InputComponentType>(pixelType, dimensionType, argc, argv);
} break;
case itk::ImageIOBase::ULONG:{ // uint64_t
typedef unsigned long InputComponentType;
res= dispatch_pT<InputComponentType>(pixelType, dimensionType, argc, argv);
} break;
case itk::ImageIOBase::LONG:{ // int64_t
typedef long InputComponentType;
res= dispatch_pT<InputComponentType>(pixelType, dimensionType, argc, argv);
} break;
case itk::ImageIOBase::FLOAT:{ // float32
typedef float InputComponentType;
res= dispatch_pT<InputComponentType>(pixelType, dimensionType, argc, argv);
} break;
case itk::ImageIOBase::DOUBLE:{ // float64
typedef double InputComponentType;
res= dispatch_pT<InputComponentType>(pixelType, dimensionType, argc, argv);
} break;
case itk::ImageIOBase::UNKNOWNCOMPONENTTYPE:
default:
std::cerr << "unknown component type" << std::endl;
break;
}//switch
return res;
}
////from http://itk-users.7.n7.nabble.com/Pad-image-with-0-but-keep-its-type-what-ever-it-is-td27442.html
//namespace itk{
// Description:
// Get the PixelType and ComponentType from fileName
void GetImageType (std::string fileName,
itk::ImageIOBase::IOPixelType &pixelType,
itk::ImageIOBase::IOComponentType &componentType,
size_t &dimensionType
){
typedef itk::Image<char, 1> ImageType; //template initialization parameters need to be given but can be arbitrary here
itk::ImageFileReader<ImageType>::Pointer imageReader= itk::ImageFileReader<ImageType>::New();
imageReader->SetFileName(fileName.c_str());
imageReader->UpdateOutputInformation();
if(!imageReader->GetImageIO()->CanStreamRead())
std::cerr << "Cannot stream the reading of the input. Streaming will be inefficient!" << std::endl;
pixelType = imageReader->GetImageIO()->GetPixelType();
componentType = imageReader->GetImageIO()->GetComponentType();
dimensionType= imageReader->GetImageIO()->GetNumberOfDimensions();
std::cerr << std::endl << "dimensions: " << dimensionType << std::endl;
std::cerr << "component type: " << imageReader->GetImageIO()->GetComponentTypeAsString(componentType) << std::endl;
std::cerr << "component size: " << imageReader->GetImageIO()->GetComponentSize() << std::endl;
std::cerr << "pixel type (string): " << imageReader->GetImageIO()->GetPixelTypeAsString(imageReader->GetImageIO()->GetPixelType()) << std::endl;
std::cerr << "pixel type: " << pixelType << std::endl << std::endl;
}
int main(int argc, char *argv[]){
if ( argc != 6 ){
std::cerr << "Missing Parameters: "
<< argv[0]
<< " Input_Image"
<< " Output_Image"
<< " compress"
<< " radius"
<< " useImageSpacing"
<< std::endl;
return EXIT_FAILURE;
}
itk::ImageIOBase::IOPixelType pixelType;
typename itk::ImageIOBase::IOComponentType componentType;
size_t dimensionType;
try {
GetImageType(argv[1], pixelType, componentType, dimensionType);
}//try
catch( itk::ExceptionObject &excep){
std::cerr << argv[0] << ": exception caught !" << std::endl;
std::cerr << excep << std::endl;
return EXIT_FAILURE;
}
return dispatch_cT(componentType, pixelType, dimensionType, argc, argv);
}