如何在CSHARP项目中将Simpleitk图像转换为VTK?

时间:2019-06-07 17:25:53

标签: vtk simpleitk

我在程序中使用Activiz(vtk的c#库)和SimpleITK,

我尝试用SimpleITK加载dicom系列,然后将图像传输到Activiz,然后使用vtkVolumeRayCastMapper创建一个卷

ImageSeriesReader imageSeriesReader = new ImageSeriesReader();

VectorString dicomNames = ImageSeriesReader.GetGDCMSeriesFileNames(@"E:\VTK3d\王社教阅片端 - 复件\王社教阅片端\Data\PV");
imageSeriesReader.SetFileNames(dicomNames);
Image image = imageSeriesReader.Execute();

VectorDouble spacing= image.GetSpacing();
image = SimpleITK.Cast(image, PixelIDValueEnum.sitkVectorUInt8);
VectorUInt32 size = image.GetSize();
int len = 1;
for (int dim = 0; dim < image.GetDimension(); dim++)
{
    len *= (int)size[dim];
}


IntPtr ptr= image.GetBufferAsUInt8();

int[] bufferAsArray = new int[len];

//Marshal.Copy(ptr, bufferAsArray, 0, len);
int max = bufferAsArray.Max();
int min = bufferAsArray.Min();
vtkImageImport dataImporter = new vtkImageImport();
dataImporter.CopyImportVoidPointer(ptr, len);
dataImporter.SetDataScalarTypeToUnsignedChar();

dataImporter.SetDataExtent(0, (int)size[0], 0, (int)size[1], 0, 100);
dataImporter.SetWholeExtent(0, (int)size[0], 0, (int)size[1], 0, 100);
dataImporter.SetDataSpacing(spacing[0], spacing[1], spacing[2]);

vtkVolumeRayCastMapper volumeMapper = new vtkVolumeRayCastMapper();

vtkPiecewiseFunction volumeScalarOpacity = new vtkPiecewiseFunction();
volumeScalarOpacity.AddPoint(-260, 0.00);
//volumeScalarOpacity.AddPoint(500, 0.15);
//volumeScalarOpacity.AddPoint(1000, 0.15);
volumeScalarOpacity.AddPoint(340, 0.85);

vtkColorTransferFunction volumeColor = new vtkColorTransferFunction();
volumeColor.AddRGBPoint(-260, 0.0, 0.0, 0.0);
//volumeColor.AddRGBPoint(500, 1.0, 0.5, 0.3);
//volumeColor.AddRGBPoint(1000, 1.0, 0.5, 0.3);
volumeColor.AddRGBPoint(340, 1.0, 1.0, 0.9);

//vtkPiecewiseFunction volumeGradientOpacity = new vtkPiecewiseFunction();
//volumeGradientOpacity.AddPoint(0, 0.0);
//volumeGradientOpacity.AddPoint(90, 0.5);
//volumeGradientOpacity.AddPoint(100, 1.0);
vtkVolumeProperty volumeProperty = new vtkVolumeProperty();
volumeProperty.SetColor(volumeColor);
volumeProperty.SetScalarOpacity(volumeScalarOpacity);
// volumeProperty.SetGradientOpacity(volumeGradientOpacity);
//volumeProperty.SetInterpolationTypeToLinear();
//volumeProperty.ShadeOn();
//volumeProperty.SetAmbient(0.4);
//volumeProperty.SetDiffuse(0.6);
//volumeProperty.SetSpecular(0.2);
vtkVolumeRayCastCompositeFunction compositeFunction = new vtkVolumeRayCastCompositeFunction();
volumeMapper.SetVolumeRayCastFunction(compositeFunction);
volumeMapper.SetInputConnection(dataImporter.GetOutputPort());
vtkVolume volume = new vtkVolume();
volume.SetProperty(volumeProperty);
volume.SetMapper(volumeMapper);
volume.SetScale(0.3);
double[] c = new double[3];
c = volume.GetCenter();
renderer.AddVolume(volume);
renderer.Render();

我可以得到一个体积,但似乎该体积已变形,切割,变形。

2 个答案:

答案 0 :(得分:0)

当您调用dataImporter.SetDataExtent和dataImporter.SetWholeExtent时,我认为您有一个偏离1的错误。最大值应为size [0] -1,size [1] -1和99。

答案 1 :(得分:0)

您需要更改dataImporter.SetDataExtentDataImporter.SetWholeExtent 到:

dataImporter.SetDataExtent(1, (int)size[1], 1, (int)size[0], 0, 19);

dataImporter.SetWholeExtent(1, (int)size[1], 1, (int)size[0], 0, 19);

和 Dicom 类型的原因,您必须删除该行:

dataImporter.SetDataScalarTypeToUnsignedChar();

或将其更改为与 Dicom 兼容的其他类型,例如 short、...。

 dataImporter.SetDataScalarTypeToShort();

也是Dicom图片pixelType的原因,需要将casting type和GetBuffer type改成int16:

image = SimpleITK.Cast(image, PixelIDValueEnum.sitkInt16);

IntPtr ptr = image.GetBufferAsInt16();

另外,您需要使用以下代码而不是 CopyImportVoidPointer

dataImporter.SetImportVoidPointer(ptr);

此方法设置导入图像数据的指针。 VTK 不会制作自己的数据副本,它会直接从提供的数组中访问数据。