ITK VTK 重建最简三维(dicom 医学影像)--仅供测试学习

5 篇文章 0 订阅
3 篇文章 0 订阅
  1. 开发环境 ITK(5.3.0)  VTK(8.2.0)  QT(5.12.12)  VS2017 win10 X64home
  2. 代码(部分代码如下,完整代码和程序下载路径如下)

 https://zyq1569.github.io/down.html下载

https://sourceforge.net/projects/health1212/files/Tools/HealthApp/下载

 如果直接运行,没有安装环境运行包,下载VC_redist.x64.exe VC_redist.x86.exe

路径下也有 https://sourceforge.net/projects/health1212/files/Tools/HealthApp/下载

  • VTK_MODULE_INIT(vtkRenderingOpenGL2);
    VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2);
    VTK_MODULE_INIT(vtkInteractionStyle);
    VTK_MODULE_INIT(vtkRenderingFreeType);
    
    using namespace std;
    typedef short PixelType;
    const unsigned int   Dimension = 3;
    typedef itk::Image< PixelType, Dimension > Input2dImageType;
    typedef itk::Image< PixelType, 3 > Input3dImageType;
    
    typedef itk::Image< PixelType, Dimension > Output2dImageType;
    typedef itk::Image< PixelType, 3 > Output3dImageType;
    
    bool getFileNames(QString dir, std::vector< std::string > &fileNames)
    {
    	using NamesGeneratorType = itk::GDCMSeriesFileNames;
    	NamesGeneratorType::Pointer nameGenerator = NamesGeneratorType::New();
    	nameGenerator->SetUseSeriesDetails(true);
    	nameGenerator->AddSeriesRestriction("0008|0021");
    	nameGenerator->SetDirectory(dir.toUtf8().data());//bool SystemTools::FileIsDirectory(const std::string& inName)
    	using SeriesIdContainer = std::vector< std::string >;
    	const SeriesIdContainer & seriesUID = nameGenerator->GetSeriesUIDs();
    	auto seriesItr = seriesUID.begin();
    	auto seriesEnd = seriesUID.end();
    	std::string seriesIdentifier;
    	while (seriesItr != seriesEnd)
    	{
    		seriesIdentifier = seriesItr->c_str();
    		fileNames = nameGenerator->GetFileNames(seriesIdentifier);
    		++seriesItr;
    	}
    	nameGenerator = NULL;
    	return true;
    }
    Input3dImageType::Pointer GdcmRead3dImage(std::string path, QString dir)
    {
    	std::vector< std::string > fileNames;
    
    	getFileNames(dir, fileNames);
    
    	using ReaderType3d = itk::ImageSeriesReader< Input3dImageType >;
    	ReaderType3d::Pointer reader3d = ReaderType3d::New();
    
    	typedef itk::GDCMImageIO   ImageIOType;//GDCMImageIO读DICOM
    	ImageIOType::Pointer gdcmImageIO = ImageIOType::New();
    
    	reader3d->SetImageIO(gdcmImageIO);
        reader3d->SetFileNames(fileNames);
        reader3d->Update();
    	Input3dImageType::Pointer image = reader3d->GetOutput();
    	reader3d = NULL;
    	gdcmImageIO = NULL;
    	return image;
    }
    
    
    vtkSmartPointer<vtkImageData> ImageDataItkToVtk(Input3dImageType::Pointer image)
    {
        typedef itk::ImageToVTKImageFilter< Input3dImageType> itkTovtkFilterType;
        itkTovtkFilterType::Pointer itkTovtkImageFilter = itkTovtkFilterType::New();
        itkTovtkImageFilter->SetInput(image);//设置图像数据从ITK转向VTK
        itkTovtkImageFilter->Update();
    
        vtkSmartPointer< vtkImageFlip > ImageFlip = vtkSmartPointer< vtkImageFlip >::New();
    	ImageFlip->SetInputData(itkTovtkImageFilter->GetOutput());
    
    	ImageFlip->SetFilteredAxes(1);
    	ImageFlip->Update();
    	vtkSmartPointer<vtkImageData> vtkdata = ImageFlip->GetOutput();
    	ImageFlip = NULL;
    	itkTovtkImageFilter = NULL;
    	return vtkdata;
    }
    
    void MainWindow::on_pBVolume3D_clicked()
    {
    	QString DicomDir = ui->m_dcmDir->toPlainText();
    	QDir dir;
    	if (!dir.exists(DicomDir))
    	{
    		QMessageBox::information(NULL, "Dicom3D", "No dicom files!");
    		return;
    	}
    
    	std::string Input_Name = qPrintable(DicomDir);
    	Input3dImageType::Pointer dicomimage = GdcmRead3dImage(Input_Name, DicomDir);
    	static bool init = false;
    	if (!init)
    	{
    		vtkObjectFactory::RegisterFactory(vtkRenderingOpenGL2ObjectFactory::New());
    		vtkObjectFactory::RegisterFactory(vtkRenderingVolumeOpenGL2ObjectFactory::New());
    		init = true;
    	}
    
    	//定义绘制器;
    	vtkRenderer *rendererViewer = vtkRenderer::New();//指向指针;
    	vtkSmartPointer<vtkRenderWindow> RenderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    	RenderWindow->AddRenderer(rendererViewer);
    
    	//透明度映射函数定义;
    	vtkPiecewiseFunction *opacityTransform = vtkPiecewiseFunction::New();
    	opacityTransform->AddPoint(-1024, 0.0);
    	opacityTransform->AddPoint(-24, 0.0);
    	opacityTransform->AddPoint(167.00000000000000, 0.16862745098039220);
    	opacityTransform->AddPoint(218.00000000000000, 0.41960784313725491);
    	opacityTransform->AddPoint(218.00000000000000, 0.41960784313725491);
    	opacityTransform->AddPoint(445.00000000000000, 0.57254901960784310);
    	opacityTransform->AddPoint(1455.0000000000000, 0.87450980392156863);
    	opacityTransform->AddPoint(2784.0000000000000, 0.88235294117647056);
    
    	//颜色映射函数定义,梯度上升的
    	vtkColorTransferFunction *colorTransformFunction = vtkColorTransferFunction::New();
    	colorTransformFunction->AddRGBPoint(-1024.0, 1.0, 0.13725490196078433, 0.17254901960784313);
    	colorTransformFunction->AddRGBPoint(24.0, 1.0, 0.13725490196078433, 0.17254901960784313);
    	colorTransformFunction->AddRGBPoint(163.0, 1.0, 0.13725490196078433, 0.17254901960784313);
    	colorTransformFunction->AddRGBPoint(167.0, 1.0, 0.35294117647058826, 0.16862745098039217);
    	colorTransformFunction->AddRGBPoint(218.0, 1.0, 0.63921568627450975, 0.11372549019607843);
    
    	colorTransformFunction->AddRGBPoint(445.0, 1.0, 1.0, 1.0);
    	colorTransformFunction->AddRGBPoint(1455.0, 1.0, 1.0, 1.0);
    	colorTransformFunction->AddRGBPoint(2784.0, 1.0, 1.0, 1.0);
    
    	vtkPiecewiseFunction *gradientTransform = vtkPiecewiseFunction::New();
    	gradientTransform->AddPoint(1, 0.0);
    	gradientTransform->AddPoint(70, 0.5);
    	gradientTransform->AddPoint(130, 1.0);
    	//gradientTransform->AddPoint(300, 0.1);
    
    
    	//体数据属性;
    	vtkVolumeProperty *volumeProperty = vtkVolumeProperty::New();
    	volumeProperty->SetColor(colorTransformFunction);
    	volumeProperty->SetScalarOpacity(opacityTransform);
    	volumeProperty->SetGradientOpacity(gradientTransform);
    	volumeProperty->ShadeOn();//应用
    	volumeProperty->SetInterpolationTypeToLinear();//直线间样条插值;
    	volumeProperty->SetAmbient(0.4);//环境光系数;
    	volumeProperty->SetDiffuse(0.69996);//漫反射;
    	volumeProperty->SetSpecular(0.2);
    	volumeProperty->SetSpecularPower(10);//高光强度;
    
    	//光纤映射类型定义:
    	//Mapper定义,
    	vtkSmartVolumeMapper *volumeMapper = vtkSmartVolumeMapper::New();
    	volumeMapper->SetInputData(ImageDataItkToVtk(dicomimage));//;cast_file->GetOutput());
    	volumeMapper->SetBlendModeToComposite();
    	volumeMapper->SetRequestedRenderModeToDefault();
    	vtkLODProp3D *lodProp3D =  vtkLODProp3D::New();
    	lodProp3D->AddLOD(volumeMapper, volumeProperty, 0.0);
    
    
    	vtkVolume *volume = vtkVolume::New();
    	volume->SetMapper(volumeMapper);
    	volume->SetProperty(volumeProperty);//设置体属性;
    
    	double volumeView[4] = { 0,0,0.5,1 };
    
    	vtkOutlineFilter *outlineData = vtkOutlineFilter::New();//线框;
    	outlineData->SetInputData(ImageDataItkToVtk(dicomimage));
    	vtkPolyDataMapper *mapOutline = vtkPolyDataMapper::New();
    	mapOutline->SetInputConnection(outlineData->GetOutputPort());
    	vtkActor *outline = vtkActor::New();
    	outline->SetMapper(mapOutline);
    	outline->GetProperty()->SetColor(0, 0, 0);//背景纯黑色;
    
    	rendererViewer->AddVolume(volume);
    	rendererViewer->AddActor(outline);
    	rendererViewer->SetBackground(1, 1, 1);
    	rendererViewer->ResetCamera();
    
    
    	//重设相机的剪切范围;
    	rendererViewer->ResetCameraClippingRange();
    	RenderWindow->SetSize(500, 500);
    
    	vtkRenderWindowInteractor *RenderWindowInteractor = vtkRenderWindowInteractor::New();
    	RenderWindowInteractor->SetRenderWindow(RenderWindow);
    
    	//设置相机跟踪模式
    	vtkInteractorStyleTrackballCamera *Interactorstyle = vtkInteractorStyleTrackballCamera::New();
    	RenderWindowInteractor->SetInteractorStyle(Interactorstyle);
    
    	RenderWindow->Render();
    	RenderWindow->SetWindowName("Volume-3D");
    	RenderWindowInteractor->Initialize();
    	RenderWindowInteractor->Start();
    
    	RenderWindowInteractor->Delete();
    	lodProp3D->Delete();
    	volumeMapper->Delete();
    	colorTransformFunction->Delete();
    	gradientTransform->Delete();
    	volumeProperty->Delete();
    	volume->Delete();
    	outlineData->Delete();
    	outline->Delete();
    	mapOutline->Delete();
    	Interactorstyle->Delete();
    	rendererViewer->Delete();
    
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值