-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcitkdicom.cpp
More file actions
295 lines (226 loc) · 9.06 KB
/
citkdicom.cpp
File metadata and controls
295 lines (226 loc) · 9.06 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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include "citkdicom.h"
#include <iostream>
// Test
#include <itkImageSeriesWriter.h>
#include <itkTileImageFilter.h>
CITKDicom::CITKDicom() :
m_DicomLoaded(false)
{
}
const std::string& CITKDicom::GetDescription()
{
static const std::string desc{"ITK DICOM: GDCM"};
return desc;
}
void CITKDicom::Clean()
{
m_DicomLoaded = false;
m_DicomImage4D = nullptr;
m_DicomDir.clear();
m_dicomReader = nullptr;
}
void CITKDicom::OpenDicomDir(const std::string& strdir)
{
/** Clean the Old Stuff **/
Clean();
/** Save the Dicom dir **/
m_DicomDir = strdir;
/** Initialize an GDCM IO Object **/
auto gdcmIO = GDCMImageIOType::New();
/** Get the filenames in the series **/
auto inputNames = InputNamesGeneratorType::New();
inputNames->SetLoadPrivateTags(true);
inputNames->SetInputDirectory(strdir);
/** Initialize and Dicom Reader and read the Image Series **/
m_dicomReader = SeriesReaderType::New();
try
{
const auto& filenames = inputNames->GetInputFileNames();
// Note: Code Hack To allow Private Tag Loading .
gdcmIO->SetLoadPrivateTags(true);
m_dicomReader->SetImageIO( gdcmIO );
m_dicomReader->SetFileNames( filenames );
m_dicomReader->Update();
auto fitr = filenames.begin();
while (fitr != filenames.end())
{
std::cout << "FileName is " << *fitr << std::endl;
fitr++;
}
m_DicomLoaded = true;
m_DicomImage4D = m_dicomReader->GetOutput();
const auto fileDict = (*m_dicomReader->GetMetaDataDictionaryArray())[0];
// Get some information on the header
itk::MetaDataDictionary::ConstIterator itr = fileDict->Begin();
itk::MetaDataDictionary::ConstIterator end = fileDict->End();
std::string value;
std::string label;
// while (itr != end)
// {
// label = itr->first;
// value = dynamic_cast<itk::MetaDataObject<std::string> *>(itr->second.GetPointer())->GetMetaDataObjectValue();
// //std::cout << " Element " << label << " has value " << value << std::endl;
// itr++;
// }
std::string entryId = "0020|0013";
itr = fileDict->Find( entryId );
if( itr == end )
{
std::cerr << "Tag " << entryId;
std::cerr << " not found in the DICOM header" << std::endl;
return ;
}
else
{
label = itr->first;
value = dynamic_cast<itk::MetaDataObject<std::string> *>(itr->second.GetPointer())->GetMetaDataObjectValue();
std::cout << " Found Element " << label << " has value " << value << std::endl;
}
// Other Relevant Information
InputImage4DType::PointType pType = m_DicomImage4D->GetOrigin();
std::cout << "Origin is " << pType[0] << " " << pType[1] << " " << pType[2] << " " << pType[3] << std::endl;
InputImage4DType::IndexType iType;
m_DicomImage4D->TransformPhysicalPointToIndex(pType, iType);
std::cout << "Index of First Point is " << iType[0] << " " << iType[1] << " " << iType[2] << std::endl;
InputImage4DType::OffsetType oType;
InputImage4DType::DirectionType dType = m_DicomImage4D->GetDirection();
InputImage4DType::OffsetType::OffsetValueType ovType;
InputImage4DType::SpacingType sType = m_DicomImage4D->GetSpacing();
ovType = m_DicomImage4D->ComputeOffset(iType);
std::cout << "Offset Value is : " << ovType << std::endl;
std::cout << "Direction " << dType[0][0] << " " << dType[1][0] << " " << dType[2][0] << " " << dType[0][1] << " " << dType[1][1] << " " << dType[2][1] << std::endl;
std::cout << "Spacing " << sType[0] << " " << sType[1] << " " << sType[2] << std::endl;
/*ExtractVolumefrom4D(0);
ExtractVolumefrom3D(0);
//Tile it back
typedef itk::TileImageFilter< Input2DImageType, OutputImageType > TilerType;
TilerType::Pointer tiler = TilerType::New();
itk::FixedArray< unsigned int, OutputDimension > layout;
layout[0] = 1;
layout[1] = 1;
layout[2] = 0;
tiler->SetLayout( layout );
unsigned int inputImageNumber = 0;
for (int ii = 0; ii < 30; ii++)
{
ExtractVolumefrom3D(ii);
m_DicomImage2D->DisconnectPipeline();
tiler->SetInput(inputImageNumber++, m_DicomImage2D);
}
PixelDataType filler = 128;
tiler->SetDefaultPixelValue( filler );
tiler->Update();
InputImageType::PointType pType1 = tiler->GetOutput()->GetOrigin();
std::cout << "Origin is " << pType1[0] << " " << pType1[1] << " " << pType1[2] << std::endl;
// Test DICOM Save from GDCM
typedef itk::Image< PixelDataType, 2 > TestOutputImageType;
typedef itk::ImageSeriesWriter< InputImageType, TestOutputImageType > SeriesWriterType;
SeriesWriterType::Pointer writer = SeriesWriterType::New();
writer->SetInput(tiler->GetOutput());
writer->SetImageIO(gdcmIO);
inputNames->SetOutputDirectory("C:\\Workspace\\TestGDCMData");
writer->SetFileNames(inputNames->GetOutputFileNames());
writer->SetMetaDataDictionaryArray(m_dicomReader->GetMetaDataDictionaryArray());
writer->Update();*/
}
catch (itk::ExceptionObject &excp)
{
/** Exception: Dir does not contain DICOM series **/
std::cerr << "Error--> (" << __FILE__ << ":" << __LINE__ << ")" << std::endl;
std::cerr << "Exception in series " << excp << std::endl;
m_DicomLoaded = false;
return;
}
}
InputImage4DType::Pointer CITKDicom::GetImage4D()
{
return (m_DicomLoaded) ? m_DicomImage4D : nullptr;
}
InputImageType::Pointer CITKDicom::GetImage3D(int iIndex)
{
/** CCheck if Data is loaded **/
if (!m_DicomLoaded)
return nullptr;
/** Check if 3D data requested is valid **/
if (iIndex > Get4thDimension())
return nullptr;
/** Extract 3D data **/
if (ExtractVolumefrom4D(iIndex))
return m_DicomImage3D;
else
return nullptr;
}
bool CITKDicom::ExtractVolumefrom4D(unsigned int iIndex)
{
/** Check if 4D Data is in memory **/
if (m_DicomImage4D.IsNull())
{
std::cerr << "Error--> (" << __FILE__ << ":" << __LINE__ << ")" << std::endl;
std::cerr << "No 4D Image was found in memory " << std::endl;
return false;
}
/** Extract the 3D data **/
using ExtractImageFilterType = itk::ExtractImageFilter<InputImage4DType, InputImageType>;
auto extractfilter = ExtractImageFilterType::New();
auto inputRegion = m_DicomImage4D->GetLargestPossibleRegion();
auto inputSize = inputRegion.GetSize();
auto inputIndex = inputRegion.GetIndex();
extractfilter->InPlaceOn();
extractfilter->SetDirectionCollapseToSubmatrix();
/** Set Slice to Extract **/
inputSize[3] = 0;
inputIndex[3] = iIndex;
InputImage4DType::RegionType extractRegion;
extractRegion.SetSize(inputSize);
extractRegion.SetIndex(inputIndex);
extractfilter->SetExtractionRegion(extractRegion);
extractfilter->SetInput(m_DicomImage4D);
extractfilter->Update();
m_DicomImage3D = extractfilter->GetOutput();
return true;
}
bool CITKDicom::ExtractVolumefrom3D(unsigned int iIndex)
{
/** Check if 4D Data is in memory **/
if (m_DicomImage3D.IsNull())
{
std::cerr << "Error--> (" << __FILE__ << ":" << __LINE__ << ")" << std::endl;
std::cerr << "No 4D Image was found in memory " << std::endl;
return false;
}
/** Extract the 3D data **/
using ExtractImageFilterType = itk::ExtractImageFilter<InputImageType, Input2DImageType>;
auto extractfilter = ExtractImageFilterType::New();
auto inputRegion = m_DicomImage3D->GetLargestPossibleRegion();
auto inputSize = inputRegion.GetSize();
auto inputIndex = inputRegion.GetIndex();
extractfilter->InPlaceOn();
extractfilter->SetDirectionCollapseToSubmatrix();
/** Set Slice to Extract **/
inputSize[2] = 0;
inputIndex[2] = iIndex;
InputImageType::RegionType extractRegion;
extractRegion.SetSize(inputSize);
extractRegion.SetIndex(inputIndex);
extractfilter->SetExtractionRegion(extractRegion);
extractfilter->SetInput(m_DicomImage3D);
extractfilter->Update();
m_DicomImage2D = extractfilter->GetOutput();
return true;
}
bool CITKDicom::isDicomLoaded() noexcept
{
return m_DicomLoaded;
}
const std::string& CITKDicom::GetDicomDir()
{
return m_DicomDir;
}
unsigned int CITKDicom::Get4thDimension()
{
/** get the 4th dimension of 4D data -- if not present return 0 **/
if (isDicomLoaded())
return m_DicomImage4D->GetLargestPossibleRegion().GetSize()[3];
else
return 0;
}