/// <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); }
/// <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); }
/// <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()); } }
public static void UpdateSectionImage(Rectangle imageRect) { if (SectionImageData == null) { SectionImageData = vtkImageData.New(); } SectionImageData.DeepCopy(RectImageData); for (int i = 0; i < imageRect.Width; i++) { SectionImageData.GetPointData().GetScalars().SetTuple1(HSectionNum * imageRect.Width + i, 255); } for (int i = 0; i < imageRect.Height; i++) { SectionImageData.GetPointData().GetScalars().SetTuple1(i * imageRect.Width + VSectionNum, 255); } SectionImageData.Update(); UpdateImageView(); }
/// <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(); }