开源库:VTK9.3.0
开发工具:Visual Studio2017
开发语言:C++
实现过程:
void initImageActor(double* Matrix, double* center, vtkSmartPointer<vtkImageCast> pImageCast,vtkSmartPointer<vtkImageReslice> imageReslice, vtkSmartPointer<vtkImageActor> actor)
{vtkSmartPointer<vtkMatrix4x4> AxialResliceMatrix = vtkSmartPointer<vtkMatrix4x4>::New();AxialResliceMatrix->DeepCopy(Matrix);AxialResliceMatrix->SetElement(0, 3, center[0]);AxialResliceMatrix->SetElement(1, 3, center[1]);AxialResliceMatrix->SetElement(2, 3, center[2]);imageReslice->SetInputConnection(pImageCast->GetOutputPort());imageReslice->SetOutputDimensionality(2);imageReslice->SetResliceAxes(AxialResliceMatrix);imageReslice->SetInterpolationModeToLinear();imageReslice->Update();actor->GetMapper()->SetInputConnection(imageReslice->GetOutputPort());actor->SetPosition(0, 0, 0);
}int main()
{vtkSmartPointer<vtkImageReslice> pImageResliceX = vtkSmartPointer<vtkImageReslice>::New();vtkSmartPointer<vtkImageReslice> pImageResliceY = vtkSmartPointer<vtkImageReslice>::New();vtkSmartPointer<vtkImageReslice> pImageResliceZ = vtkSmartPointer<vtkImageReslice>::New();vtkSmartPointer<vtkDICOMImageReader> reader = vtkSmartPointer<vtkDICOMImageReader>::New();reader->SetDirectoryName("D:\\image\\images\\CT\\20200115"); //dicom文件目录reader->Update();int extent[6];double spacing[3];double origin[3];reader->GetOutput()->GetExtent(extent);reader->GetOutput()->GetSpacing(spacing);reader->GetOutput()->GetOrigin(origin);double center[3];center[0] = origin[0] + spacing[0] * 0.5 * (extent[0] + extent[1]);center[1] = origin[1] + spacing[1] * 0.5 * (extent[2] + extent[3]);center[2] = origin[2] + spacing[2] * 0.5 * (extent[4] + extent[5]);double Axial[16] = {1, 0, 0, 0,0, 1, 0, 0,0, 0, 1, 0,0, 0, 0, 1 };double Coronal[16] = {1, 0, 0, 0,0, 0, -1, 0,0, 1, 0, 0,0, 0, 0, 1 };double Sagittal[16] = {0, 0, 1, 0,1, 0, 0, 0,0, 1, 0, 0,0, 0, 0, 1 };vtkSmartPointer<vtkImageCast> pImageCast = vtkSmartPointer<vtkImageCast>::New();pImageCast->SetInputConnection(reader->GetOutputPort());pImageCast->SetOutputScalarTypeToUnsignedChar();pImageCast->ClampOverflowOn();pImageCast->Update();vtkSmartPointer<vtkImageActor> pImageActorX = vtkSmartPointer<vtkImageActor>::New();vtkSmartPointer<vtkImageActor> pImageActorY = vtkSmartPointer<vtkImageActor>::New();vtkSmartPointer<vtkImageActor> pImageActorZ = vtkSmartPointer<vtkImageActor>::New();initImageActor(Axial, center, pImageCast, pImageResliceX, pImageActorX);initImageActor(Coronal, center, pImageCast, pImageResliceY, pImageActorY);initImageActor(Sagittal, center, pImageCast, pImageResliceZ, pImageActorZ);vtkSmartPointer<vtkRenderer> pRendererX = vtkSmartPointer<vtkRenderer>::New();vtkSmartPointer<vtkRenderer> pRendererY = vtkSmartPointer<vtkRenderer>::New();vtkSmartPointer<vtkRenderer> pRendererZ = vtkSmartPointer<vtkRenderer>::New();vtkSmartPointer<vtkRenderer> pRenderer = vtkSmartPointer<vtkRenderer>::New();vtkSmartPointer<vtkRenderWindow> pRenderWindow = vtkSmartPointer<vtkRenderWindow>::New();pRendererX->AddActor(pImageActorX);pRendererY->AddActor(pImageActorY);pRendererZ->AddActor(pImageActorZ);pRendererX->SetBackground(0, 0, 0);pRendererY->SetBackground(0, 0, 0);pRendererZ->SetBackground(0, 0, 0);pRenderer->SetBackground(0.1, 0.2, 0.4);double ltView[4] = { 0, 0, 0.5, 0.5 };double rtView[4] = { 0.5, 0, 1, 0.5 };double lbView[4] = { 0, 0.5, 0.5, 1 };double rbView[4] = { 0.5, 0.5, 1, 1 };pRenderer->SetViewport(rtView);pRendererX->SetViewport(lbView);pRendererY->SetViewport(rbView);pRendererZ->SetViewport(ltView);pRenderWindow->AddRenderer(pRendererX);pRenderWindow->AddRenderer(pRendererY);pRenderWindow->AddRenderer(pRendererZ);pRenderWindow->AddRenderer(pRenderer);vtkSmartPointer<vtkRenderWindowInteractor> pRenderWindowInteractor =vtkSmartPointer<vtkRenderWindowInteractor>::New();pRenderWindow->SetSize(600, 600);vtkSmartPointer<vtkInteractorStyleImage> imagestyle = vtkSmartPointer<vtkInteractorStyleImage>::New();pRenderWindowInteractor->SetInteractorStyle(imagestyle);pRenderWindowInteractor->SetRenderWindow(pRenderWindow);pRenderWindowInteractor->Initialize();pRenderWindow->Render();pRenderWindowInteractor->Initialize();pRenderWindowInteractor->Start();return 0;
}
运行结果: