当前位置:   article > 正文

VTK读取序列DCM格式医学图像_读取dcm 文件 cpp

读取dcm 文件 cpp

通常处理医学图像,使用VTK库,VTK库在官网下载,并经过Cmake,编译并配置好环境变量后使用,下文提供使用VTK读取三维图像并显示的代码,经过调试可运行。

  1. // RAWREAD.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include<vtkRenderWindow.h>
  5. #include<vtkRenderWindowInteractor.h>
  6. #include<vtkDICOMImageReader.h>
  7. #include<vtkMarchingCubes.h>
  8. #include<vtkPolyDataMapper.h>
  9. #include<vtkStripper.h>
  10. #include<vtkActor.h>
  11. #include<vtkProperty.h>
  12. #include<vtkCamera.h>
  13. #include<vtkOutlineFilter.h>
  14. #include<vtkOBJExporter.h>
  15. #include<vtkRenderer.h>
  16. #include<vtkMetaImageReader.h>
  17. #include<vtkInteractorStyleTrackballCamera.h>
  18. #include<iostream>
  19. #include<string.h>
  20. //需要进行初始化,否则会报错
  21. #include <vtkAutoInit.h>
  22. #include<vtkRenderingVolumeOpenGL2ObjectFactory.h>
  23. #include<vtkRenderingOpenGL2ObjectFactory.h>
  24. #include <vtkAutoInit.h>
  25. VTK_MODULE_INIT(vtkRenderingOpenGL2)
  26. VTK_MODULE_INIT(vtkInteractionStyle)
  27. VTK_MODULE_INIT(vtkRenderingFreeType)
  28. int main(void)
  29. {
  30. vtkObjectFactory::RegisterFactory(vtkRenderingOpenGL2ObjectFactory::New());
  31. vtkObjectFactory::RegisterFactory(vtkRenderingVolumeOpenGL2ObjectFactory::New());
  32. vtkSmartPointer<vtkRenderer> ren = vtkSmartPointer<vtkRenderer>::New();
  33. vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();//WINDOW;
  34. renWin->AddRenderer(ren);
  35. vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();//wininteratcor;
  36. iren->SetRenderWindow(renWin);
  37. vtkSmartPointer<vtkDICOMImageReader> reader = vtkSmartPointer<vtkDICOMImageReader>::New();
  38. reader->SetDirectoryName("D:/CT_Brain");
  39. reader->SetDataByteOrderToLittleEndian();
  40. reader->Update();
  41. cout << "读取数据完毕" << endl;
  42. cout << "The width is" << reader->GetWidth() << endl;
  43. cout << "The height is" << reader->GetHeight() << endl;
  44. cout << "The depth is" << reader->GetPixelSpacing() << endl;
  45. cout << "The Output port is" << reader->GetOutputPort() << endl;
  46. vtkSmartPointer<vtkMarchingCubes> marchingcube = vtkSmartPointer<vtkMarchingCubes>::New();
  47. marchingcube->SetInputConnection(reader->GetOutputPort());//获得读取的数据的点集;
  48. marchingcube->SetValue(0, 200);//Setting the threshold;
  49. marchingcube->ComputeNormalsOn();//计算表面法向量;
  50. vtkSmartPointer<vtkStripper> Stripper = vtkSmartPointer<vtkStripper>::New();
  51. Stripper->SetInputConnection(marchingcube->GetOutputPort());//获取三角片
  52. vtkSmartPointer<vtkPolyDataMapper> Mapper = vtkSmartPointer<vtkPolyDataMapper>::New();//将三角片映射为几何数据;
  53. Mapper->SetInputConnection(Stripper->GetOutputPort());
  54. Mapper->ScalarVisibilityOff();//
  55. vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();//Created a actor;
  56. actor->SetMapper(Mapper);//获得皮肤几何数据
  57. actor->GetProperty()->SetDiffuseColor(1, .49, .25);//设置皮肤颜色;
  58. actor->GetProperty()->SetSpecular(0.3);//反射率;
  59. actor->GetProperty()->SetOpacity(1.0);//透明度;
  60. actor->GetProperty()->SetSpecularPower(20);//反射光强度;
  61. actor->GetProperty()->SetColor(1, 0, 0);//设置角的颜色;
  62. actor->GetProperty()->SetRepresentationToWireframe();//线框;
  63. //vtkSmartPointer<vtkCamera> camera = vtkSmartPointer<vtkCamera>::New();//Setting the Camera;
  64. //camera->SetViewUp(0, 0, -1);//设置相机向上方向;
  65. //camera->SetPosition(0, 1, 0);//位置:世界坐标系,相机位置;
  66. //camera->SetFocalPoint(0, 0, 0);//焦点,世界坐标系,控制相机方向;
  67. //camera->ComputeViewPlaneNormal();//重置视平面方向,基于当前的位置和焦点;
  68. vtkSmartPointer<vtkOutlineFilter> outfilterline = vtkSmartPointer<vtkOutlineFilter>::New();
  69. outfilterline->SetInputConnection(reader->GetOutputPort());
  70. vtkSmartPointer<vtkPolyDataMapper> outmapper = vtkSmartPointer<vtkPolyDataMapper>::New();
  71. outmapper->SetInputConnection(outfilterline->GetOutputPort());
  72. vtkSmartPointer<vtkActor> OutlineActor = vtkSmartPointer<vtkActor>::New();
  73. OutlineActor->SetMapper(outmapper);
  74. OutlineActor->GetProperty()->SetColor(0, 0, 0);//线框颜色
  75. ren->AddActor(actor);
  76. ren->AddActor(OutlineActor);
  77. //ren->SetActiveCamera(camera);//设置渲染器的相机;
  78. ren->ResetCamera();
  79. ren->ResetCameraClippingRange();
  80. //camera->Dolly(1.5);//使用Dolly()方法延伸着视平面法向移动相机;
  81. ren->SetBackground(1, 1, 1);//设置背景颜色;
  82. renWin->SetSize(1000, 600);
  83. vtkInteractorStyleTrackballCamera *style = vtkInteractorStyleTrackballCamera::New();
  84. iren->SetInteractorStyle(style);
  85. renWin->Render();
  86. iren->Initialize();
  87. iren->Start();
  88. vtkSmartPointer<vtkOBJExporter> porter = vtkSmartPointer<vtkOBJExporter>::New();
  89. porter->SetFilePrefix("D:/polywrite.obj");//重建图像输出
  90. porter->SetInput(renWin);
  91. porter->Write();
  92. return EXIT_SUCCESS;
  93. }

运行结果:

 

 序列医学图像:

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号