public partial class Form1 : Form { #region 私有变量 private Kitware.VTK.vtkRenderer _render = null; private Kitware.VTK.vtkRenderWindow _renWin = null; private Kitware.VTK.vtkRenderWindowInteractor _iren = null; private Kitware.VTK.vtkDICOMImageReader _reader = null; #endregion #region 属性 public string FileDir { get; set; } #endregion public Form1() { InitializeComponent(); } private void renderWindowControl1_Load(object sender, EventArgs e) { _render = renderWindowControl1.RenderWindow.GetRenderers().GetFirstRenderer(); _renWin = renderWindowControl1.RenderWindow; _iren = renderWindowControl1.RenderWindow.GetInteractor(); } private void button1_Click(object sender, EventArgs e) { FolderBrowserDialog dlg = new FolderBrowserDialog(); if (DialogResult.OK == dlg.ShowDialog()) { FileDir = dlg.SelectedPath; } } private void button2_Click(object sender, EventArgs e) { if (!String.IsNullOrEmpty(FileDir)) { _reader = new Kitware.VTK.vtkDICOMImageReader(); _reader.SetDirectoryName(FileDir); _reader.SetDataByteOrderToLittleEndian(); _reader.Update(); _reader.SetDataScalarTypeToShort(); _reader.UpdateWholeExtent(); _reader.GetOutput().UpdateInformation(); double[] range = _reader.GetOutput().GetScalarRange();//获得颜色映射标量值的范围 Console.WriteLine("reader range[0]={0},range[1]={1}", range[0],range[1]); double min = range[0]; double max = range[1]; double diff = max - min; double slope = 255.0 / diff;//斜率 double inter = -slope * min;//截距 double shift = inter / slope; vtkImageShiftScale vtkImageCast = vtkImageShiftScale.New(); //With vtkImageShiftScale Pixels are shifted //(a constant value added) and then scaled (multiplied by a scalar. As a convenience, //this class allows you to set the output scalar type similar to vtkImageCast. //This is because shift scale operations frequently convert data types. //将像素的数据范围限定在0-255之间 vtkImageCast.SetInput(_reader.GetOutput()); vtkImageCast.SetScale(slope); vtkImageCast.SetShift(shift); vtkImageCast.SetOutputScalarTypeToUnsignedShort(); vtkImageCast.Update(); //获得透明度转换函数的参数 range = vtkImageCast.GetOutput().GetScalarRange(); Console.WriteLine("ImageCast range[0]={0},range[1]={1}", range[0], range[1]); double level = 0.5 * (range[1] + range[0]); double window = range[1] - range[0]; vtkPiecewiseFunction opacityFun = new vtkPiecewiseFunction(); opacityFun.ClampingOff(); // opacityFun.AddPoint(13, 0.0); // opacityFun.AddPoint(32, 0.25); // opacityFun.AddPoint(51, 0.0);//lung // opacityFun.AddPoint(33.0,0.0); //opacityFun.AddPoint(49,0.3); // opacityFun.AddPoint(64,0.0);//soft opacityFun.AddPoint(73.0,0.0); opacityFun.AddPoint(89,1.0); opacityFun.AddPoint(104,0.0);//bone //opacityFun.AddPoint(opacityLevel-0.5*opacityWindow,0.0); //opacityFun.AddPoint(opacityLevel + 0.5 * opacityWindow, 1.0); vtkColorTransferFunction colorFun = new vtkColorTransferFunction(); colorFun.AddRGBSegment(0.0, 0.0, 0.0, 0.0, 255.0, 1.0, 1.0, 1.0); vtkVolumeProperty property = new vtkVolumeProperty(); property.SetInterpolationTypeToLinear(); property.SetScalarOpacity(opacityFun); property.SetColor(colorFun); vtkFixedPointVolumeRayCastMapper mapper = new vtkFixedPointVolumeRayCastMapper(); mapper.SetInput(vtkImageCast.GetOutput()); // mapper.SetInput(_reader.GetOutput()); //会报错:无法渲染非unsigned char或者unsigned short 类型的数据 //vtkVolumeRayCastMapper (09C92D38): //Cannot volume render data of type short, only unsigned char or unsigned short. vtkVolume volume = new vtkVolume(); volume.SetProperty(property); volume.SetMapper(mapper); _render.AddViewProp(volume); _render.ResetCamera(); _renWin.Render(); _iren.Initialize(); _iren.Start(); } else { MessageBox.Show("请先选定文件夹!"); } } }
第二种:
public partial class Form1 : Form { #region 私有成员 private Kitware.VTK.vtkRenderer _render = null; private Kitware.VTK.vtkRenderWindow _renWin=null; private Kitware.VTK.vtkRenderWindowInteractor _iren = null; private Kitware.VTK.vtkDICOMImageReader _reader = null; #endregion #region 属性 public string FileDir { get; set; } #endregion public Form1() { InitializeComponent(); } private void btn_Open_Click(object sender, EventArgs e) { FolderBrowserDialog dlg = new FolderBrowserDialog(); if (DialogResult.OK == dlg.ShowDialog()) { FileDir = dlg.SelectedPath; } } private void btn_render_Click(object sender, EventArgs e) { if (!String.IsNullOrEmpty(FileDir)) { _reader = new vtkDICOMImageReader(); _reader.SetDirectoryName(FileDir); _reader.SetDataByteOrderToLittleEndian(); _reader.Update(); vtkPiecewiseFunction compositeOpacity = new vtkPiecewiseFunction(); compositeOpacity.AddPoint(-3024, 0, 0.5, 0.0); compositeOpacity.AddPoint(-1000, 0, 0.5, 0.0); compositeOpacity.AddPoint(-500, 1.0, 0.33, 0.45); compositeOpacity.AddPoint(3071, 1.0, 0.5, 0.0); vtkColorTransferFunction colorFun = new vtkColorTransferFunction(); colorFun.AddRGBPoint(-3024, 0, 0, 0, 0.5, 0.0); colorFun.AddRGBPoint(-1000, 0.62, 0.36, 0.18, 0.5, 0.0); colorFun.AddRGBPoint(-500, 0.88, 0.60, 0.29, 0.33, 0.45); colorFun.AddRGBPoint(3071, 0.83, 0.66, 1, 0.5, 0.0); vtkVolumeProperty property = new vtkVolumeProperty(); property.ShadeOff(); property.SetInterpolationTypeToLinear(); property.SetColor(colorFun); property.SetScalarOpacity(compositeOpacity); vtkSmartVolumeMapper mapper = new vtkSmartVolumeMapper(); mapper.SetBlendModeToComposite(); mapper.SetInput(_reader.GetOutput()); vtkVolume volume = new vtkVolume(); volume.SetMapper(mapper); volume.SetProperty(property); _render.AddViewProp(volume); _render.ResetCamera(); _renWin.Render(); _iren.Initialize(); _iren.Start(); } else MessageBox.Show("请先选定文件夹!"); } private void renderWindowControl1_Load(object sender, EventArgs e) { _renWin = renderWindowControl1.RenderWindow; _render = renderWindowControl1.RenderWindow.GetRenderers().GetFirstRenderer(); _iren = renderWindowControl1.RenderWindow.GetInteractor(); } }