2

I'm using Activiz(c# library of vtk) and SimpleITK in my program,

I tried to load dicom series with SimpleITK, then transfer the image to Activiz,then create a volume with 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();

I can get a volume, but it seems that this volume has been distorted, cut, deformed.

The_Outsider
  • 1,875
  • 2
  • 24
  • 42
RicK
  • 21
  • 2

2 Answers2

1

you need to change dataImporter.SetDataExtent and DataImporter.SetWholeExtent to:

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

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

and cause of Dicom types, you have to remove the line:

dataImporter.SetDataScalarTypeToUnsignedChar();

Or change it to other types such as short,... that are compatible with Dicom.

 dataImporter.SetDataScalarTypeToShort();

And also cause of pixelType of Dicom images, you need to change the casting type and GetBuffer type to int16:

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

IntPtr ptr = image.GetBufferAsInt16();

Also, you need to use the below code instead of CopyImportVoidPointer

dataImporter.SetImportVoidPointer(ptr);

This method sets the pointer from which the image data is imported. VTK will not make its own copy of the data, it will access the data directly from the supplied array.

0

I think you have an off-by-1 error when you call dataImporter.SetDataExtent and dataImporter.SetWholeExtent. The maxes should be size[0]-1, size[1]-1, and 99.

Dave Chen
  • 1,905
  • 1
  • 12
  • 18