-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathioutils.h
127 lines (109 loc) · 3.56 KB
/
ioutils.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
#ifndef __ioutils_h_
#define __ioutils_h_
#include <itkImageFileWriter.h>
#include <itkImageFileReader.h>
#include <itkShiftScaleImageFilter.h>
#include <itkStatisticsImageFilter.h>
#include <itkNumericTraits.h>
#include <itkOrientImageFilter.h>
#include <itkSpatialOrientation.h>
int readImageInfo(std::string filename, itk::ImageIOBase::IOComponentType *ComponentType, int *dim)
{
itk::ImageIOBase::Pointer imageIO = itk::ImageIOFactory::CreateImageIO(filename.c_str(), itk::ImageIOFactory::ReadMode);
if (imageIO.IsNull())
return 0;
imageIO->SetFileName(filename.c_str());
imageIO->ReadImageInformation();
*ComponentType = imageIO->GetComponentType();
*dim = imageIO->GetNumberOfDimensions();
return(1);
}
template <class TImage>
void writeIm(const typename TImage::Pointer Im, std::string filename)
{
typedef typename itk::ImageFileWriter<TImage> WriterType;
typename WriterType::Pointer writer = WriterType::New();
writer->SetInput(Im);
try
{
writer->SetFileName(filename.c_str());
writer->Update();
}
catch(itk::ExceptionObject &ex)
{
std::cout << ex << std::endl;
std::cout << filename << std::endl;
}
}
template <class TImage, class PixType>
void writeImScale(typename TImage::Pointer Im, std::string filename)
{
const int dim = TImage::ImageDimension;
typedef typename itk::Image<PixType, dim> OutType;
typedef typename itk::ImageFileWriter<OutType> WriterType;
typedef typename itk::StatisticsImageFilter<TImage> StatsType;
typedef typename itk::ShiftScaleImageFilter<TImage, OutType> FitType;
typename StatsType::Pointer stats = StatsType::New();
typename FitType::Pointer fitter = FitType::New();
stats->SetInput(Im);
stats->Update();
float range = itk::NumericTraits<PixType >::max() -
itk::NumericTraits< PixType >::NonpositiveMin() + 1;
float mx = stats->GetMaximum();
float mn = stats->GetMinimum();
float scale = range/(mx - mn);
float shift = - mn;
fitter->SetInput(stats->GetOutput());
fitter->SetScale(scale);
fitter->SetShift(shift);
typename WriterType::Pointer writer = WriterType::New();
writer->SetInput(fitter->GetOutput());
writer->SetFileName(filename.c_str());
writer->Update();
}
template <class TImage>
typename TImage::Pointer readIm(std::string filename)
{
typedef typename itk::ImageFileReader<TImage> ReaderType;
typename ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(filename.c_str());
typename TImage::Pointer result = reader->GetOutput();
try
{
result->Update();
}
catch(itk::ExceptionObject &ex)
{
std::cout << ex << std::endl;
std::cout << filename << std::endl;
return 0;
}
result->DisconnectPipeline();
return(result);
}
template <class TImage>
typename TImage::Pointer readImOriented(std::string filename, itk::SpatialOrientation::ValidCoordinateOrientationFlags direction)
{
typedef typename itk::ImageFileReader<TImage> ReaderType;
typename ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(filename.c_str());
typedef typename itk::OrientImageFilter<TImage, TImage> ReorientType;
typename ReorientType::Pointer reorient = ReorientType::New();
reorient->SetInput(reader->GetOutput());
reorient->UseImageDirectionOn();
reorient->SetDesiredCoordinateOrientation(direction);
typename TImage::Pointer result = reorient->GetOutput();
try
{
result->Update();
}
catch(itk::ExceptionObject &ex)
{
std::cout << ex << std::endl;
std::cout << filename << std::endl;
return 0;
}
result->DisconnectPipeline();
return(result);
}
#endif