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; private double opacityWindow = 4096; private double opacityLevel = 2048; #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 renderWindowControl1_Load(object sender, EventArgs e) { _render = renderWindowControl1.RenderWindow.GetRenderers().GetFirstRenderer(); _renWin = renderWindowControl1.RenderWindow; _iren = renderWindowControl1.RenderWindow.GetInteractor(); } private void btn_render_Click(object sender, EventArgs e) { if (!String.IsNullOrEmpty(FileDir)) { _reader = new Kitware.VTK.vtkDICOMImageReader(); _reader.SetDirectoryName(FileDir); _reader.SetDataByteOrderToLittleEndian(); _reader.Update(); vtkPiecewiseFunction opacityFun = new vtkPiecewiseFunction(); opacityFun.AddSegment(opacityLevel-0.5*opacityWindow,0.0, opacityLevel+0.5*opacityWindow,1.0); Console.WriteLine("opacityLevel-0.5*opacityWindow:" + (opacityLevel - 0.5 * opacityWindow).ToString()); Console.WriteLine("opacityLevel+0.5*opacityWindow:" + (opacityLevel + 0.5 * opacityWindow).ToString()); 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.ShadeOn(); property.SetColor(colorFun); property.SetScalarOpacity(opacityFun); vtkSmartVolumeMapper mapper = new vtkSmartVolumeMapper(); // vtkVolumeProMapper mapper = new vtkVolumeProMapper(); // vtkVolumeRayCastMIPFunction mapper.SetBlendModeToMaximumIntensity(); mapper.SetInput(_reader.GetOutput()); vtkVolume volume = new vtkVolume(); volume.SetProperty(property); volume.SetMapper(mapper); _render.AddViewProp(volume); _render.ResetCamera(); _renWin.Render(); _iren.Initialize(); _iren.Start(); } else { MessageBox.Show("請先選定文件夾!"); } } }
第二種:c#
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; private double opacityWindow = 4096; private double opacityLevel = 2048; #endregion #region 屬性 public string FileDir { get; set; } #endregion public Form1() { InitializeComponent(); } 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();//得到顏色映射標量值的範圍 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. //若是不進行轉換,vtkVolumeRayCastMapper會沒法渲染short類型的數據, //必需要通過轉換將數據轉換爲vtkVolumeRayCastMapper能夠渲染的數據 vtkImageCast.SetInput(_reader.GetOutput()); vtkImageCast.SetScale(slope); vtkImageCast.SetShift(shift); vtkImageCast.SetOutputScalarTypeToUnsignedShort(); vtkImageCast.Update(); //得到透明度轉換函數的參數 range=vtkImageCast.GetOutput().GetScalarRange(); double level = 0.5 * (range[1] + range[0]); double window = range[1] - range[0]; vtkPiecewiseFunction opacityFun = new vtkPiecewiseFunction(); //opacityFun.AddPoint(level-window/2,0.0); // opacityFun.AddPoint(level+window/2,1.0); // opacityFun.AddSegment(0,0.0,255,1.0);//all //opacityFun.AddPoint(73.0, 0.0); //opacityFun.AddPoint(89, 1.0); //opacityFun.AddPoint(104, 0.0);//bone //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.8);//0.3 opacityFun.AddPoint(64, 0.0);//soft //opacityFun.AddPoint(opacityLevel-0.5*opacityWindow,0.0); //opacityFun.AddPoint(opacityLevel + 0.5 * opacityWindow, 1.0); vtkPiecewiseFunction grayTransferFun = new vtkPiecewiseFunction(); // grayTransferFun.AddSegment(level-window/2,0.0,level+window/2,1.0); grayTransferFun.AddSegment(0, 0.0, 255, 1.0); Console.WriteLine("level:{0}\n window :{1}",level,window); vtkVolumeProperty property = new vtkVolumeProperty(); property.SetInterpolationTypeToLinear(); property.SetScalarOpacity(opacityFun); property.SetColor(grayTransferFun); vtkVolumeRayCastMIPFunction mipFun = new vtkVolumeRayCastMIPFunction(); mipFun.SetMaximizeMethodToOpacity(); vtkVolumeRayCastMapper mapper = new vtkVolumeRayCastMapper(); mapper.SetVolumeRayCastFunction(mipFun); 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("請先選定文件夾!"); } } private void renderWindowControl1_Load(object sender, EventArgs e) { _render = renderWindowControl1.RenderWindow.GetRenderers().GetFirstRenderer(); _renWin = renderWindowControl1.RenderWindow; _iren = renderWindowControl1.RenderWindow.GetInteractor(); } }