-
Notifications
You must be signed in to change notification settings - Fork 1
/
Common.h
215 lines (163 loc) · 7.37 KB
/
Common.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
/*-
* Nathan Lay
* Imaging Biomarkers and Computer-Aided Diagnosis Laboratory
* National Institutes of Health
* March 2017
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef COMMON_H
#define COMMON_H
#include <ctime>
#include <iostream>
#include <string>
#include <vector>
#include "itkImage.h"
#include "itkGDCMImageIO.h"
#include "itkGDCMSeriesFileNames.h"
#include "itkImageSeriesReader.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
void Trim(std::string &strString);
std::vector<std::string> SplitString(const std::string &strValue, const std::string &strDelim);
std::time_t ParseDICOMDateTime(const std::string &strAcquisitionDate, const std::string &strAcquisitionTime);
bool FileExists(const std::string &strPath);
bool IsFolder(const std::string &strPath);
bool RmDir(const std::string &strPath);
bool MkDir(const std::string &strPath);
bool Unlink(const std::string &strPath);
bool Copy(const std::string &strFrom, const std::string &strTo, bool bReplace = false);
bool Rename(const std::string &strFrom, const std::string &strTo, bool bReplace = false);
void USleep(unsigned int uiMicroSeconds);
std::string BaseName(std::string strPath);
std::string DirName(std::string strPath);
void SanitizeFileName(std::string &strFileName); // Does NOT operate on paths
void FindFiles(const char *p_cDir, const char *p_cPattern, std::vector<std::string> &vFiles, bool bRecursive = false);
void FindFolders(const char *p_cDir, const char *p_cPattern, std::vector<std::string> &vFolders, bool bRecursive = false);
void FindDicomFolders(const char *p_cDir, const char *p_cPattern, std::vector<std::string> &vFolders, bool bRecursive = false);
// Use LoadImg since Windows #defines LoadImage ... lame
template<typename PixelType, unsigned int Dimension>
typename itk::Image<PixelType, Dimension>::Pointer LoadImg(const std::string &strPath);
template<typename PixelType, unsigned int Dimension>
bool SaveImg(typename itk::Image<PixelType, Dimension>::Pointer p_clImage, const std::string &strPath, bool bCompress = true);
template<typename PixelType, unsigned int Dimension>
typename itk::Image<PixelType, Dimension>::Pointer LoadDicomImage(const std::string &strPath, const std::string &strSeriesUID = std::string());
template<typename PixelType, unsigned int Dimension>
typename itk::Image<PixelType, Dimension>::Pointer LoadImg(const std::string &strPath) {
typedef itk::Image<PixelType, Dimension> ImageType;
typedef itk::ImageFileReader<ImageType> ReaderType;
typename ReaderType::Pointer p_clReader = ReaderType::New();
p_clReader->SetFileName(strPath);
try {
p_clReader->Update();
}
catch (itk::ExceptionObject &e) {
std::cerr << "Error: " << e << std::endl;
return typename ImageType::Pointer();
}
return p_clReader->GetOutput();
}
template<typename PixelType, unsigned int Dimension>
bool SaveImg(typename itk::Image<PixelType, Dimension>::Pointer p_clImage, const std::string &strPath, bool bCompress) {
typedef itk::Image<PixelType, Dimension> ImageType;
typedef itk::ImageFileWriter<ImageType> WriterType;
typename WriterType::Pointer p_clWriter = WriterType::New();
p_clWriter->SetFileName(strPath);
p_clWriter->SetUseCompression(bCompress);
p_clWriter->SetInput(p_clImage);
try {
p_clWriter->Update();
}
catch (itk::ExceptionObject &e) {
std::cerr << "Error: " << e << std::endl;
return false;
}
return true;
}
template<typename PixelType, unsigned int Dimension>
typename itk::Image<PixelType, Dimension>::Pointer LoadDicomImage(const std::string &strPath, const std::string &strSeriesUID) {
typedef itk::Image<PixelType, Dimension> ImageType;
typedef itk::GDCMImageIO ImageIOType;
typedef itk::GDCMSeriesFileNames FileNameGeneratorType;
if (!FileExists(strPath)) // File or folder must exist
return typename ImageType::Pointer();
ImageIOType::Pointer p_clImageIO = ImageIOType::New();
p_clImageIO->LoadPrivateTagsOn();
p_clImageIO->KeepOriginalUIDOn();
if (Dimension == 2) {
// Read a 2D image
typedef itk::ImageFileReader<ImageType> ReaderType;
if (IsFolder(strPath)) // Must be a file
return typename ImageType::Pointer();
if (!p_clImageIO->CanReadFile(strPath.c_str()))
return typename ImageType::Pointer();
typename ReaderType::Pointer p_clReader = ReaderType::New();
p_clReader->SetImageIO(p_clImageIO);
p_clReader->SetFileName(strPath);
try {
p_clReader->Update();
}
catch (itk::ExceptionObject &e) {
std::cerr << "Error: " << e << std::endl;
return typename ImageType::Pointer();
}
return p_clReader->GetOutput();
}
// Passed a file, read the series UID (ignore the one provided, if any)
if (!IsFolder(strPath)) {
if (!p_clImageIO->CanReadFile(strPath.c_str()))
return typename ImageType::Pointer();
p_clImageIO->SetFileName(strPath.c_str());
try {
p_clImageIO->ReadImageInformation();
}
catch (itk::ExceptionObject &e) {
std::cerr << "Error: " << e << std::endl;
return typename ImageType::Pointer();
}
const itk::MetaDataDictionary &clDicomTags = p_clImageIO->GetMetaDataDictionary();
std::string strTmpSeriesUID;
if (!itk::ExposeMetaData(clDicomTags, "0020|000e", strTmpSeriesUID))
return typename ImageType::Pointer();
Trim(strTmpSeriesUID);
return LoadDicomImage<PixelType, Dimension>(DirName(strPath), strTmpSeriesUID); // Call this function again
}
FileNameGeneratorType::Pointer p_clFileNameGenerator = FileNameGeneratorType::New();
// Use the ACTUAL series UID ... not some custom ITK concatenations of lots of junk.
p_clFileNameGenerator->SetUseSeriesDetails(false);
p_clFileNameGenerator->SetDirectory(strPath);
if (strSeriesUID.empty()) {
// Passed a folder but no series UID ... pick the first series UID
const FileNameGeneratorType::SeriesUIDContainerType &vSeriesUIDs = p_clFileNameGenerator->GetSeriesUIDs();
if (vSeriesUIDs.empty())
return typename ImageType::Pointer();
// Use first series UID
return LoadDicomImage<PixelType, Dimension>(strPath, vSeriesUIDs[0]);
}
const FileNameGeneratorType::FileNamesContainerType &vDicomFiles = p_clFileNameGenerator->GetFileNames(strSeriesUID);
if (vDicomFiles.empty())
return typename ImageType::Pointer();
// Read 3D or higher (but 4D probably doesn't work correctly)
typedef itk::ImageSeriesReader<ImageType> ReaderType;
typename ReaderType::Pointer p_clReader = ReaderType::New();
p_clReader->SetImageIO(p_clImageIO);
p_clReader->SetFileNames(vDicomFiles);
try {
p_clReader->Update();
}
catch (itk::ExceptionObject &e) {
std::cerr << "Error: " << e << std::endl;
return typename ImageType::Pointer();
}
return p_clReader->GetOutput();
}
#endif // !COMMON_H