Пример #1
0
        /// <summary>
        /// Converts 3D byte array to vtkImageData.
        /// </summary>
        /// <param name="data">Input array.</param>
        /// <param name="dims">Input array dimensions. Give the begin and end extent for each dimension.</param>
        /// <returns>Converted array.</returns>
        public static vtkImageData byteToVTK1D(byte[] data, int[] dims)
        {
            int h = dims[1] - dims[0] + 1;
            int w = dims[3] - dims[2] + 1;
            int d = dims[5] - dims[4] + 1;
            //Create VTK data for putput
            vtkImageData vtkdata = vtkImageData.New();
            //Character array for conversion
            vtkUnsignedCharArray charArray = vtkUnsignedCharArray.New();
            //Pin byte array
            GCHandle pinnedArray = GCHandle.Alloc(data, GCHandleType.Pinned);

            //Set character array input
            charArray.SetArray(pinnedArray.AddrOfPinnedObject(), h * w * d, 1);
            //Set vtkdata properties and connect array
            //Data from char array
            vtkdata.GetPointData().SetScalars(charArray);
            //Number of scalars/pixel
            vtkdata.SetNumberOfScalarComponents(1);
            //Set data extent
            vtkdata.SetExtent(dims[0], dims[1], dims[2], dims[3], dims[4], dims[5]);
            //Scalar type
            vtkdata.SetScalarTypeToUnsignedChar();
            vtkdata.Update();

            return(vtkdata);
        }
Пример #2
0
        public static void GetRectData(vtkPolyData data, Rectangle rect)
        {
            var points     = vtkPoints.New();
            var colorArray = vtkUnsignedCharArray.New();

            colorArray.SetNumberOfComponents(3);

            RectImageData = vtkImageData.New();
            RectImageData.SetExtent(0, rect.Size.Width - 1, 0, rect.Size.Height - 1, 0, 0);
            RectImageData.SetNumberOfScalarComponents(1);
            RectImageData.SetScalarTypeToUnsignedChar();

            for (var i = rect.Top; i < rect.Bottom; i++)
            {
                for (var j = rect.Left; j < rect.Right; j++)
                {
                    double[] p = data.GetPoint(i * ImageWidth + j);
                    points.InsertNextPoint(j - rect.Left, i - rect.Top, p[2]);

                    double[] c = data.GetPointData().GetScalars().GetTuple3(i * ImageWidth + j);
                    colorArray.InsertNextTuple3(c[0], c[1], c[2]);

                    RectImageData.SetScalarComponentFromDouble(j - rect.Left, i - rect.Top, 0, 0, c[0]);
                }
            }


            RectPolyData = vtkPolyData.New();
            RectPolyData.SetPoints(points);
            RectPolyData.GetPointData().SetScalars(colorArray);
            RectPolyData.Update();
        }
