ITK读取dicom序列图、VTK显示、ITK导出dicom序列图
运行结果
开发环境
windows10、VS2017 、VTK8.2、ITK5.0.1代码
#pragma once
#include "pch.h"
//有些头文件可能没用到
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingFreeType);
#include <vtkImageData.h>
#include <vtkProperty.h>
#include <vtkDataSetMapper.h>
#include <vtkRendererCollection.h>
#include <itkImageToVTKImageFilter.h>
#include <vtkPolyDataMapper.h>
#include <vtkDICOMImageReader.h>
#include <vtkPolyData.h>
#include <vtkPointData.h>
#include <vtkAxesActor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkSmartPointer.h>
#include <vtkImageActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkImageViewer2.h>
#include <vtkImageSliceMapper.h>
#include <vtkImageFlip.h>
#include <vtkImageGradient.h>
#include <vtkImageMagnitude.h>
#include <vtkImageShiftScale.h>
#include <vtkImageHybridMedian2D.h>
#include <vtkTextProperty.h>
#include <vtkProperty2D.h>
#include <vtkSliderWidget.h>
#include <vtkActor.h>
#include <vtkCommand.h>
#include <vtkWidgetEvent.h>
#include <vtkCallbackCommand.h>
#include <vtkWidgetEventTranslator.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkSliderRepresentation2D.h>
#include "itkGDCMSeriesFileNames.h"
#include "itkVTKImageToImageFilter.h"
#include "itkRescaleIntensityImageFilter.h"
#include "itkImageSeriesWriter.h"
#include "itkCurvatureFlowImageFilter.h"
#include "itkCastImageFilter.h"
#include "itksys/SystemTools.hxx"
#include "itkImageRegionIterator.h"
#include <itkImage.h>
#include <itkImageFileReader.h>
#include <itkImageFileWriter.h>
#include <itkPNGImageIOFactory.h>
#include <itkConnectedThresholdImageFilter.h>
#include <itkImageSeriesReader.h>
#include <itkGDCMImageIO.h>
#include "itkBinaryThresholdImageFilter.h"
#include "itkBinaryFillholeImageFilter.h"
#include "itkGrayscaleFillholeImageFilter.h"
#include "itkMeanImageFilter.h"
#include "itkGradientMagnitudeImageFilter.h"
#include "itkExtractImageFilter.h"
#include <vector>
#include <string>
using namespace std;
typedef short PixelType;
const unsigned int Dimension = 2;
typedef itk::Image< PixelType, Dimension > Input2dImageType;
typedef itk::Image< PixelType, 3 > Input3dImageType;
typedef itk::Image< PixelType, Dimension > Output2dImageType;
typedef itk::Image< PixelType, 3 > Output3dImageType;
typedef itk::GDCMImageIO ImageIOType;//GDCMImageIO读DICOM
ImageIOType::Pointer gdcmImageIO = ImageIOType::New();
using ReaderType3d = itk::ImageSeriesReader< Input3dImageType >;
ReaderType3d::Pointer reader3d = ReaderType3d::New();
typedef itk::ImageFileReader< Input2dImageType > ReaderType2d;
ReaderType2d::Pointer reader2d = ReaderType2d::New();
using NamesGeneratorType = itk::GDCMSeriesFileNames;
NamesGeneratorType::Pointer nameGenerator = NamesGeneratorType::New();
class vtkSliderCallback : public vtkCommand
{
public:
static vtkSliderCallback *New()
{
return new vtkSliderCallback;
}
virtual void Execute(vtkObject *caller, unsigned long, void*)
{
vtkSliderWidget *sliderWidget =
reinterpret_cast<vtkSliderWidget*>(caller);
this->viewer->SetSlice(static_cast<vtkSliderRepresentation *>(sliderWidget->GetRepresentation())->GetValue());
}
vtkSliderCallback() {}
vtkSmartPointer<vtkImageViewer2> viewer = nullptr;
};
Input3dImageType::Pointer read3dImage(const char *path)
{
reader3d->SetImageIO(gdcmImageIO);
nameGenerator->SetUseSeriesDetails(true);
nameGenerator->AddSeriesRestriction("0008|0021");
nameGenerator->SetDirectory(path);
using SeriesIdContainer = std::vector< std::string >;
const SeriesIdContainer & seriesUID = nameGenerator->GetSeriesUIDs();
auto seriesItr = seriesUID.begin();
auto seriesEnd = seriesUID.end();
using FileNamesContainer = std::vector< std::string >;
FileNamesContainer fileNames;
std::string seriesIdentifier;
while (seriesItr != seriesEnd)
{
seriesIdentifier = seriesItr->c_str();
fileNames = nameGenerator->GetFileNames(seriesIdentifier);
++seriesItr;
}
reader3d->SetFileNames(fileNames);
reader3d->Update();
return reader3d->GetOutput();
}
vtkSmartPointer<vtkImageData> itkToVtk(Input3dImageType::Pointer image)
{
typedef itk::ImageToVTKImageFilter< Input3dImageType> itkTovtkFilterType;
itkTovtkFilterType::Pointer itkTovtkImageFilter = itkTovtkFilterType::New();
itkTovtkImageFilter->SetInput(image);//设置图像数据从ITK转向VTK
itkTovtkImageFilter->Update();
vtkSmartPointer< vtkImageFlip > flip1 = vtkSmartPointer< vtkImageFlip >::New();
flip1->SetInputData(itkTovtkImageFilter->GetOutput());
flip1->SetFilteredAxes(1);
flip1->Update();
return flip1->GetOutput();
}
void show3dImage(Input3dImageType::Pointer image)
{
vtkSmartPointer<vtkImageViewer2> viewer =
vtkSmartPointer<vtkImageViewer2>::New();
viewer->SetInputData(itkToVtk(image));
//设置基本属性
viewer->SetSize(640, 480);
viewer->SetColorLevel(500);
viewer->SetColorWindow(2000);
viewer->SetSlice(40);
viewer->SetSliceOrientationToXY();
viewer->Render();
viewer->GetRenderer()->SetBackground(0, 0, 0);
viewer->GetRenderWindow()->SetWindowName("ImageViewer2D");
vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =
vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
vtkSmartPointer<vtkRenderWindowInteractor> rwi =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
//设置交互属性
viewer->SetupInteractor(rwi);
//viewer->GetRenderer()->GetRenderWindow()->GetInteractor()->SetInteractorStyle(style);
vtkSmartPointer<vtkSliderRepresentation2D> sliderRep =
vtkSmartPointer<vtkSliderRepresentation2D>::New();
sliderRep->SetMinimumValue(viewer->GetSliceMin());
sliderRep->SetMaximumValue(viewer->GetSliceMax());
sliderRep->SetValue(5.0);
sliderRep->GetSliderProperty()->SetColor(1, 0, 0);//red
sliderRep->GetTitleProperty()->SetColor(1, 0, 0);//red
sliderRep->GetLabelProperty()->SetColor(1, 0, 0);//red
sliderRep->GetSelectedProperty()->SetColor(0, 1, 0);//green
sliderRep->GetTubeProperty()->SetColor(1, 1, 0);//yellow
sliderRep->GetCapProperty()->SetColor(1, 1, 0);//yellow
sliderRep->GetPoint1Coordinate()->SetCoordinateSystemToDisplay();
sliderRep->GetPoint1Coordinate()->SetValue(40, 40);
sliderRep->GetPoint2Coordinate()->SetCoordinateSystemToDisplay();
sliderRep->GetPoint2Coordinate()->SetValue(500, 40);
vtkSmartPointer<vtkSliderWidget> sliderWidget =
vtkSmartPointer<vtkSliderWidget>::New();
sliderWidget->SetInteractor(rwi);
sliderWidget->SetRepresentation(sliderRep);
sliderWidget->SetAnimationModeToAnimate();
sliderWidget->EnabledOn();
vtkSmartPointer<vtkSliderCallback> callback =
vtkSmartPointer<vtkSliderCallback>::New();
callback->viewer = viewer;
sliderWidget->AddObserver(vtkCommand::InteractionEvent, callback);
vtkSmartPointer<vtkAxesActor> axes = vtkSmartPointer<vtkAxesActor>::New();
double axesSize[3] = { 100,100,100 };
axes->SetTotalLength(axesSize);
axes->SetConeRadius(0.1);
axes->SetShaftTypeToLine();
axes->SetAxisLabels(false);
viewer->GetRenderer()->AddActor(axes);
rwi->Start();
viewer->Render();
}
void write3dImage(const char *path, Input3dImageType::Pointer image)
{
itksys::SystemTools::MakeDirectory(path);
typedef itk::ImageSeriesWriter < Input3dImageType, Output2dImageType > WriterType;
WriterType::Pointer writer = WriterType::New();
nameGenerator->SetOutputDirectory(path);
writer->SetMetaDataDictionaryArray(reader3d->GetMetaDataDictionaryArray());
writer->SetImageIO(gdcmImageIO);
writer->SetFileNames(nameGenerator->GetOutputFileNames());
writer->SetInput(image);
writer->Update();
}
int main()
{
const char *Input_Name = ("C:\\Users\\lDesktop\\test1\\in");
const char *output_Name = ("C:\\Users\\lDesktop\\test1\\out");
Input3dImageType::Pointer image = read3dImage(Input_Name);
show3dImage(image);
//导出dicom序列图
//write3dImage(output_Name ,image);
return EXIT_SUCCESS;
}