欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【004】 ITK 读取 CT Dicom 数据并使用 VTK 显示

程序员文章站 2022-03-31 22:06:44
...

【004】 ITK 读取 CT Dicom 数据并使用 VTK 显示



1.CMakeList.txt

cmake_minimum_required(VERSION 3.5)
project(Image2ReadImage LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(ITK REQUIRED)
include(${ITK_USE_FILE})
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
add_executable(Image2ReadImage main.cpp)
target_link_libraries(Image2ReadImage ${ITK_LIBRARIES} ${VTK_LIBRARIES})

2.ITK 读取 CT dicom 文件并使用 VTK 显示代码实现

#include "itkImage.h"
#include "itkImageFileReader.h"

#include "vtkSmartPointer.h"
#include "itkImageToVTKImageFilter.h"
#include "vtkImageViewer2.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderWindow.h"
#include "vtkRenderer.h"

int main()
{
    using PixelType = unsigned int;
    using ImageType = itk::Image<PixelType,3>;
    using ImageReader = itk::ImageFileReader<ImageType>;
    ImageReader::Pointer reader = ImageReader::New();
    // 此处是我 CT Dicom 文件的路径,请根据实际情况修改
    reader->SetFileName("D:\\AnnjeffCode\\QtCreator\\ITK-Examples\\image000.dcm");
    reader->Update();
    ImageType::Pointer image = reader->GetOutput();
	// 到此处,使用 ITK 读取 dicom 文件已经结束
	
	// 此处开始使用 VTK 显示读取的 CT 图像
    using ConnectorType = itk::ImageToVTKImageFilter<ImageType>;  //VTK和ITK链接器
    ConnectorType::Pointer connector = ConnectorType::New();
    connector->SetInput(image);
    connector->Update();

    vtkSmartPointer<vtkRenderWindowInteractor> interactor = vtkRenderWindowInteractor::New();
    vtkSmartPointer<vtkImageViewer2> viewer = vtkSmartPointer<vtkImageViewer2>::New();
    viewer->SetInputData(connector->GetOutput());
    viewer->SetupInteractor(interactor);
    viewer->SetSize(800,600);
    viewer->SetColorWindow(255); //设置窗宽
    viewer->SetColorLevel(500);   //设置窗位
    viewer->SetSliceOrientationToXY();
    viewer->SetSlice(1);
    viewer->GetRenderer()->SetBackground(1,1,1);
    viewer->GetRenderWindow()->SetWindowName("ImageViewer2D");
    viewer->Render();
    interactor->Initialize();
    interactor->Start();

    return 0;
}