VTK 实现MIP(两种) Activiz

 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("请先选定文件夹!");
            }
        }
    }

第二种:

 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();

        }
    }


你可能感兴趣的:(VTK 实现MIP(两种) Activiz)