Exemplo n.º 1
0
        public static List <string> CreateVolume(string outputDirectory, string outputName, params string[] sortedInputSliceFilenames)
        {
            var volumeMetadata   = new VolumeData();
            var imageSerializer  = new XmlSerializer(typeof(ImageData));
            var volumeSerializer = new XmlSerializer(typeof(VolumeData));

            Directory.CreateDirectory(outputDirectory);

            var volumeRawFilename  = Path.Combine(outputDirectory, outputName + ".raw");
            var volumeRawStream    = File.Create(volumeRawFilename);
            var firstSlicePosition = new[] { 0.0, 0.0, 0.0 };
            var lastSlicePosition  = new[] { 0.0, 0.0, 0.0 };
            var sliceCount         = 0;

            foreach (var inputFilename in sortedInputSliceFilenames)
            {
                try
                {
                    using (var inputStream = File.OpenRead(inputFilename))
                    {
                        var imageData = (ImageData)imageSerializer.Deserialize(inputStream);
                        volumeRawStream.Write(imageData.PixelData, 0, imageData.PixelData.Length);
                        if (sliceCount == 0) // Use first slice as reference slice for volume.
                        {
                            firstSlicePosition = imageData.ImagePositionPatient;
                            volumeMetadata.FirstSliceLocation = imageData.SliceLocation;
                            volumeMetadata.Columns            = imageData.Columns;
                            volumeMetadata.Rows   = imageData.Rows;
                            volumeMetadata.Height = imageData.Height;
                            volumeMetadata.Width  = imageData.Width;
                            volumeMetadata.ImageOrientationPatient = imageData.ImageOrientationPatient;
                            volumeMetadata.RescaleIntercept        = imageData.RescaleIntercept;
                            volumeMetadata.RescaleSlope            = imageData.RescaleSlope;
                            volumeMetadata.WindowCenter            = imageData.WindowCenter;
                            volumeMetadata.WindowWidth             = imageData.WindowWidth;
                        }

                        volumeMetadata.MinIntensity      = Math.Min(volumeMetadata.MinIntensity, imageData.MinIntensity);
                        volumeMetadata.MaxIntensity      = Math.Max(volumeMetadata.MaxIntensity, imageData.MaxIntensity);
                        volumeMetadata.LastSliceLocation = imageData.SliceLocation;
                        lastSlicePosition = imageData.ImagePositionPatient;
                        sliceCount++;
                    }
                }
                catch (Exception err)
                {
                    Logger.Debug("Problem reading slice: " + inputFilename + ". " + err.Message);
                }
            }
            volumeRawStream.Close();

            volumeMetadata.Depth  = Math.Abs(volumeMetadata.LastSliceLocation - volumeMetadata.FirstSliceLocation);
            volumeMetadata.Slices = sliceCount;
            volumeMetadata.ImagePositionPatient = new[]
            {
                (lastSlicePosition[0] + firstSlicePosition[0]) / 2.0,
                (lastSlicePosition[1] + firstSlicePosition[1]) / 2.0,
                (lastSlicePosition[2] + firstSlicePosition[2]) / 2.0
            };

            var volumeXmlFilename = Path.Combine(outputDirectory, outputName + ".xml");
            var volumeXmlStream   = File.Create(volumeXmlFilename);

            volumeSerializer.Serialize(volumeXmlStream, volumeMetadata);
            volumeXmlStream.Close();

            return(new List <string> {
                volumeXmlFilename, volumeRawFilename
            });
        }
Exemplo n.º 2
0
        public static List<string> CreateVolume(string outputDirectory, string outputName, params string[] sortedInputSliceFilenames)
        {
            var volumeMetadata = new VolumeData();
            var imageSerializer = new XmlSerializer(typeof(ImageData));
            var volumeSerializer = new XmlSerializer(typeof (VolumeData));

            Directory.CreateDirectory(outputDirectory);

            var volumeRawFilename = Path.Combine(outputDirectory, outputName + ".raw");
            var volumeRawStream = File.Create(volumeRawFilename);
            var firstSlicePosition = new[] { 0.0, 0.0, 0.0 };
            var lastSlicePosition = new[] { 0.0, 0.0, 0.0 };
            var sliceCount = 0;

            foreach (var inputFilename in sortedInputSliceFilenames)
            {
                try
                {
                    using (var inputStream = File.OpenRead(inputFilename))
                    {
                        var imageData = (ImageData) imageSerializer.Deserialize(inputStream);
                        volumeRawStream.Write(imageData.PixelData, 0, imageData.PixelData.Length);
                        if (sliceCount == 0) // Use first slice as reference slice for volume.
                        {
                            firstSlicePosition = imageData.ImagePositionPatient;
                            volumeMetadata.FirstSliceLocation = imageData.SliceLocation;
                            volumeMetadata.Columns = imageData.Columns;
                            volumeMetadata.Rows = imageData.Rows;
                            volumeMetadata.Height = imageData.Height;
                            volumeMetadata.Width = imageData.Width;
                            volumeMetadata.ImageOrientationPatient = imageData.ImageOrientationPatient;
                            volumeMetadata.RescaleIntercept = imageData.RescaleIntercept;
                            volumeMetadata.RescaleSlope = imageData.RescaleSlope;
                            volumeMetadata.WindowCenter = imageData.WindowCenter;
                            volumeMetadata.WindowWidth = imageData.WindowWidth;
                        }

                        volumeMetadata.MinIntensity = Math.Min(volumeMetadata.MinIntensity, imageData.MinIntensity);
                        volumeMetadata.MaxIntensity = Math.Max(volumeMetadata.MaxIntensity, imageData.MaxIntensity);
                        volumeMetadata.LastSliceLocation = imageData.SliceLocation;
                        lastSlicePosition = imageData.ImagePositionPatient;
                        sliceCount++;

                    }
                }
                catch (Exception err)
                {
                    Logger.Debug("Problem reading slice: " + inputFilename + ". " + err.Message);
                }
            }
            volumeRawStream.Close();

            volumeMetadata.Depth = Math.Abs(volumeMetadata.LastSliceLocation - volumeMetadata.FirstSliceLocation);
            volumeMetadata.Slices = sliceCount;
            volumeMetadata.ImagePositionPatient = new[]
            {
                (lastSlicePosition[0] + firstSlicePosition[0]) / 2.0,
                (lastSlicePosition[1] + firstSlicePosition[1]) / 2.0,
                (lastSlicePosition[2] + firstSlicePosition[2]) / 2.0
            };

            var volumeXmlFilename = Path.Combine(outputDirectory, outputName + ".xml");
            var volumeXmlStream = File.Create(volumeXmlFilename);
            volumeSerializer.Serialize(volumeXmlStream, volumeMetadata);
            volumeXmlStream.Close();

            return new List<string> { volumeXmlFilename, volumeRawFilename };
        }