ITK、VTK 读取 DiCOM 文件信息,并交互预览
DICOM 文件介绍
DICOM 全名叫 Digital Imaging and Communications in Medicine(医学数字图像与通信),是医学图像和相关信息的过节标准,也定义了临床所需数据医学图像格式;
DICOM 数据格式包含大量属性,例如病人名字、ID、年龄等基本信息,这些信息从储存在 “头文件” 中,这种储存方式避免了病人与图像误配;
一个病人样本 包含多个 DICOM 子文件,单个 DICOM 对象只包含具有一帧像素数据,帧与帧之间以序列形式存在,可以来回切换形成类似于电影放映的情景,类似于把一个3D 图像扫描成像,沿着垂直方向等厚度分割得到
ITK 对 DICOM 数据进行读取
ITK 根据 DICOM 文件读取方式有两种类型,单张 DICOM 读取或 序列 DICOM 读取
ITK 读取单张 DICOM 文件
- itkImageSeriesReader;
- itkGDCMImageIO;
读取后可以通过 itkGCOMImageIO 中 Get*** 函数获取病人相关信息,并打印输出,这里需要注意一点,DICOM 对应的数据类型为 signed short;核心代码:
string DirectoryPath = "D:/Data_Training/Dicom_data/215020265/AT000002.152046493.dcm";
typedef signed short InternalPixelType;
const unsigned int Dimension = 2;
using InternalImageType = itk::Image<InternalPixelType, Dimension>;
typedef itk::Image<InternalPixelType, Dimension> ImageType;
typedef itk::ImageSeriesReader<ImageType> ReaderType;
typedef itk::GDCMImageIO ImageIOType;
ImageIOType::Pointer gdcmIO = ImageIOType::New();
ReaderType::Pointer reader = ReaderType::New();
reader->SetImageIO(gdcmIO);
reader->SetFileNames(DirectoryPath);
// Reading Data;
try
{
reader->Update();
reader->GetMetaDataDictionary();
gdcmIO->GetMetaDataDictionary();// 读取头文件信息;
//信息赋值
char* name = new char[50];
char* patientID = new char[50];
char* time = new char[50];
char* manufacture = new char[50];
char* modility = new char[50];
char* hospital = new char[50];
char* sex = new char[50];
char* age = new char[50];
char* description = new char[100];
int pixelType = gdcmIO->GetPixelType();
int componentType = gdcmIO->GetComponentType();
int fileType = gdcmIO->GetFileType();
ImageIOType::ByteOrder byteOrder;
byteOrder = gdcmIO->GetByteOrder();
unsigned int dim = 0;
gdcmIO->GetDimensions(dim);
ImageIOType::SizeType imgsize;
imgsize = gdcmIO->GetImageSizeInPixels();
int componetSize = gdcmIO->GetComponentSize();
int dimension = gdcmIO->GetNumberOfDimensions();
int ori = 0;
gdcmIO->GetOrigin(ori);
int spa = 0;
gdcmIO->GetPatientSex(sex);
gdcmIO->GetPatientAge(age);
gdcmIO->GetStudyDescription(description);
gdcmIO->GetSpacing(spa);
gdcmIO->GetPatientName(name);
gdcmIO->GetModality(modility);
gdcmIO->GetPatientID(patientID);
gdcmIO->GetManufacturer(manufacture);
gdcmIO->GetStudyDate(time);
ImageIOType::TCompressionType compressType;
compressType = gdcmIO->GetCompressionType();
gdcmIO->GetInstitution(hospital);
ImageType::SpacingType spacetype;
spacetype = reader->GetOutput()->GetSpacing();
ImageType::PointType origin;
origin = reader->GetOutput()->GetOrigin();
cout << "name:" << name << endl;
cout << "age:" << age << endl;
cout << "sex:" << sex << endl;
cout << "description:" << description << endl;
cout << "hospital:" << hospital << endl;
cout << "modility:" << modility << endl;
cout << "manfacture:" << manufacture << endl;
cout << "dim:" << dim << endl;
cout << "origin:" << origin << endl;
cout << "Time:" << time << endl;
cout << "patientID:" << patientID << endl;
}
catch (const itk::ExceptionObject& excp)
{
cout << " Reading Exceptaion Caught" << endl;
cout << excp.what() << endl;
return EXIT_FAILURE;
}
ITK 读取序列 DICOM 文件
- itkImageSeriesReader;
- itkGDCMImageIO;
- itkGDCMSeriesFileNames;
与单张读取不同的是,多了一个 itkGDCMSeriesFileNames 类,用于打包 DICOM 序列文件,再传入 GDCMImageIO 进行读取
string DirectoryPath = "D:/Data_Training/Dicom_data/215020265";
typedef signed short InternalPixelType;
const unsigned int Dimension = 3;
using InternalImageType = itk::Image<InternalPixelType, Dimension>;
typedef itk::Image<InternalPixelType, Dimension> ImageType;
typedef itk::ImageSeriesReader<ImageType> ReaderType;
typedef itk::GDCMImageIO ImageIOType;
typedef itk::GDCMSeriesFileNames NamesGeneratorType;
ImageIOType::Pointer gdcmIO = ImageIOType::New();
NamesGeneratorType::Pointer namesGenerator = NamesGeneratorType::New();
namesGenerator->SetInputDirectory(DirectoryPath);
const ReaderType::FileNamesContainer & filenames =
namesGenerator->GetInputFileNames();
std::size_t numberOfFileNames = filenames.size();
std::cout << numberOfFileNames << std::endl;
ReaderType::Pointer reader = ReaderType::New();
reader->SetImageIO(gdcmIO);
reader->SetFileNames(filenames);
// Reading Data;
try
{
reader->Update();
reader->GetMetaDataDictionary();
gdcmIO->GetMetaDataDictionary();// 读取头文件信息;
//信息赋值
char* name = new char[50];
char* patientID = new char[50];
char* time = new char[50];
char* manufacture = new char[50];
char* modility = new char[50];
char* hospital = new char[50];
char* sex = new char[50];
char* age = new char[50];
char* description = new char[100];
int pixelType = gdcmIO->GetPixelType();
int componentType = gdcmIO->GetComponentType();
int fileType = gdcmIO->GetFileType();
ImageIOType::ByteOrder byteOrder;
byteOrder = gdcmIO->GetByteOrder();
unsigned int dim = 0;
gdcmIO->GetDimensions(dim);
ImageIOType::SizeType imgsize;
imgsize = gdcmIO->GetImageSizeInPixels();
int componetSize = gdcmIO->GetComponentSize();
int dimension = gdcmIO->GetNumberOfDimensions();
int ori = 0;
gdcmIO->GetOrigin(ori);
int spa = 0;
gdcmIO->GetPatientSex(sex);
gdcmIO->GetPatientAge(age);
gdcmIO->GetStudyDescription(description);
gdcmIO->GetSpacing(spa);
gdcmIO->GetPatientName(name);
gdcmIO->GetModality(modility);
gdcmIO->GetPatientID(patientID);
gdcmIO->GetManufacturer(manufacture);
gdcmIO->GetStudyDate(time);
ImageIOType::TCompressionType compressType;
compressType = gdcmIO->GetCompressionType();
gdcmIO->GetInstitution(hospital);
ImageType::SpacingType spacetype;
spacetype = reader->GetOutput()->GetSpacing();
ImageType::PointType origin;
origin = reader->GetOutput()->GetOrigin();
cout << "name:" << name << endl;
cout << "age:" << age << endl;
cout << "sex:" << sex << endl;
cout << "description:" << description << endl;
cout << "hospital:" << hospital << endl;
cout << "modility:" << modility << endl;
cout << "manfacture:" << manufacture << endl;
cout << "dim:" << dim << endl;
cout << "origin:" << origin << endl;
cout << "Time:" << time << endl;
cout << "patientID:" << patientID << endl;
}
catch (const itk::ExceptionObject& excp)
{
cout << " Reading Exceptaion Caught" << endl;
cout << excp.what() << endl;
return EXIT_FAILURE;
}
运行结果如下:
除了上面病人年龄、性别、ID 基本信息之外,用 itkGDCMImageIO 获取更多病人相关信息,这里从itkGDCMImageIO.h 源码中获取到了下面函数,大家可以参考一下:
VTK 读取 DICOM
vtk 读取 DICOM 就比较简单了,因为 VTK 封装的有相关 DICOM 读取类: vtkDICOMImageReader,可直接进行操作;
VTK读取单张DICOM文件并进行预览
- vtkDICOMImageReader
#include<vtkSmartPointer.h>
#include<vtkImageViewer2.h>
#include<vtkDICOMImageReader.h>
#include<vtkRenderWindow.h>
#include<vtkRenderWindowInteractor.h>
#include<vtkRenderer.h>
#include<iostream>
#include<string.h>
#include<vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
int main()
{
using namespace std;
string OpenPath = "D:/Data_Training/Dicom_data/215020265/AT000001.151955287.dcm";
vtkSmartPointer<vtkDICOMImageReader> reader =
vtkSmartPointer<vtkDICOMImageReader>::New();
reader->SetFileName(OpenPath.c_str());
reader->Update();
vtkSmartPointer<vtkImageViewer2> imageViewer =
vtkSmartPointer<vtkImageViewer2>::New();
imageViewer->SetInputConnection(reader->GetOutputPort());
vtkSmartPointer<vtkRenderWindowInteractor> renWin =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
imageViewer->SetupInteractor(renWin);
imageViewer->Render();
imageViewer->GetRenderer()->ResetCamera();
imageViewer->Render();
renWin->Start();
return EXIT_SUCCESS;
}
因为加入了一个交互窗口,因此可利用放大缩小
VTK读取序列DICOM文件并进行预览
- vtkDICOMImageReader
用法与单张读取相似,区别是序列图这里读取的文件夹,而单张读取的是单张 DICOM;
#include<vtkSmartPointer.h>
#include<vtkImageViewer2.h>
#include<vtkDICOMImageReader.h>
#include<vtkRenderWindow.h>
#include<vtkRenderWindowInteractor.h>
#include<vtkRenderer.h>
#include<vtkTextProperty.h>
#include<vtkTextMapper.h>
#include<vtkActor2D.h>
#include<vtkInteractorStyleImage.h>
#include<vtkObjectFactory.h>
#include<iostream>
#include<string.h>
#include<vtkAutoInit.h>
#include<sstream>
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingFreeType)
using namespace std;
class StatusMessage{
public:
static std::string Format(int slice, int maxSlice)
{
std::stringstream tmp;
tmp << "Slice Number " << slice + 1 << "/" << maxSlice + 1;
return tmp.str();
}
};
// Define own interation style;
class myVtkInteractorStyleImage : public vtkInteractorStyleImage
{
public:
static myVtkInteractorStyleImage* New();
vtkTypeMacro(myVtkInteractorStyleImage, vtkInteractorStyleImage);
protected:
vtkImageViewer2* _ImageViewer;
vtkTextMapper* _StatusMapper;
int _Slice;
int _MinSlice;
int _MaxSlice;
public:
void SetImageViewer(vtkImageViewer2* imageViwer)
{
_ImageViewer = imageViwer;
_MinSlice = imageViwer->GetSliceMin();
_MaxSlice = imageViwer->GetSliceMax();
_Slice = _MinSlice;
cout << "Slice : Min = " << _MinSlice << ", Max = " << _MaxSlice << endl;
}
void SetStatusMapper(vtkTextMapper* statusMapper)
{
_StatusMapper = statusMapper;
}
protected:
void MoveSliceForward()
{
if (_Slice<_MaxSlice)
{
_Slice += 1;
cout << " MoveSliceForward::Slice = " << _Slice << endl;
_ImageViewer->SetSlice(_Slice);
std::string msg = StatusMessage::Format(_Slice, _MaxSlice);
_StatusMapper->SetInput(msg.c_str());
_ImageViewer->Render();
}
}
void MoveSliceBackward()
{
if(_Slice>_MinSlice)
{
_Slice -= 1;
cout << "MoveSliceBackward::Slice = " << _Slice << endl;
_ImageViewer->SetSlice(_Slice);
std::string msg = StatusMessage::Format(_Slice, _MaxSlice);
_StatusMapper->SetInput(msg.c_str());
_ImageViewer->Render();
}
}
virtual void OnKeyDown()
{
string key = this->GetInteractor()->GetKeySym();
if (key.compare("Up") == 0)
{
MoveSliceForward();
}
else if (key.compare("Down") == 0)
{
MoveSliceBackward();
}
vtkInteractorStyleImage::OnKeyDown();
}
virtual void OnMouseWheelForward()
{
MoveSliceForward();
}
virtual void OnMouseWheelBackward()
{
if (_Slice > _MinSlice)
{
MoveSliceBackward();
}
}
};
vtkStandardNewMacro(myVtkInteractorStyleImage);
int main()
{
using namespace std;
string OpenPath = "D:/Data_Training/Dicom_data/215020265/";
vtkSmartPointer<vtkDICOMImageReader> reader =
vtkSmartPointer<vtkDICOMImageReader>::New();
reader->SetDirectoryName(OpenPath.c_str());
reader->Update();
vtkSmartPointer<vtkImageViewer2> imageViewer =
vtkSmartPointer<vtkImageViewer2>::New();
imageViewer->SetInputConnection(reader->GetOutputPort());
//Slice Status
vtkSmartPointer<vtkTextProperty> sliceTextProp =
vtkSmartPointer<vtkTextProperty>::New();
sliceTextProp->SetFontFamilyToCourier();
sliceTextProp->SetFontSize(20);
sliceTextProp->SetVerticalJustificationToBottom();
sliceTextProp->SetJustificationToLeft();
vtkSmartPointer<vtkTextMapper> sliceTextMapper =
vtkSmartPointer<vtkTextMapper>::New();
string msg = StatusMessage::Format(imageViewer->GetSliceMin(), imageViewer->GetSliceMax());
sliceTextMapper->SetInput(msg.c_str());
cout << "msg" << msg.c_str() << endl;
sliceTextMapper->SetTextProperty(sliceTextProp);
vtkSmartPointer<vtkActor2D> sliceTextActor =
vtkSmartPointer<vtkActor2D>::New();
sliceTextActor->SetMapper(sliceTextMapper);
sliceTextActor->SetPosition(15, 10);
//usage hint message;
vtkSmartPointer<vtkTextProperty> usageTextProp =
vtkSmartPointer<vtkTextProperty>::New();
usageTextProp->SetFontFamilyToCourier();
usageTextProp->SetFontSize(14);
usageTextProp->SetVerticalJustificationToTop();
usageTextProp->SetJustificationToLeft();
vtkSmartPointer<vtkTextMapper> usageTextMapper =
vtkSmartPointer<vtkTextMapper>::New();
usageTextMapper->SetInput("Slice with mouse wheel\n or Up/Down-Key\n =Zoom weh pressed right\n mouse button while dragging");
usageTextMapper->SetTextProperty(usageTextProp);
vtkSmartPointer<vtkActor2D> usageTextActor =
vtkSmartPointer <vtkActor2D>::New();
usageTextActor->SetMapper(usageTextMapper);
usageTextActor->GetPositionCoordinate()->SetCoordinateSystemToNormalizedDisplay();
usageTextActor->GetPositionCoordinate()->SetValue(0.05, 0.95);
vtkSmartPointer<myVtkInteractorStyleImage> myInteractorStyle =
vtkSmartPointer<myVtkInteractorStyleImage>::New();
myInteractorStyle->SetImageViewer(imageViewer);
myInteractorStyle->SetStatusMapper(sliceTextMapper);
vtkSmartPointer<vtkRenderWindowInteractor> renWin =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
imageViewer->SetupInteractor(renWin);
renWin->SetInteractorStyle(myInteractorStyle);
imageViewer->GetRenderer()->AddActor2D(sliceTextActor);
imageViewer->GetRenderer()->AddActor2D(usageTextActor);
imageViewer->Render();
imageViewer->GetRenderer()->ResetCamera();
imageViewer->Render();
renWin->Start();
return EXIT_SUCCESS;
}
代码摘抄于官方,这里定义了一个自定义交互类,可以通过滑动鼠标或者按键来完成切片更换。并且通过类 vtkTextMapper 在界面上添加了两个文本注释,方便引导用户
预览效果如下:
Reference:
1,https://itk.org/Doxygen/html/classitk_1_1ImageSeriesReader.html
2,https://lorensen.github.io/VTKExamples/site/Cxx/IO/ReadDICOMSeries/