Example #1
0
        private Point3d GetSpacing(DicomFile[] files)
        {
            Point3d spacing = new Point3d(0, 0, 0);

            if (files.Length == 1)
            {
                return(GetSpacing(files[0]));
            }
            else
            {
                PixelDataInfo fileHeader     = new PixelDataInfo(files[0]);
                double        thirdDimension = 0;
                if (fileHeader.SliceThickness != 0)
                {
                    thirdDimension = fileHeader.SliceThickness;
                }
                else if (fileHeader.GridFrameOffsetVector.Length > 1)
                {
                    thirdDimension = fileHeader.GridFrameOffsetVector[1] - fileHeader.GridFrameOffsetVector[0];
                }

                spacing.X += Math.Abs(fileHeader.RowDir.X) * fileHeader.PixelSpacing[1] + Math.Abs(fileHeader.ColDir.X) * fileHeader.PixelSpacing[0];
                spacing.Y += Math.Abs(fileHeader.RowDir.Y) * fileHeader.PixelSpacing[1] + Math.Abs(fileHeader.ColDir.Y) * fileHeader.PixelSpacing[0];
                spacing.Z += Math.Abs(fileHeader.RowDir.Z) * fileHeader.PixelSpacing[1] + Math.Abs(fileHeader.ColDir.Z) * fileHeader.PixelSpacing[0];
                spacing.X += (1 - Math.Abs(fileHeader.RowDir.X) - Math.Abs(fileHeader.ColDir.X)) * thirdDimension;
                spacing.Y += (1 - Math.Abs(fileHeader.RowDir.Y) - Math.Abs(fileHeader.ColDir.Y)) * thirdDimension;
                spacing.Z += (1 - Math.Abs(fileHeader.RowDir.Z) - Math.Abs(fileHeader.ColDir.Z)) * thirdDimension;
            }
            return(spacing);
        }
Example #2
0
        private float[] GetCoords(DicomFile[] files, int size, double rowDir, double colDir, double position, int scale)
        {
            PixelDataInfo tempHeader = new PixelDataInfo(files[0]);

            float[] coords = new float[size];
            if (rowDir != 0 || colDir != 0)
            {
                for (int i = 0; i < size; i++)
                {
                    coords[i] = (float)(position + i * rowDir * tempHeader.PixelSpacing[1] + i * colDir * tempHeader.PixelSpacing[0]);
                }
            }
            else
            {
                if (tempHeader.GridFrameOffsetVector.Length > 1)
                {
                    for (int i = 0; i < size; i++)
                    {
                        coords[i] = (float)(position + tempHeader.GridFrameOffsetVector[i]);
                    }
                }
                for (int i = 0; i < files.Length; i++)
                {
                    coords[i] = (float)(position + i * tempHeader.SliceThickness);
                }
            }
            return(coords);
        }
 private PixelDataInfo[] GetHeaderInfo(DicomFile[] files)
 {
     PixelDataInfo[] headerInfo = new PixelDataInfo[files.Length];
     for (int i = 0; i < files.Length; i++)
     {
         headerInfo[i] = new PixelDataInfo(files[i]);
     }
     return(headerInfo);
 }
Example #4
0
        private Point3d GetSize(DicomFile file)
        {
            PixelDataInfo fileHeader = new PixelDataInfo(file);
            Point3d       size       = new Point3d();

            size.X += Math.Abs(fileHeader.RowDir.X) * fileHeader.Columns + Math.Abs(fileHeader.ColDir.X) * fileHeader.Rows;
            size.Y += Math.Abs(fileHeader.RowDir.Y) * fileHeader.Columns + Math.Abs(fileHeader.ColDir.Y) * fileHeader.Rows;
            size.Z += Math.Abs(fileHeader.RowDir.Z) * fileHeader.Columns + Math.Abs(fileHeader.ColDir.Z) * fileHeader.Rows;
            size.X += (1 - Math.Abs(fileHeader.RowDir.X) - Math.Abs(fileHeader.ColDir.X)) * fileHeader.GridFrameOffsetVector.Length;
            size.Y += (1 - Math.Abs(fileHeader.RowDir.Y) - Math.Abs(fileHeader.ColDir.Y)) * fileHeader.GridFrameOffsetVector.Length;
            size.Z += (1 - Math.Abs(fileHeader.RowDir.Z) - Math.Abs(fileHeader.ColDir.Z)) * fileHeader.GridFrameOffsetVector.Length;
            return(size);
        }
