【VTK】dicom序列换切片

#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

#include <vtkSmartPointer.h>
#include <vtkMetaImageReader.h>
#include <vtkDicomImageReader.h>
#include <vtkMatrix4x4.h>
#include <vtkLookupTable.h>
#include <vtkImageMapToColors.h>
#include <vtkImageActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>

#include <vtkCommand.h> //建立“观察者/命令”模式监听鼠标消息 完成交互
#include <vtkImageReslice.h>
#include <vtkImageData.h>

class vtkImageInteractionCallback : public vtkCommand
{
public:
	static vtkImageInteractionCallback* New() //回调函数初始化函数
	{
		return new vtkImageInteractionCallback;
	}
	vtkImageInteractionCallback()
	{
		this->Slicing = 0;
		this->ImageReslice = 0;
		this->Interactor = 0;
	}
	void SetImageReslice(vtkImageReslice* reslice)
	{
		this->ImageReslice = reslice;
	}
	vtkImageReslice* GetImageReslice()
	{
		return this->ImageReslice;
	}
	void SetImageResliceColorMap(vtkImageMapToColors* colorMap)
	{
		this->colorMap = colorMap;
	}
	vtkImageMapToColors* GetImageResliceColorMap()
	{
		return this->colorMap;
	}
	void SetInteractor(vtkRenderWindowInteractor* interactor)
	{
		this->Interactor = interactor;
	}
	vtkRenderWindowInteractor* GetInteractor()
	{
		return  this->Interactor;
	}
	virtual void Execute(vtkObject*, unsigned long event, void*)
	{
		vtkRenderWindowInteractor* interactor = GetInteractor();
		int lastPos[2];
		interactor->GetLastEventPosition(lastPos);
		int currPos[2];
		interactor->GetEventPosition(currPos);

		if (event == vtkCommand::LeftButtonPressEvent)
		{
			this->Slicing = 1; //标志位 
		}
		else if (event == vtkCommand::LeftButtonReleaseEvent)
		{
			this->Slicing = 0; //标志位 
		}
		else if (event == vtkCommand::MouseMoveEvent)
		{
			if (this->Slicing)//检验鼠标左键已经按下 正在执行操作
			{
				std::cout << "LeftButton has been pressed: " << std::endl;
				vtkImageReslice* reslice = this->ImageReslice;
				//记下鼠标Y向变化的幅值大小
				int deltaY = lastPos[1] - currPos[1];
				std::cout << "鼠标Y向变化的幅值大小: " << deltaY << std::endl;
				//记下鼠标X向变化的幅值大小
				int deltaX = lastPos[0] - currPos[0];
				std::cout << "鼠标X向变化的幅值大小: " << deltaX << std::endl;

				reslice->Update();
				double sliceSpacing = reslice->GetOutput()->GetSpacing()[2];
				vtkMatrix4x4* matrix = reslice->GetResliceAxes();
				//重新定位切片需要经过的中心点
				double point[4];
				double center[4];
				point[0] = 0;
				point[1] = 0;
				point[2] = sliceSpacing * deltaY;
				point[3] = 1.0;
				matrix->MultiplyPoint(point, center);
				//***********理解MultiplyPoint   center4*1  =  matrix4*4  乘以  point4*1
				for (int i = 0; i < 4; i++)
				{
					for (int j = 0; j < 4; j++)
					{
						std::cout << matrix->GetElement(i, j) << "\t";
					}
					std::cout << "\n";
				}
				std::cout << "\n";
				std::cout << point[0] << "\t" << point[1] << "\t" << point[2] << "\t" << point[3] << "\n";
				std::cout << center[0] << "\t" << center[1] << "\t" << center[2] << "\t" << center[3] << "\n";

				//******************
				matrix->SetElement(0, 3, center[0]);
				matrix->SetElement(1, 3, center[1]);
				matrix->SetElement(2, 3, center[2]);
				static double colorWindowLevel = 500;
				static double colorWindowWith = 2000;
				colorWindowLevel += deltaX;

				colorMap->GetLookupTable()->SetRange(colorWindowLevel - colorWindowWith / 2, colorWindowLevel + colorWindowWith / 2);

				reslice->Update();
				colorMap->Update();
				interactor->Render();
			}
			else
			{
				vtkInteractorStyle* style = vtkInteractorStyle::SafeDownCast(
					interactor->GetInteractorStyle());
				if (style)
				{
					style->OnMouseMove();
				}
			}
		}
	}
private:
	int Slicing;
	vtkImageReslice* ImageReslice;
	vtkImageMapToColors* colorMap;
	vtkRenderWindowInteractor* Interactor;
};
//**********************************************************************************//
int main()
{
	vtkSmartPointer<vtkMetaImageReader> reader =
		vtkSmartPointer<vtkMetaImageReader>::New();
	reader->SetFileName("brain.mhd");
	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]);

	static double axialElements[16] = {
		1, 0, 0, 0,
		0, 1, 0, 0,
		0, 0, 1, 0,
		0, 0, 0, 1
	};

	vtkSmartPointer<vtkMatrix4x4> resliceAxes =
		vtkSmartPointer<vtkMatrix4x4>::New();
	resliceAxes->DeepCopy(axialElements);

	resliceAxes->SetElement(0, 3, center[0]);
	resliceAxes->SetElement(1, 3, center[1]);
	resliceAxes->SetElement(2, 3, center[2]);

	vtkSmartPointer<vtkImageReslice> reslice =
		vtkSmartPointer<vtkImageReslice>::New();
	reslice->SetInputConnection(reader->GetOutputPort());
	reslice->SetOutputDimensionality(2);
	reslice->SetResliceAxes(resliceAxes);
	reslice->SetInterpolationModeToLinear();
	reslice->Update();

	vtkSmartPointer<vtkLookupTable> colorTable =
		vtkSmartPointer<vtkLookupTable>::New();
	colorTable->SetRange(0, 1000);
	colorTable->SetValueRange(0.0, 1.0);
	colorTable->SetSaturationRange(0.0, 0.0);
	colorTable->SetRampToLinear();
	colorTable->Build();

	vtkSmartPointer<vtkImageMapToColors> colorMap =
		vtkSmartPointer<vtkImageMapToColors>::New();
	colorMap->SetLookupTable(colorTable);
	colorMap->SetInputConnection(reslice->GetOutputPort());
	colorMap->Update();

	vtkSmartPointer<vtkImageActor> imgActor =
		vtkSmartPointer<vtkImageActor>::New();
	imgActor->SetInputData(colorMap->GetOutput());
	imgActor->Update();

	vtkSmartPointer<vtkRenderer> renderer =
		vtkSmartPointer<vtkRenderer>::New();
	renderer->AddActor(imgActor);
	renderer->SetBackground(.4, .5, .6);

	vtkSmartPointer<vtkRenderWindow> renderWindow =
		vtkSmartPointer<vtkRenderWindow>::New();
	renderWindow->SetSize(500, 500);
	renderWindow->AddRenderer(renderer);

	vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
		vtkSmartPointer<vtkRenderWindowInteractor>::New();
	vtkSmartPointer<vtkInteractorStyleImage> imagestyle =
		vtkSmartPointer<vtkInteractorStyleImage>::New();

	renderWindowInteractor->SetInteractorStyle(imagestyle);
	renderWindowInteractor->SetRenderWindow(renderWindow);
	renderWindowInteractor->Initialize();
	//****************建立 观察者-命令 模式****************//
	vtkSmartPointer<vtkImageInteractionCallback> callback =
		vtkSmartPointer<vtkImageInteractionCallback>::New();
	callback->SetImageReslice(reslice);
	callback->SetInteractor(renderWindowInteractor);
	callback->SetImageResliceColorMap(colorMap);

	imagestyle->AddObserver(vtkCommand::MouseMoveEvent, callback);
	imagestyle->AddObserver(vtkCommand::LeftButtonPressEvent, callback);
	imagestyle->AddObserver(vtkCommand::LeftButtonReleaseEvent, callback);

	renderWindowInteractor->Start();

	return 0;
}

