ITK笔记——读取多帧DICOM图像

上篇笔记介绍了单个DICOM图像的读取(ITK笔记——读取单个DICOM切片),接下来看如何读取DICOM序列以及使用。

读取多个DICOM图像

定义数据类型

1
2
3
using PixelType = signed short;
constexpr unsigned int Dimension = 3; // The dimension is 3, not 2
using ImageType = itk::Image<PixelType, Dimension>;

初始化GDCM接口

1
2
using ImageIOType = itk::GDCMImageIO;
ImageIOType::Pointer dicomIO = ImageIOType::New();

设置ImageFileReader

1
2
3
4
5
6
7
8
9
10
11
using NamesGeneratorType = itk::GDCMSeriesFileNames;
NamesGeneratorType::Pointer nameGenarator = NamesGeneratorType::New();
nameGenarator->SetDirectory(argv[1]);

using FilenamesContainer = std::vector<std::string>;
FilenamesContainer filenames = nameGenarator->GetInputFileNames();

using ReaderType = itk::ImageSeriesReader<ImageType>;
ReaderType::Pointer reader = ReaderType::New();
reader->SetImageIO(dicomIO);
reader->SetFileNames(filenames);

执行读取

1
reader->Update();

使用DICOM数据

获取医学信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
char name[512];
dicomIO->GetPatientName(name);
std::cout << "patient: " << name << std::endl;

using Dictionary = itk::MetaDataDictionary;
using MetaDataStringType = itk::MetaDataObject<std::string>;
const Dictionary& dic = dicomIO->GetMetaDataDictionary();
auto itr = dic.Begin();
auto end = dic.End();
std::string entryId = "0010|0010";
auto tagItr = dic.Find(entryId);
if (tagItr != end)
{
MetaDataStringType::ConstPointer entryvalue =
dynamic_cast<const MetaDataStringType*> (tagItr->second.GetPointer());
if (entryvalue)
{
std::string tagvalue = entryvalue->GetMetaDataObjectValue();
std::cout << "patient: " << tagvalue << std::endl;
}
}

转成VTK格式数据

1
2
3
4
using ImageConnector = itk::ImageToVTKImageFilter<ImageType>;
ImageConnector::Pointer imageConnector = ImageConnector::New();
imageConnector->SetInput(reader->GetOutput());
imageConnector->Update();

ReadDICMOSeries.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
#include "itkImage.h"
#include "itkGDCMImageIO.h"
#include "itkGDCMSeriesFileNames.h"
#include "itkImageSeriesReader.h"
#include "itkImageToVTKImageFilter.h"

#include <string>
#include <vector>

int main(int argc, char* argv[])
{
if (argc < 2)
{
std::cerr << "Usage: " << argv[0] << " DicomDirectory" << std::endl;
return EXIT_FAILURE;
}

using PixelType = signed short;
constexpr unsigned int Dimension = 3; // The dimension is 3, not 2

using ImageType = itk::Image<PixelType, Dimension>;

using ImageIOType = itk::GDCMImageIO;
ImageIOType::Pointer dicomIO = ImageIOType::New();

using NamesGeneratorType = itk::GDCMSeriesFileNames;
NamesGeneratorType::Pointer nameGenarator = NamesGeneratorType::New();
nameGenarator->SetDirectory(argv[1]);

using FilenamesContainer = std::vector<std::string>;
FilenamesContainer filenames = nameGenarator->GetInputFileNames();

using ReaderType = itk::ImageSeriesReader<ImageType>;
ReaderType::Pointer reader = ReaderType::New();
reader->SetImageIO(dicomIO);
reader->SetFileNames(filenames);
try
{
reader->Update();
}
catch (itk::ExceptionObject& ex)
{
std::cout << ex << std::endl;
return EXIT_FAILURE;
}

char name[512];
dicomIO->GetPatientName(name);
std::cout << "patient: " << name << std::endl;

using Dictionary = itk::MetaDataDictionary;
using MetaDataStringType = itk::MetaDataObject<std::string>;
const Dictionary& dic = dicomIO->GetMetaDataDictionary();
auto itr = dic.Begin();
auto end = dic.End();
std::string entryId = "0010|0010";
auto tagItr = dic.Find(entryId);
if (tagItr != end)
{
MetaDataStringType::ConstPointer entryvalue =
dynamic_cast<const MetaDataStringType*> (tagItr->second.GetPointer());
if (entryvalue)
{
std::string tagvalue = entryvalue->GetMetaDataObjectValue();
std::cout << "patient: " << tagvalue << std::endl;
}
}

using ImageConnector = itk::ImageToVTKImageFilter<ImageType>;
ImageConnector::Pointer imageConnector = ImageConnector::New();
imageConnector->SetInput(reader->GetOutput());
imageConnector->Update();

return EXIT_SUCCESS;
}

CMakeLists.txt

Example Download

1
2
3
4
5
6
7
8
9
cmake_minimum_required(VERSION 2.8)

project(ReadDICOMSeries)

find_package(ITK REQUIRED)
include(${ITK_USE_FILE})

add_executable(ReadDICMOSeries ReadDICMOSeries.cxx)
target_link_libraries(ReadDICMOSeries ${ITK_LIBRARIES})

DICOM tag
在这里插入图片描述

Reference

Examples/IO/DicomSeriesReadImageWrite2.cxx
在这里插入图片描述