Example #5
0
        public GridBasedVoxelDataStructure Load(DicomFile[] dicomFiles)
        {
            GridBasedVoxelDataStructure grid = new GridBasedVoxelDataStructure();

            DicomFile[]   files       = SortOnZIncreasing(dicomFiles);
            PixelDataInfo DicomHeader = new PixelDataInfo(files[0]);
            Point3d       size        = GetSize(files);

            grid.ConstantGridSpacing = true;
            grid.GridSpacing         = GetSpacing(files);
            grid.XCoords             = GetCoords(files, (int)size.X, DicomHeader.RowDir.X, DicomHeader.ColDir.X, DicomHeader.ImagePositionPatient.X, 1);
            grid.YCoords             = GetCoords(files, (int)size.Y, DicomHeader.RowDir.Y, DicomHeader.ColDir.Y, DicomHeader.ImagePositionPatient.Y, 1);
            grid.ZCoords             = GetCoords(files, (int)size.Z, DicomHeader.RowDir.Z, DicomHeader.ColDir.Z, DicomHeader.ImagePositionPatient.Z, 1);
            grid.Data = new float[(int)size.X / 1, (int)size.Y / 1, (int)size.Z / 1];

            int currYIndex = 0;
            int currZIndex = 0;
            int currXIndex = 0;

            for (int i = 0; i < files.Length; i++)
            {
                DicomPixelData dicomPixelData = DicomPixelData.Create(files[i].Dataset);
                PixelDataInfo  tempHeaderInfo = new PixelDataInfo(files[i]);
                for (int j = 0; j < tempHeaderInfo.GridFrameOffsetVector.Length; j++)
                {
                    byte[]  framePixelData = dicomPixelData.GetFrame(j).Data;
                    float[] dataArray      = GetDataArray(framePixelData, tempHeaderInfo.BitsAllocated, tempHeaderInfo.PixelRepresentation);
                    for (int k = 0; k < dataArray.Length; k++)
                    {
                        int currRow = k % tempHeaderInfo.Columns;
                        int currCol = (int)Math.Floor((double)(k / (tempHeaderInfo.Columns)));
                        currXIndex = (int)Math.Abs(tempHeaderInfo.RowDir.X) * currRow + (int)Math.Abs(tempHeaderInfo.ColDir.X) * currCol
                                     + (1 - (int)Math.Abs(tempHeaderInfo.RowDir.X) - (int)Math.Abs(tempHeaderInfo.ColDir.X)) * (i + j);;
                        currYIndex = (int)Math.Abs(tempHeaderInfo.RowDir.Y) * currRow + (int)Math.Abs(tempHeaderInfo.ColDir.Y) * currCol
                                     + (1 - (int)Math.Abs(tempHeaderInfo.RowDir.Y) - (int)Math.Abs(tempHeaderInfo.ColDir.Y)) * (i + j);;
                        currZIndex = (int)Math.Abs(tempHeaderInfo.RowDir.Z) * currRow + (int)Math.Abs(tempHeaderInfo.ColDir.Z) * currCol
                                     + (1 - (int)Math.Abs(tempHeaderInfo.RowDir.Z) - (int)Math.Abs(tempHeaderInfo.ColDir.Z)) * (i + j);

                        //Rescale back to actual values
                        dataArray[k] = dataArray[k] * tempHeaderInfo.RescaleSlope + tempHeaderInfo.RescaleSlope;
                        grid.Data[currXIndex, currYIndex, currZIndex] = dataArray[k];
                    }
                }
            }
            return(grid);
            //setGlobalMax();
        }
        public SliceBasedVoxelDataStructure Load(DicomFile[] files)
        {
            SliceBasedVoxelDataStructure grid = new SliceBasedVoxelDataStructure();

            files = sort(files);

            PixelDataInfo[] headerInfos = GetHeaderInfo(files);

            if (files.Length > 1)
            {
                grid.MatrixA = getMatrixA(files.First(), files.Last(), files.Length);
            }
            else
            {
                grid.MatrixA = getSingleMatrixA(files[0]);
            }

            for (int i = 0; i < files.Length; i++)
            {
                PixelDataInfo  headerInfo     = headerInfos[i];
                DicomPixelData dicomPixelData = DicomPixelData.Create(files[i].Dataset);

                for (int j = 0; j < headerInfo.GridFrameOffsetVector.Length; j++)
                {
                    byte[]  framePixelData = dicomPixelData.GetFrame(j).Data;
                    float[] dataArray      = GetDataArray(framePixelData, dicomPixelData.BitsAllocated, headerInfo.PixelRepresentation);
                    //rescale to proper values
                    for (int k = 0; k < dataArray.Length; k++)
                    {
                        dataArray[k] = dataArray[k] * headerInfo.RescaleSlope + headerInfo.RescaleIntercept;
                    }

                    grid.AddSlice(dataArray, headerInfo.Rows, headerInfo.Columns, headerInfo.PixelSpacing[0], headerInfo.PixelSpacing[1], headerInfo.ImagePositionPatient.X, headerInfo.ImagePositionPatient.Y, headerInfo.ImagePositionPatient.Z + headerInfo.GridFrameOffsetVector[j], headerInfo.RowDir.X, headerInfo.RowDir.Y, headerInfo.RowDir.Z, headerInfo.ColDir.X, headerInfo.ColDir.Y, headerInfo.ColDir.Z);
                }
                files[i] = null;
            }

            //grid.Window = (int)grid.GlobalMax.Value ;
            //grid.Level = (int)(grid.GlobalMax.Value / 2);

            return(grid);
        }
