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

itk 读取dicom,用vtk重建显示

程序员文章站 2022-03-31 23:03:25
...
using PixelType = float;
	constexpr unsigned int Dimension = 3;
	using ImageType = itk::Image< PixelType, Dimension >;
	using ReaderType = itk::ImageSeriesReader< ImageType >;
	using ImageIOType = itk::GDCMImageIO;
	using NamesGeneratorType = itk::GDCMSeriesFileNames;
	ReaderType::Pointer reader = ReaderType::New();
	ImageIOType::Pointer dicomIO = ImageIOType::New();
	reader->SetImageIO(dicomIO);
	NamesGeneratorType::Pointer nameGenerator = NamesGeneratorType::New();
	nameGenerator->SetUseSeriesDetails(true);
	nameGenerator->SetDirectory("D:\\software\\SLC");

	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;
	}

	reader->SetFileNames(fileNames);

	try
	{
		reader->Update();
	}
	catch (itk::ExceptionObject& ex)
	{
		std::cout << ex << std::endl;
		
	}
	ImageType::SizeType imgSize = reader->GetOutput()->GetLargestPossibleRegion().GetSize();
	cout << "read done!Original size: " << imgSize << endl;


	typedef itk::ImageToVTKImageFilter< ImageType> itkTovtkFilterType;
	itkTovtkFilterType::Pointer itkTovtkImageFilter = itkTovtkFilterType::New();
	itkTovtkImageFilter->SetInput(reader->GetOutput());
	itkTovtkImageFilter->Update();

	vtkSmartPointer<vtkGPUVolumeRayCastMapper> volumeMapper =
		vtkSmartPointer<vtkGPUVolumeRayCastMapper>::New();
	volumeMapper->SetInputData(itkTovtkImageFilter->GetOutput());
	volumeMapper->SetSampleDistance(volumeMapper->GetSampleDistance() / 2);	//设置光线采样距离

	vtkSmartPointer<vtkVolumeProperty> volumeProperty =
		vtkSmartPointer<vtkVolumeProperty>::New();
	volumeProperty->SetInterpolationTypeToLinear();
	volumeProperty->ShadeOn();  //打开或者关闭阴影测试
	volumeProperty->SetAmbient(0.1);
	volumeProperty->SetDiffuse(0.9);  //漫反射
	volumeProperty->SetSpecular(0.2); //镜面反射
	volumeProperty->SetSpecularPower(10.0);
	//设置不透明度
	vtkSmartPointer<vtkPiecewiseFunction> opacityFun =
	vtkSmartPointer<vtkPiecewiseFunction>::New();

	opacityFun->AddPoint(-3024, 0);
	opacityFun->AddPoint(129.54, 0);
	opacityFun->AddPoint(145.24, 0.17);
	opacityFun->AddPoint(169.92, 0.63);
	opacityFun->AddPoint(395.58, 0.81);
	opacityFun->AddPoint(1578.73, 0.81);
	opacityFun->AddPoint(3071, 0.81);


	volumeProperty->SetScalarOpacity(opacityFun); //设置不透明度传输函数

	//设置颜色属性
	vtkSmartPointer<vtkColorTransferFunction> colorFun =
		vtkSmartPointer<vtkColorTransferFunction>::New();
	colorFun->AddRGBPoint(-3024, 0, 0, 0);
	colorFun->AddRGBPoint(129.54, 0.55, 0.25, 0.15);
	colorFun->AddRGBPoint(157.02, 1, 1, 1);
	colorFun->AddRGBPoint(169.92, 0.99, 0.87, 0.39);
	colorFun->AddRGBPoint(395.58, 1, 0.88, 0.66);
	colorFun->AddRGBPoint(1578.73, 1, 0.95, 0.96);
	colorFun->AddRGBPoint(3071, 0.83, 0.66, 1);


	volumeProperty->SetColor(colorFun);
	vtkSmartPointer<vtkVolume> volume =
		vtkSmartPointer<vtkVolume>::New();
	volume->SetMapper(volumeMapper);
	volume->SetProperty(volumeProperty);

	vtkSmartPointer<vtkCamera>camera = vtkSmartPointer<vtkCamera>::New();
	camera->SetViewUp(0, 0, -1);
	camera->SetPosition(0, 1, 0);
	camera->SetFocalPoint(0, 0, 0);
	camera->ComputeViewPlaneNormal();
	camera->Azimuth(10.0);
	camera->Elevation(10.0);
	mImageViewerRenderer4->SetActiveCamera(camera);


	mImageViewerRenderer4->SetBackground(0, 0, 0);
	mImageViewerRenderer4->AddVolume(volume);

	mImageViewerRenderWindow4->AddRenderer(mImageViewerRenderer4);

	mImageViewerRenderWindow4->Render();
	mImageViewerRenderWindow4->SetWindowName("VolumeRendering");

	mImageViewerWindowInteractor4->SetRenderWindow(mImageViewerRenderWindow4);
	mImageViewerRenderer4->ResetCamera();
	mImageViewerRenderWindow4->Render();

	vtkSmartPointer<vtkInteractorStyleTrackballCamera>style = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
	mImageViewerWindowInteractor4->SetInteractorStyle(style);
	mImageViewerWindowInteractor4->Initialize();
	mImageViewerWindowInteractor4->Start(); 

 

相关标签: vtk