博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
VTK 实现体绘制(两种)Activiz
阅读量:5862 次
发布时间:2019-06-19

本文共 8357 字,大约阅读时间需要 27 分钟。

hot3.png

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

转载于:https://my.oschina.net/u/2009228/blog/536366

你可能感兴趣的文章
移动开发Html 5前端性能优化指南
查看>>
silverlight style和template 使用之tip
查看>>
Eclipse配置python环境
查看>>
晶振不起振的原因及其解决方法
查看>>
《系统架构师》——操作系统和硬件基础
查看>>
如何看待一本图书
查看>>
oracle参数列表
查看>>
Linux 中如何通过命令行访问 Dropbox
查看>>
开发进度——4
查看>>
JS里验证信息
查看>>
Akka actor tell, ask 函数的实现
查看>>
windows10 chrome 调试 ios safari 方法
查看>>
Hello , Ruby!
查看>>
Netty 4.1.35.Final 发布,经典开源 Java 网络服务框架
查看>>
详解Microsoft.AspNetCore.CookiePolicy
查看>>
SCDPM2012 R2实战一:基于SQL 2008 R2集群的SCDPM2012 R2的安装
查看>>
SQL SERVER中字段类型与C#数据类型的对应关系
查看>>
Linux lsof命令详解
查看>>
SVG path
查看>>
js判断checkbox是否选中
查看>>