Ejemplo n.º 1
0
        public static void segmentation_pipeline(out List <vtkImageData> outputs, Rendering.renderPipeLine volume, int[] batch_d, int[] extent, int[] axes, string wpath, int bs = 2)
        {
            //Outputs
            outputs = new List <vtkImageData>();
            //Get input dimensions
            int[] dims = volume.getDims();

            // Initialize UNet
            UNet model = new UNet();

            try
            {
                model.Initialize(24, batch_d, wpath, false);
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("Model not found");

                return;
            }

            for (int k = 0; k < axes.Length; k++)
            {
                outputs.Add(vtkImageData.New());
            }

            //Segment BCI from axis
            for (int k = 0; k < axes.Length; k++)
            {
                //Inference
                IList <IList <float> > result = segment_sample(volume, model, extent, axes[k], bs, (float)108.58790588378906, (float)47.622276306152344);
                //Copy result to vtkImagedata
                vtkImageData _tmp = IO.inference_to_vtk(result, new int[] { dims[1] + 1, dims[3] + 1, dims[5] + 1 }, extent, axes[k]);

                outputs.ElementAt(k).DeepCopy(_tmp);
                _tmp.Dispose();
                result = null;
            }
            model.Dispose();
        }
Ejemplo n.º 2
0
        public static void segmentation_pipeline(out List <vtkImageData> outputs, Rendering.renderPipeLine volume, int[] batch_d, int[] extent, int[] axes, int bs = 2)
        {
            //Outputs
            outputs = new List <vtkImageData>();
            //Get input dimensions
            int[] dims = volume.getDims();

            //Initialize unet
            string wpath = "Z:\\Tuomas\\UNetE3BN.h5";

            UNet model = new UNet();

            model.Initialize(24, batch_d, wpath, false);

            //Segment BCI from axis
            foreach (int axis in axes)
            {
                IList <IList <float> > result = segment_sample(volume, model, extent, axis, bs, (float)113.05652141, (float)39.87462853);
                //Convert back to vtkimage data
                vtkImageData tmp = IO.inference_to_vtk(result, new int[] { dims[1] + 1, dims[3] + 1, dims[5] + 1 }, extent, axis);
                outputs.Add(tmp);
            }
        }
Ejemplo n.º 3
0
        public static IList <IList <float> > segment_sample(Rendering.renderPipeLine vtkObject, UNet model, int[] extent, int axis,
                                                            int step = 1, float mu = 0, float sd = 0)
        {
            //Segmentation range
            int[] bounds = new int[] { extent[axis * 2], extent[axis * 2 + 1] };
            //Output list
            IList <IList <float> > output = null;

            //Iterate over vtk data
            for (int k = 0; k < (bounds[1] - bounds[0]) / step; k++)
            {
                //Set current VOI and orientation
                int[] _curext = new int[6];
                int[] _ori    = new int[3];
                if (axis == 0)
                {
                    int start = extent[0] + k * step;
                    int stop  = Math.Min(extent[0] + (k + 1) * step - 1, extent[1]);
                    _curext = new int[] { start, stop, extent[2], extent[3], extent[4], extent[5] };
                    _ori    = new int[] { 2, 1, 0 };
                }
                if (axis == 1)
                {
                    int start = extent[2] + k * step;
                    int stop  = Math.Min(extent[2] + (k + 1) * step - 1, extent[3]);
                    _curext = new int[] { extent[0], extent[1], start, stop, extent[4], extent[5] };
                    _ori    = new int[] { 2, 0, 1 };
                }
                if (axis == 2)
                {
                    int start = extent[4] + k * step;
                    int stop  = Math.Min(extent[4] + (k + 1) * step - 1, extent[5]);
                    _curext = new int[] { extent[0], extent[1], extent[2], extent[3], start, stop };
                    _ori    = new int[] { 0, 1, 2 };
                }

                //Extract VOI to float array
                float[] input_array = DataTypes.byteToFloat(DataTypes.vtkToByte(vtkObject.getVOI(_curext, _ori)), mu, sd);

                //Segment current slice
                IList <IList <float> > _cur = model.Inference(input_array);
                //Append results to output list
                if (output == null)
                {
                    output = _cur;
                }
                else
                {
                    //Loop over slices
                    foreach (IList <float> item in _cur)
                    {
                        output.Add(item);
                    }
                }
                GC.Collect();
            }

            //Return output list
            return(output);
        }