QT中vtk读取nii文件并修改其中标签

// 获取读取器的输出数据
vtkSmartPointer<vtkNIFTIImageReader> reader = vtkSmartPointer<vtkNIFTIImageReader>::New();

// 设置读取器的输入文件名
const char* initNiiName = "D:/initInput.nii";
reader->SetFileName(initNiiName);

// 读取NII图像数据
try {
	reader->Update();
}
catch (itk::ExceptionObject &ex) {
	std::cout << "Exception caught in fixed image reading!" << std::endl;
	LOG_ERROR("Detail failed");
	std::cout << ex << std::endl;
}

vtkSmartPointer<vtkImageData> imageData = reader->GetOutput();
vtkSmartPointer<vtkImageData> thresholdedData1 = vtkSmartPointer<vtkImageData>::New();
thresholdedData1->SetOrigin(imageData->GetOrigin());
thresholdedData1->SetSpacing(imageData->GetSpacing());
thresholdedData1->SetDimensions(imageData->GetDimensions());
thresholdedData1->AllocateScalars(VTK_UNSIGNED_SHORT, 1);
auto label_dims1 = imageData->GetDimensions();
auto segDataLabelPtr1 = (unsigned short*)thresholdedData1->GetScalarPointer();

for (size_t iz = 0; iz < label_dims1[2]; iz++)
	for (size_t iy = 0; iy < label_dims1[1]; iy++)
		for (size_t ix = 0; ix < label_dims1[0]; ix++)
		{
			int idx = ix + iy * label_dims1[0] + iz * label_dims1[0] * label_dims1[1];

			segDataLabelPtr1[idx] = 1;
		}

// 创建 NIfTI 图像写入器
vtkSmartPointer<vtkNIFTIImageWriter> writer = vtkSmartPointer<vtkNIFTIImageWriter>::New();
writer->SetFileName("D:/output.nii");

// 设置要写入的图像数据
writer->SetInputData(thresholdedData1);
// 复制元数据
writer->SetQFormMatrix(reader->GetQFormMatrix());
writer->SetSFormMatrix(reader->GetSFormMatrix());
writer->SetTimeDimension(reader->GetTimeDimension());
writer->SetTimeSpacing(reader->GetTimeSpacing());
// 执行写入操作
writer->Update();

posted on 2024-10-10 08:53  大海z16  阅读(37)  评论(0编辑  收藏  举报