Пример #3
0
        public static void ReadDataFromFile(string filename)
        {
            try
            {
                SimplePointFile.GetImageInfo(filename);

                ImageWidth  = SimplePointFile.ImageWidth;
                ImageHeight = SimplePointFile.ImageHeight;

                //Console.WriteLine("ImageWidth:" + ImageWidth);
                //Console.WriteLine("ImageHeight:" + ImageHeight);

                var points = vtkPoints.New();

                var colorArray = vtkUnsignedCharArray.New();
                colorArray.SetNumberOfComponents(3);

                ImageData = vtkImageData.New();
                ImageData.SetExtent(0, ImageWidth - 1, 0, ImageHeight - 1, 0, 0);
                ImageData.SetNumberOfScalarComponents(1);
                ImageData.SetScalarTypeToUnsignedChar();


                SimplePointFile.OpenReadFile(filename);
                double[] data;
                while ((data = SimplePointFile.ReadLine()) != null)
                {
                    points.InsertNextPoint(data[0], data[1], data[2]);
                    colorArray.InsertNextTuple3(data[3], data[4], data[5]);

                    ImageData.SetScalarComponentFromDouble((int)data[0], (int)data[1], 0, 0, data[3]);
                }

                PolyData = vtkPolyData.New();
                PolyData.SetPoints(points);
                PolyData.GetPointData().SetScalars(colorArray);
                PolyData.Update();

                //Console.WriteLine("PolyData & ImageData Load.");
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            finally
            {
                SimplePointFile.CloseReadFile();
            }
        }
        private void Window_Activated(object sender, EventArgs e)
        {
            // 一定要先 vtkformhost.Child = vtkControl;下面三条顺序不能乱 否则报错 切记!
            vtkformhost.Child = vtkControl;
            vtkRenderWindow renWin    = vtkControl.RenderWindow;
            vtkRenderer     aRenderer = renWin.GetRenderers().GetFirstRenderer();

            renWin.AddRenderer(aRenderer);

            Marshal.Copy(ys1, 0, temp128, imgLine * imgPixel * imgNum);


            temp.SetArray(temp128, imgLine * imgPixel * imgNum, 1);

            testimgdata.SetDimensions(imgLine, imgPixel, imgNum);
            testimgdata.SetSpacing(1, 1, 4);
            testimgdata.SetScalarTypeToUnsignedChar();
            testimgdata.SetNumberOfScalarComponents(1);
            testimgdata.AllocateScalars();
            testimgdata.GetPointData().SetScalars(temp);
            testimgdata.Modified();

            VtkSetColor();
            VtkSetOpacity();

            volumeProperty.SetColor(colorTransferFunction);
            volumeProperty.SetScalarOpacity(opacityTransferFunction);
            volumeProperty.ShadeOn();
            volumeProperty.SetInterpolationTypeToLinear();

            volumeMapper.SetVolumeRayCastFunction(compositeFunction);
            volumeMapper.SetInput(testimgdata);

            volume.SetMapper(volumeMapper);
            volume.SetProperty(volumeProperty);

            aCamera.SetViewUp(0, -1, 0);
            aCamera.SetPosition(-1, 0, 1);
            aCamera.ComputeViewPlaneNormal();

            aRenderer.AddVolume(volume);
            aRenderer.SetBackground(0, 0, 0.6);
            aRenderer.SetActiveCamera(aCamera);
            aRenderer.ResetCamera();
            renWin.SetSize(800, 800);
            renWin.Render();
        }
Пример #5
0
        /// <summary>
        /// Load image files into vtkImageData.
        /// </summary>
        /// <param name="path">Path to images.</param>
        /// <param name="extension">Image extension.</param>
        /// <returns></returns>
        public static vtkImageData VTKLoader(string path, string extension)
        {
            /*DEPRECATED!!*/
            //Output
            vtkImageData data = vtkImageData.New();
            //Get files from path
            DirectoryInfo d = new DirectoryInfo(@path);

            FileInfo[] files = d.GetFiles();

            vtkStringArray allfiles = vtkStringArray.New();

            //Iterate over files and read image data
            foreach (FileInfo file in files)
            {
                //Fullfile
                string fullfile = Path.Combine(path, file.Name);
                allfiles.InsertNextValue(fullfile);
            }
            if (extension == ".png")
            {
                vtkPNGReader reader = vtkPNGReader.New();
                reader.SetFileNames(allfiles);
                reader.Update();
                data = reader.GetOutput();
                reader.Dispose();
            }
            if (extension == ".jpg")
            {
                vtkJPEGReader reader = vtkJPEGReader.New();
                reader.SetFileNames(allfiles);
                reader.Update();
                data = reader.GetOutput();
                reader.Dispose();
            }
            if (extension == ".bmp")
            {
                vtkBMPReader reader = vtkBMPReader.New();
                reader.SetFileNames(allfiles);
                reader.Update();
                data = reader.GetOutput();
                reader.Dispose();
            }
            data.SetScalarTypeToUnsignedChar();
            data.Update();
            return(data);
        }
Пример #6
0
        private void GreedyTerrainDecimation()
        {
            // Create an image
            vtkImageData image = vtkImageData.New();

            image.SetDimensions(3, 3, 1);
            image.SetNumberOfScalarComponents(1);
            image.SetScalarTypeToUnsignedChar();
            int[] dims = image.GetDimensions();
            unsafe {
                for (int i = 0; i < dims[0]; i++)
                {
                    for (int j = 0; j < dims[1]; j++)
                    {
                        byte *ptr = (byte *)image.GetScalarPointer(i, j, 0);
                        *     ptr = (byte)vtkMath.Round(vtkMath.Random(0, 1));
                    }
                }
            }
            vtkGreedyTerrainDecimation decimation = vtkGreedyTerrainDecimation.New();

            decimation.SetInputConnection(image.GetProducerPort());
            decimation.Update();
            vtkPolyDataMapper mapper = vtkPolyDataMapper.New();

            mapper.SetInputConnection(decimation.GetOutputPort());
            // actor
            vtkActor actor = vtkActor.New();

            actor.SetMapper(mapper);
            actor.GetProperty().SetInterpolationToFlat();
            actor.GetProperty().EdgeVisibilityOn();
            actor.GetProperty().SetEdgeColor(1, 0, 0);

            // get a reference to the renderwindow of our renderWindowControl1
            vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow;
            // renderer
            vtkRenderer renderer = renderWindow.GetRenderers().GetFirstRenderer();

            // set background color
            renderer.SetBackground(0.2, 0.3, 0.4);
            // add our actor to the renderer
            renderer.AddActor(actor);
        }
        //把fyGrid转为vtkImageData
        private vtkImageData ConvertfyGrid2vtkImageData(ExGrid Grid)
        {
            vtkImageData ImageData = vtkImageData.New();

            ImageData.SetScalarTypeToUnsignedChar();

            ImageData.SetNumberOfScalarComponents(1);
            ImageData.SetDimensions(Grid.ICount, Grid.JCount, Grid.KCount);
            ImageData.SetSpacing(1, 1, 1);
            ImageData.AllocateScalars();
            byte[] data = new byte[Grid.CellCount];

            for (int i = 0; i < Grid.CellCount; i++)
            {
                data[i] = (byte)Grid.GetCell(i).Value;
            }
            System.Runtime.InteropServices.Marshal.Copy(data, 0, (IntPtr)ImageData.GetScalarPointer(), data.Length);
            return(ImageData);
        }
Пример #8
0
        /// <summary>
        /// Converts 3D byte array to vtkImageData.
        /// </summary>
        /// <param name="data">Input array.</param>
        /// <param name="orientation">Data orientation as a list of axes (0-2)</param>
        /// <returns>Converted array.</returns>
        public static vtkImageData byteToVTK(byte[,,] data, int[] orientation = null)
        {
            //Get input dimensions
            int[] dims = new int[] { data.GetLength(0), data.GetLength(1), data.GetLength(2) };
            //Create VTK data for putput
            vtkImageData vtkdata = vtkImageData.New();
            //Character array for conversion
            vtkUnsignedCharArray charArray = vtkUnsignedCharArray.New();
            //Pin byte array
            GCHandle pinnedArray = GCHandle.Alloc(data, GCHandleType.Pinned);

            //Set character array input
            charArray.SetArray(pinnedArray.AddrOfPinnedObject(), dims[0] * dims[1] * dims[2], 1);
            //Set vtkdata properties and connect array
            //Data from char array
            vtkdata.GetPointData().SetScalars(charArray);
            //Number of scalars/pixel
            vtkdata.SetNumberOfScalarComponents(1);
            //Data extent, 1st and last axis are swapped from the char array
            //Data is converted back to original orientation
            vtkdata.SetExtent(0, dims[2] - 1, 0, dims[1] - 1, 0, dims[0] - 1);
            //Scalar type
            vtkdata.SetScalarTypeToUnsignedChar();
            vtkdata.SetSpacing(1.0, 1.0, 1.0);
            vtkdata.Update();
            pinnedArray.Free();
            //Return vtk data
            if (orientation == null)
            {
                return(vtkdata);
            }
            else
            {
                vtkImagePermute permuter = vtkImagePermute.New();
                permuter.SetInput(vtkdata);
                permuter.SetFilteredAxes(orientation[0], orientation[1], orientation[2]);
                permuter.Update();
                return(permuter.GetOutput());
            }
        }
Пример #9
0
        /// <summary>
        /// Calculates mean and standard deviation images from volume-of-interest. Obsolete.
        /// </summary>
        /// <param name="output"></param>
        /// <param name="mu"></param>
        /// <param name="std"></param>
        /// <param name="input"></param>
        /// <param name="depth"></param>
        /// <param name="threshold"></param>
        public static void get_voi_mu_std(out vtkImageData output, out double[,] mu, out double[,] std, vtkImageData input, int depth, double threshold = 70.0)
        {
            //Get data extent
            int[] dims = input.GetExtent();
            int   h    = dims[1] - dims[0] + 1;
            int   w    = dims[3] - dims[2] + 1;
            int   d    = dims[5] - dims[4] + 1;

            //Compute strides
            int stridew = 1;
            int strideh = w;
            int strided = h * w;

            byte[] bytedata = DataTypes.vtkToByte(input);
            byte[] voidata  = new byte[bytedata.Length];

            double[,] _mu  = new double[h, w];
            double[,] _std = new double[h, w];

            //Get voi indices
            Parallel.For(24, h - 24, (int y) =>
            {
                Parallel.For(24, w - 24, (int x) =>
                {
                    int start = d - 1;
                    int stop  = 0;
                    //Compute mean
                    for (int z = d - 1; z > 0; z -= 1)
                    {
                        int pos    = z * strided + y * strideh + x * stridew;
                        double val = (double)bytedata[pos];
                        if (val >= threshold)
                        {
                            start = z;
                            stop  = Math.Max(z - depth, 0);
                            //Compute mean and std
                            for (int zz = start; zz > stop; zz -= 1)
                            {
                                int newpos      = zz * strided + y * strideh + x * stridew;
                                double newval   = (double)bytedata[newpos];
                                voidata[newpos] = (byte)newval;
                                _mu[y, x]       = newval / (double)depth;
                            }

                            for (int zz = start; zz > stop; zz -= 1)
                            {
                                int newpos    = zz * strided + y * strideh + x * stridew;
                                double newval = (double)bytedata[newpos];
                                _std[y, x]   += ((double)newval - _mu[y, x]) * ((double)newval - _mu[y, x]);
                            }

                            _std[y, x] = Math.Pow(_std[y, x] / (depth - 1), 0.5);
                            break;
                        }
                    }
                });
            });

            mu     = _mu;
            std    = _std;
            output = vtkImageData.New();
            //Copy voi data to input array
            vtkUnsignedCharArray charArray = vtkUnsignedCharArray.New();
            //Pin byte array
            GCHandle pinnedArray = GCHandle.Alloc(voidata, GCHandleType.Pinned);

            //Set character array input
            charArray.SetArray(pinnedArray.AddrOfPinnedObject(), h * w * d, 1);
            //Set vtkdata properties and connect array
            //Data from char array
            output.GetPointData().SetScalars(charArray);
            //Number of scalars/pixel
            output.SetNumberOfScalarComponents(1);
            //Data extent, 1st and last axis are swapped from the char array
            //Data is converted back to original orientation
            output.SetExtent(dims[0], dims[1], dims[2], dims[3], dims[4], dims[5]);
            //Scalar type
            output.SetScalarTypeToUnsignedChar();
            output.Update();
        }
Пример #10
0
		private static vtkImageData CreateVtkVolume(Volume volume)
		{
			var vtkVolume = new vtkImageData();
			vtkVolume.RegisterVtkErrorEvents();
			vtkVolume.SetDimensions(volume.ArrayDimensions.Width, volume.ArrayDimensions.Height, volume.ArrayDimensions.Depth);
			vtkVolume.SetOrigin(0, 0, 0);
			vtkVolume.SetSpacing(volume.VoxelSpacing.X, volume.VoxelSpacing.Y, volume.VoxelSpacing.Z);

			if (volume.BitsPerVoxel == 16)
			{
				if (!volume.Signed)
				{
					using (var array = new vtkUnsignedShortArray())
					{
						array.SetArray((ushort[]) volume.Array, (VtkIdType) volume.ArrayLength, 1);

						vtkVolume.SetScalarTypeToUnsignedShort();
						vtkVolume.GetPointData().SetScalars(array);
					}
				}
				else
				{
					using (var array = new vtkShortArray())
					{
						array.SetArray((short[]) volume.Array, (VtkIdType) volume.ArrayLength, 1);

						vtkVolume.SetScalarTypeToShort();
						vtkVolume.GetPointData().SetScalars(array);
					}
				}
			}
			else if (volume.BitsPerVoxel == 8)
			{
				if (!volume.Signed)
				{
					using (var array = new vtkUnsignedCharArray())
					{
						array.SetArray((byte[]) volume.Array, (VtkIdType) volume.ArrayLength, 1);

						vtkVolume.SetScalarTypeToUnsignedChar();
						vtkVolume.GetPointData().SetScalars(array);
					}
				}
				else
				{
					using (var array = new vtkSignedCharArray())
					{
						array.SetArray((sbyte[]) volume.Array, (VtkIdType) volume.ArrayLength, 1);

						vtkVolume.SetScalarTypeToSignedChar();
						vtkVolume.GetPointData().SetScalars(array);
					}
				}
			}
			else
			{
				throw new NotSupportedException("Unsupported volume scalar type.");
			}

			// This call is necessary to ensure vtkImageData data's info is correct (e.g. updates WholeExtent values)
			vtkVolume.UpdateInformation();

			return vtkVolume;
		}
Пример #11
0
        private static vtkImageData CreateVtkVolume(Volume volume)
        {
            var vtkVolume = new vtkImageData();

            vtkVolume.RegisterVtkErrorEvents();
            vtkVolume.SetDimensions(volume.ArrayDimensions.Width, volume.ArrayDimensions.Height, volume.ArrayDimensions.Depth);
            vtkVolume.SetOrigin(0, 0, 0);
            vtkVolume.SetSpacing(volume.VoxelSpacing.X, volume.VoxelSpacing.Y, volume.VoxelSpacing.Z);

            if (volume.BitsPerVoxel == 16)
            {
                if (!volume.Signed)
                {
                    using (var array = new vtkUnsignedShortArray())
                    {
                        array.SetArray((ushort[])volume.Array, (VtkIdType)volume.ArrayLength, 1);

                        vtkVolume.SetScalarTypeToUnsignedShort();
                        vtkVolume.GetPointData().SetScalars(array);
                    }
                }
                else
                {
                    using (var array = new vtkShortArray())
                    {
                        array.SetArray((short[])volume.Array, (VtkIdType)volume.ArrayLength, 1);

                        vtkVolume.SetScalarTypeToShort();
                        vtkVolume.GetPointData().SetScalars(array);
                    }
                }
            }
            else if (volume.BitsPerVoxel == 8)
            {
                if (!volume.Signed)
                {
                    using (var array = new vtkUnsignedCharArray())
                    {
                        array.SetArray((byte[])volume.Array, (VtkIdType)volume.ArrayLength, 1);

                        vtkVolume.SetScalarTypeToUnsignedChar();
                        vtkVolume.GetPointData().SetScalars(array);
                    }
                }
                else
                {
                    using (var array = new vtkSignedCharArray())
                    {
                        array.SetArray((sbyte[])volume.Array, (VtkIdType)volume.ArrayLength, 1);

                        vtkVolume.SetScalarTypeToSignedChar();
                        vtkVolume.GetPointData().SetScalars(array);
                    }
                }
            }
            else
            {
                throw new NotSupportedException("Unsupported volume scalar type.");
            }

            // This call is necessary to ensure vtkImageData data's info is correct (e.g. updates WholeExtent values)
            vtkVolume.UpdateInformation();

            return(vtkVolume);
        }