一、功能
通过ITK读取一张图片(dcm格式),然后通过vtk显示出来。
版本:VS2019
itk5.0.1
vtk 8.2.0
二、程序主要思路
1-读取dcm格式图片
2-转换为vtk可以读取的数据类型
3-设置显示参数,然后显示出来
三、代码
工程构建参见前面的博客,代码部分如下:
//保证VTK正常运行 #include "vtkAutoInit.h" VTK_MODULE_INIT(vtkRenderingOpenGL2); // VTK was built with vtkRenderingOpenGL2 VTK_MODULE_INIT(vtkInteractionStyle); #include "itkImage.h" #include "itkImageFileReader.h" #include "itkGDCMImageIO.h" #include "itkImageToVTKImageFilter.h" #include <vtkSmartPointer.h> #include <vtkImageActor.h> #include <vtkRenderer.h> #include <vtkRenderWindow.h> #include <vtkRenderWindowInteractor.h> #include <vtkInteractorStyleImage.h> int main(int argc, char * argv[]) { //设置读取图像类型 using PixelType=signed short; const unsigned int Dimension= 2 ; typedef itk::Image<PixelType, Dimension> ImageType; typedef itk::ImageFileReader<ImageType> ReaderType; //读取指针 ReaderType::Pointer reader = ReaderType::New(); //设置读取文件 reader->SetFileName("D:\\Files\\Data\\3219032438350584179-8\\DICOM\\S258070\\S20\\I10"); //创建读取DCM的GDCMIOImage类 using ImageIOType =itk::GDCMImageIO; ImageIOType::Pointer gdcmImageIO = ImageIOType::New(); reader->SetImageIO(gdcmImageIO); try { reader->Update(); } catch (itk::ExceptionObject& e) { std::cerr << "exception in file reader" << std::endl; std::cerr << e << std::endl; return EXIT_FAILURE; } //链接滤波器,转换为VTK类型 typedef itk::ImageToVTKImageFilter<ImageType> ConnectorType; ConnectorType::Pointer connector = ConnectorType::New(); connector->SetInput(reader->GetOutput()); try { connector->Update(); } catch (itk::ExceptionObject& e) { std::cerr << "exception in file reader" << std::endl; std::cerr << e << std::endl; return EXIT_FAILURE; } /* vtkImageActor在3D场景下渲染图像 */ vtkSmartPointer<vtkImageActor> actor = vtkImageActor::New(); actor->SetInputData(connector->GetOutput()); actor->InterpolateOff(); actor->Update(); vtkSmartPointer<vtkRenderer> render = vtkRenderer::New(); render->AddActor(actor); render->SetBackground(255, 1, 1);//设置窗口背景颜色 vtkSmartPointer<vtkRenderWindow> window = vtkRenderWindow::New(); window->SetSize(600, 600); //设置窗口大小 window->AddRenderer(render); window->SetWindowName("the CT picture ");//设置窗口名称 vtkSmartPointer<vtkRenderWindowInteractor> interactor = vtkRenderWindowInteractor::New(); interactor->SetRenderWindow(window); interactor->Initialize(); interactor->Start(); return 0; }
四、结果显示
在这里注意,我发现如果设置的读取的读片类型不同,显示的效果会有差异:
比如如果图片格式是:
//设置读取图像类型 using PixelType=unsigned char; const unsigned int Dimension= 2 ; typedef itk::Image<PixelType, Dimension> ImageType;
图片效果如下:
如果图片格式设置如下:
//设置读取图像类型 using PixelType=signed short; const unsigned int Dimension= 2 ; typedef itk::Image<PixelType, Dimension> ImageType;
图片显示如下:
感觉都不是很清晰。
五、参考文献
关于vtk如何现实图片:https://blog.csdn.net/shenziheng1/article/details/54565337
代码主要来源:https://blog.csdn.net/cuihaolong/article/details/53943981