Example #7
0
        private Point3d GetSize(DicomFile[] files)
        {
            Point3d size = new Point3d(0, 0, 0);

            if (files.Length == 1)
            {
                return(GetSize(files[0]));
            }
            else
            {
                PixelDataInfo fileHeader = new PixelDataInfo(files[0]);
                size.X += Math.Abs(fileHeader.RowDir.X) * fileHeader.Columns + Math.Abs(fileHeader.ColDir.X) * fileHeader.Rows;
                size.Y += Math.Abs(fileHeader.RowDir.Y) * fileHeader.Columns + Math.Abs(fileHeader.ColDir.Y) * fileHeader.Rows;
                size.Z += Math.Abs(fileHeader.RowDir.Z) * fileHeader.Columns + Math.Abs(fileHeader.ColDir.Z) * fileHeader.Rows;
                size.X += (1 - Math.Abs(fileHeader.RowDir.X) - Math.Abs(fileHeader.ColDir.X)) * files.Length;
                size.Y += (1 - Math.Abs(fileHeader.RowDir.Y) - Math.Abs(fileHeader.ColDir.Y)) * files.Length;
                size.Z += (1 - Math.Abs(fileHeader.RowDir.Z) - Math.Abs(fileHeader.ColDir.Z)) * files.Length;
            }
            return(size);
        }