对于使用VTK库在Python中进行DICOM重建,你可以按照以下步骤进行操作: 1. 首先,确保你已经安装了VTK库。你可以使用pip命令来安装它: ```shell pip install vtk ``` 2. 导入所需的模块和类: ```python import vtk from vtk.util import numpy_support ``` 3. 读取DICOM文件并将其加载到VTK中: ```python reader = vtk.vtkDICOMImageReader() reader.SetFileName("path/to/dicom/file.dcm") reader.Update() ``` 4. 获取DICOM数据并将其转为NumPy数组: ```python image_data = reader.GetOutput() dimensions = image_data.GetDimensions() spacing = image_data.GetSpacing() origin = image_data.GetOrigin() point_data = image_data.GetPointData() array_data = point_data.GetArray(0) numpy_array = numpy_support.vtk_to_numpy(array_data) ``` 5. 创建一个VTK渲染器和渲染窗口: ```python renderer = vtk.vtkRenderer() render_window = vtk.vtkRenderWindow() render_window.AddRenderer(renderer) ``` 6. 创建一个VTK图像器以显示重建的图像: ```python image_mapper = vtk.vtkImageMapper() image_mapper.SetInputData(image_data) image_actor = vtk.vtkActor2D() image_actor.SetMapper(image_mapper) renderer.AddActor2D(image_actor) ``` 7. 创建一个VTK交互器来显示渲染窗口: ```python interactor = vtk.vtkRenderWindowInteractor() interactor.SetRenderWindow(render_window) interactor.Initialize() interactor.Start() ``` 这些步骤可以帮助你在Python中使用VTK库进行DICOM重建。你可以根据需要进行进一步的定制和操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值