Example #8
0
        public GridBasedVoxelDataStructure Load(DicomFile[] dicomFiles, IProgress <double> progress)
        {
            GridBasedVoxelDataStructure grid = new GridBasedVoxelDataStructure();

            DicomFile[]   files       = SortOnZIncreasing(dicomFiles);
            PixelDataInfo DicomHeader = new PixelDataInfo(files[0]);
            Point3d       size        = GetSize(files);

            grid.ConstantGridSpacing = true;
            grid.GridSpacing         = GetSpacing(files);
            grid.XCoords             = GetCoords(files, (int)size.X, DicomHeader.RowDir.X, DicomHeader.ColDir.X, DicomHeader.ImagePositionPatient.X, 1);
            grid.YCoords             = GetCoords(files, (int)size.Y, DicomHeader.RowDir.Y, DicomHeader.ColDir.Y, DicomHeader.ImagePositionPatient.Y, 1);
            grid.ZCoords             = GetCoords(files, (int)size.Z, DicomHeader.RowDir.Z, DicomHeader.ColDir.Z, DicomHeader.ImagePositionPatient.Z, 1);

            grid.Data = new float[(int)size.X * (int)size.Y * (int)size.Z];

            int currYIndex = 0;
            int currZIndex = 0;
            int currXIndex = 0;

            for (int i = 0; i < files.Length; i++)
            {
                DicomPixelData dicomPixelData = DicomPixelData.Create(files[i].Dataset);
                PixelDataInfo  tempHeaderInfo = new PixelDataInfo(files[i]);
                for (int j = 0; j < tempHeaderInfo.GridFrameOffsetVector.Length; j++)
                {
                    byte[]  framePixelData = dicomPixelData.GetFrame(j).Data;
                    float[] dataArray      = GetDataArray(framePixelData, tempHeaderInfo.BitsAllocated, tempHeaderInfo.PixelRepresentation);
                    for (int k = 0; k < dataArray.Length; k++)
                    {
                        int currRow = k % tempHeaderInfo.Columns;
                        int currCol = (int)Math.Floor((double)(k / (tempHeaderInfo.Columns)));
                        currXIndex = (int)Math.Abs(tempHeaderInfo.RowDir.X) * currRow + (int)Math.Abs(tempHeaderInfo.ColDir.X) * currCol
                                     + (1 - (int)Math.Abs(tempHeaderInfo.RowDir.X) - (int)Math.Abs(tempHeaderInfo.ColDir.X)) * (i + j);;
                        currYIndex = (int)Math.Abs(tempHeaderInfo.RowDir.Y) * currRow + (int)Math.Abs(tempHeaderInfo.ColDir.Y) * currCol
                                     + (1 - (int)Math.Abs(tempHeaderInfo.RowDir.Y) - (int)Math.Abs(tempHeaderInfo.ColDir.Y)) * (i + j);;
                        currZIndex = (int)Math.Abs(tempHeaderInfo.RowDir.Z) * currRow + (int)Math.Abs(tempHeaderInfo.ColDir.Z) * currCol
                                     + (1 - (int)Math.Abs(tempHeaderInfo.RowDir.Z) - (int)Math.Abs(tempHeaderInfo.ColDir.Z)) * (i + j);

                        //Rescale back to actual HUs
                        dataArray[k] = dataArray[k] * tempHeaderInfo.RescaleSlope + tempHeaderInfo.RescaleIntercept;
                        grid.SetVoxelByIndices(currXIndex, currYIndex, currZIndex, dataArray[k]);

                        if (dataArray[k] > grid.MaxVoxel.Value)
                        {
                            grid.MaxVoxel.Value      = dataArray[k];
                            grid.MaxVoxel.Position.X = grid.XCoords[currXIndex];
                            grid.MaxVoxel.Position.Y = grid.YCoords[currYIndex];
                            grid.MaxVoxel.Position.Z = grid.ZCoords[currZIndex];
                        }
                        if (dataArray[k] < grid.MinVoxel.Value)
                        {
                            grid.MinVoxel.Value      = dataArray[k];
                            grid.MinVoxel.Position.X = grid.XCoords[currXIndex];
                            grid.MinVoxel.Position.Y = grid.YCoords[currYIndex];
                            grid.MinVoxel.Position.Z = grid.ZCoords[currZIndex];
                        }
                    }

                    progress.Report(100 * ((((double)j / (double)tempHeaderInfo.GridFrameOffsetVector.Length) / files.Length) + ((double)i / (double)files.Length)));
                }
            }
            grid.XRange = new Range(grid.XCoords[0], grid.XCoords[grid.XCoords.Length - 1]);
            grid.YRange = new Range(grid.YCoords[0], grid.YCoords[grid.YCoords.Length - 1]);
            grid.ZRange = new Range(grid.ZCoords[0], grid.ZCoords[grid.ZCoords.Length - 1]);

            //grid.ComputeMax();
            return(grid);
        }